Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[AssetMapper] Truncate public digests to 8 characters
  • Loading branch information
smnandre authored and fabpot committed Aug 4, 2024
commit e1ae985e53e2da71132fae06cbcf789641ee8b17
5 changes: 5 additions & 0 deletions src/Symfony/Component/AssetMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Shorten the public digest of mapped assets to 7 characters

7.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class MappedAssetFactory implements MappedAssetFactoryInterface
{
private const PREDIGESTED_REGEX = '/-([0-9a-zA-Z]{7,128}\.digested)/';
private const PUBLIC_DIGEST_LENGTH = 7;

private array $assetsCache = [];
private array $assetsBeingCreated = [];
Expand Down Expand Up @@ -89,10 +90,7 @@ private function getDigest(MappedAsset $asset, ?string $content): array
return [hash('xxh128', $content), false];
}

return [
hash_file('xxh128', $asset->sourcePath),
false,
];
return [hash_file('xxh128', $asset->sourcePath), false];
}

private function compileContent(MappedAsset $asset): ?string
Expand All @@ -119,6 +117,7 @@ private function getPublicPath(MappedAsset $asset, ?string $content): ?string
return $this->assetsPathResolver->resolvePublicPath($asset->logicalPath);
}

$digest = substr(base64_encode($digest), 0, self::PUBLIC_DIGEST_LENGTH);
$digestedPath = preg_replace_callback('/\.(\w+)$/', fn ($matches) => "-{$digest}{$matches[0]}", $asset->logicalPath);

return $this->assetsPathResolver->resolvePublicPath($digestedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGettingAssetWorks()
{
$client = static::createClient();

$client->request('GET', '/assets/file1-b3445cb7a86a0795a7af7f2004498aef.css');
$client->request('GET', '/assets/file1-YjM0NDV.css');
$response = $client->getResponse();
$this->assertSame(200, $response->getStatusCode());
$this->assertInstanceOf(BinaryFileResponse::class, $response);
Expand All @@ -39,7 +39,7 @@ public function testGettingAssetWithNonAsciiFilenameWorks()
{
$client = static::createClient();

$client->request('GET', '/assets/voilà-6344422da690fcc471f23f7a8966cd1c.css');
$client->request('GET', '/assets/voilà-NjM0NDQ.css');
$response = $client->getResponse();
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(<<<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function testAssetsAreCompiled()
// match Compiling \d+ assets
$this->assertMatchesRegularExpression('/Compiled \d+ assets/', $tester->getDisplay());

$this->assertFileExists($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js');
$this->assertFileExists($targetBuildDir.'/subdir/file5-ZjRmZGM.js');
$this->assertSame(<<<EOF
import '../file4.js';
console.log('file5.js');

EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js'));
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-ZjRmZGM.js'));

$finder = new Finder();
$finder->in($targetBuildDir)->files();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler;
use Symfony\Component\AssetMapper\Compiler\JavaScriptImportPathCompiler;
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
use Symfony\Component\AssetMapper\Exception\InvalidArgumentException;
use Symfony\Component\AssetMapper\Factory\MappedAssetFactory;
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
use Symfony\Component\AssetMapper\MappedAsset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testDefaultAssetPackageIsDecorated()
{
$packages = $this->kernel->getContainer()->get('public.assets.packages');
\assert($packages instanceof Packages);
$this->assertSame('/assets/file1-b3445cb7a86a0795a7af7f2004498aef.css', $packages->getUrl('file1.css'));
$this->assertSame('/assets/file1-YjM0NDV.css', $packages->getUrl('file1.css'));
$this->assertSame('/non-existent.css', $packages->getUrl('non-existent.css'));
}
}