Skip to content

Commit 0862610

Browse files
committed
feature symfony#57879 [AssetMapper] Truncate public digests to 8 characters (smnandre)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [AssetMapper] Truncate public digests to 8 characters | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #... | License | MIT ⚠️ 2024-08-03 Update: **Changes Made Based on Feedback** This pull request (PR) now shortens the public digest of mapped assets to **8 characters**. The digest is used in the filenames of deployed assets, leading to a tangible reduction in HTML size, particularly in the generated import map. -- **Original message:** I've been wanting to use shorter digests in my mapped assets / importmap URL's. This PR adds an hashAlgorithm argument to the MappedAssetFactory to do so. I'm not sure this is enough/good choice, as we need some requirement regarding the digests (7+ caracters, alnum only) But adding an entire AssetHasherInterface would be probably overkill, so i'm _**very open**_ for feedback / ideas there. (I did not put FrameworkBundle modifications in this PR, as the discussion will probably have an impact on what's to do) Commits ------- e1ae985 [AssetMapper] Truncate public digests to 8 characters
2 parents 6d6dd4a + e1ae985 commit 0862610

File tree

6 files changed

+14
-9
lines changed

6 files changed

+14
-9
lines changed

src/Symfony/Component/AssetMapper/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Shorten the public digest of mapped assets to 7 characters
8+
49
7.1
510
---
611

src/Symfony/Component/AssetMapper/Factory/MappedAssetFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
class MappedAssetFactory implements MappedAssetFactoryInterface
2525
{
2626
private const PREDIGESTED_REGEX = '/-([0-9a-zA-Z]{7,128}\.digested)/';
27+
private const PUBLIC_DIGEST_LENGTH = 7;
2728

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

92-
return [
93-
hash_file('xxh128', $asset->sourcePath),
94-
false,
95-
];
93+
return [hash_file('xxh128', $asset->sourcePath), false];
9694
}
9795

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

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

124123
return $this->assetsPathResolver->resolvePublicPath($digestedPath);

src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testGettingAssetWorks()
2121
{
2222
$client = static::createClient();
2323

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

42-
$client->request('GET', '/assets/voilà-6344422da690fcc471f23f7a8966cd1c.css');
42+
$client->request('GET', '/assets/voilà-NjM0NDQ.css');
4343
$response = $client->getResponse();
4444
$this->assertSame(200, $response->getStatusCode());
4545
$this->assertSame(<<<EOF

src/Symfony/Component/AssetMapper/Tests/Command/AssetMapperCompileCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function testAssetsAreCompiled()
5959
// match Compiling \d+ assets
6060
$this->assertMatchesRegularExpression('/Compiled \d+ assets/', $tester->getDisplay());
6161

62-
$this->assertFileExists($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js');
62+
$this->assertFileExists($targetBuildDir.'/subdir/file5-ZjRmZGM.js');
6363
$this->assertSame(<<<EOF
6464
import '../file4.js';
6565
console.log('file5.js');
6666
67-
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js'));
67+
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-ZjRmZGM.js'));
6868

6969
$finder = new Finder();
7070
$finder->in($targetBuildDir)->files();

src/Symfony/Component/AssetMapper/Tests/Factory/MappedAssetFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler;
2020
use Symfony\Component\AssetMapper\Compiler\JavaScriptImportPathCompiler;
2121
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
22+
use Symfony\Component\AssetMapper\Exception\InvalidArgumentException;
2223
use Symfony\Component\AssetMapper\Factory\MappedAssetFactory;
2324
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
2425
use Symfony\Component\AssetMapper\MappedAsset;

src/Symfony/Component/AssetMapper/Tests/MapperAwareAssetPackageIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testDefaultAssetPackageIsDecorated()
3737
{
3838
$packages = $this->kernel->getContainer()->get('public.assets.packages');
3939
\assert($packages instanceof Packages);
40-
$this->assertSame('/assets/file1-b3445cb7a86a0795a7af7f2004498aef.css', $packages->getUrl('file1.css'));
40+
$this->assertSame('/assets/file1-YjM0NDV.css', $packages->getUrl('file1.css'));
4141
$this->assertSame('/non-existent.css', $packages->getUrl('non-existent.css'));
4242
}
4343
}

0 commit comments

Comments
 (0)