8000 [AssetMapper] Avoid loading potentially ALL assets in dev server · symfony/symfony@05fc854 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05fc854

Browse files
committed
[AssetMapper] Avoid loading potentially ALL assets in dev server
1 parent 8fd49c1 commit 05fc854

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,8 +1300,8 @@ private function registerAssetMapperConfiguration(array $config, ContainerBuilde
13001300
$container->removeDefinition('asset_mapper.dev_server_subscriber');
13011301
} else {
13021302
$container->getDefinition('asset_mapper.dev_server_subscriber')
1303-
->setArgument(1, $config['public_prefix'])
1304-
->setArgument(2, $config['extensions']);
1303+
->setArgument(2, $config['public_prefix'])
1304+
->setArgument(3, $config['extensions']);
13051305
}
13061306

13071307
$container->getDefinition('asset_mapper.compiler.css_asset_url_compiler')

src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
->set('asset_mapper.dev_server_subscriber', AssetMapperDevServerSubscriber::class)
8282
->args([
8383
service('asset_mapper'),
84+
service('cache.asset_mapper'),
8485
abstract_arg('asset public prefix'),
8586
abstract_arg('extensions map'),
8687
])

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
->private()
6767
->tag('cache.pool')
6868

69+
->set('cache.asset_mapper')
70+
->parent('cache.system')
71+
->private()
72+
->tag('cache.pool')
73+
6974
->set('cache.messenger.restart_workers_signal')
7075
->parent('cache.app')
7176
->private()

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/XmlFrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testAssetMapper()
8282
$this->assertSame('/assets_path/', $definition->getArgument(1));
8383

8484
$definition = $container->getDefinition('asset_mapper.dev_server_subscriber');
85-
$this->assertSame(['zip' => 'application/zip'], $definition->getArgument(2));
85+
$this->assertSame(['zip' => 'application/zip'], $definition->getArgument(3));
8686

8787
$definition = $container->getDefinition('asset_mapper.importmap.renderer');
8888
$this->assertSame(['data-turbo-track' => 'reload'], $definition->getArgument(3));

src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\AssetMapper;
1313

14+
use Psr\Cache\CacheItemPoolInterface;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpKernel\Event\RequestEvent;
@@ -102,6 +103,7 @@ final class AssetMapperDevServerSubscriber implements EventSubscriberInterface
102103

103104
public function __construct(
104105
private readonly AssetMapperInterface $assetMapper,
106+
private readonly CacheItemPoolInterface $cacheMapCache,
105107
string $publicPrefix = '/assets/',
106108
array $extensionsMap = [],
107109
) {
@@ -120,13 +122,7 @@ public function onKernelRequest(RequestEvent $event): void
120122
return;
121123
}
122124

123-
$asset = null;
124-
foreach ($this->assetMapper->allAssets() as $assetCandidate) {
125-
if ($pathInfo === $assetCandidate->getPublicPath()) {
126-
$asset = $assetCandidate;
127-
break;
128-
}
129-
}
125+
$asset = $this->findAssetFromCache($pathInfo);
130126

131127
if (!$asset) {
132128
throw new NotFoundHttpException(sprintf('Asset with public path "%s" not found.', $pathInfo));
@@ -160,4 +156,32 @@ private function getMediaType(string $path): ?string
160156

161157
return $this->extensionsMap[$extension] ?? null;
162158
}
159+
160+
private function findAssetFromCache(string $pathInfo): ?MappedAsset
161+
{
162+
$cachedAsset = $this->cacheMapCache->getItem(hash('xxh128', $pathInfo));
163+
$asset = $cachedAsset->isHit() ? $this->assetMapper->getAsset($cachedAsset->get()) : null;
164+
165+
if (null !== $asset && $asset->getPublicPath() === $pathInfo) {
166+
return $asset;
167+
}
168+
169+
// we did not find a match
170+
$asset = null;
171+
foreach ($this->assetMapper->allAssets() as $assetCandidate) {
172+
if ($pathInfo === $assetCandidate->getPublicPath()) {
173+
$asset = $assetCandidate;
174+
break;
175+
}
176+
}
177+
178+
if (null === $asset) {
179+
return null;
180+
}
181+
182+
$cachedAsset->set($asset->getLogicalPath());
183+
$this->cacheMapCache->save($cachedAsset);
184+
185+
return $asset;
186+
}
163187
}

0 commit comments

Comments
 (0)
0