8000 bug #52100 [AssetMapper] Disable profiler when the "dev server" respo… · symfony/symfony@9233ea8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9233ea8

Browse files
bug #52100 [AssetMapper] Disable profiler when the "dev server" respond (smnandre)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [AssetMapper] Disable profiler when the "dev server" respond | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT In debug mode, the "dev server" send assets as if they were compiled in the public directory. To improve the DX and not "pollute" too much the profiler (see capture below), this PR does two things : * disable the profiler for those requests * stop the request & response event propagation, to speed things a bit <img width="800" alt="Capture d’écran 2023-10-17 à 10 57 44" src="https://github.com/symfony/symfony/assets/1359581/98eff6c5-2da4-46e3-9e7e-44b34ec38eef"> Commits ------- e1174ac [AssetMapper] Disable profiler when the "dev server" respond
2 parents b9122bc + e1174ac commit 9233ea8

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use Symfony\Component\AssetMapper\ImportMap\Resolver\JsDelivrEsmResolver;
4040
use Symfony\Component\AssetMapper\MapperAwareAssetPackage;
4141
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolver;
42-
use Symfony\Component\HttpKernel\Event\RequestEvent;
4342

4443
return static function (ContainerConfigurator $container) {
4544
$container->services()
@@ -93,8 +92,9 @@
9392
abstract_arg('asset public prefix'),
9493
abstract_arg('extensions map'),
9594
service('cache.asset_mapper'),
95+
service('profiler')->nullOnInvalid(),
9696
])
97-
->tag('kernel.event_subscriber', ['event' => RequestEvent::class])
97+
->tag('kernel.event_subscriber')
9898

9999
->set('asset_mapper.command.compile', AssetMapperCompileCommand::class)
100100
->args([

src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Event\RequestEvent;
18+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
1819
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1920
use Symfony\Component\HttpKernel\KernelEvents;
21+
use Symfony\Component\HttpKernel\Profiler\Profiler;
2022

2123
/**
2224
* Functions like a controller that returns assets from the asset mapper.
@@ -104,6 +106,7 @@ public function __construct(
104106
string $publicPrefix = '/assets/',
105107
array $extensionsMap = [],
106108
private readonly ?CacheItemPoolInterface $cacheMapCache = null,
109+
private readonly ?Profiler $profiler = null,
107110
) {
108111
$this->publicPrefix = rtrim($publicPrefix, '/').'/';
109112
$this->extensionsMap = array_merge(self::EXTENSIONS_MAP, $extensionsMap);
@@ -126,6 +129,8 @@ public function onKernelRequest(RequestEvent $event): void
126129
throw new NotFoundHttpException(sprintf('Asset with public path "%s" not found.', $pathInfo));
127130
}
128131

132+
$this->profiler?->disable();
133+
129134
$mediaType = $this->getMediaType($asset->publicPath);
130135
$response = (new Response(
131136
$asset->content,
@@ -137,14 +142,26 @@ public function onKernelRequest(RequestEvent $event): void
137142
->setEtag($asset->digest)
138143
;
139144

145+
$response->headers->set('X-Assets-Dev', true);
146+
140147
$event->setResponse($response);
148+
$event->stopPropagation();
149+
}
150+
151+
public function onKernelResponse(ResponseEvent $event): void
152+
{
153+
if ($event->getResponse()->headers->get('X-Assets-Dev')) {
154+
$event->stopPropagation();
155+
}
141156
}
142157

143158
public static function getSubscribedEvents(): array
144159
{
145160
return [
146161
// priority higher than RouterListener
147162
KernelEvents::REQUEST => [['onKernelRequest', 35]],
163+
// Highest priority possible to bypass all other listeners
164+
KernelEvents::RESPONSE => [['onKernelResponse', 2048]],
148165
];
149166
}
150167

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testGettingAssetWorks()
3030
EOF, $response->getContent());
3131
$this->assertSame('"b3445cb7a86a0795a7af7f2004498aef"', $response->headers->get('ETag'));
3232
$this->assertSame('immutable, max-age=604800, public', $response->headers->get('Cache-Control'));
33+
$this->assertTrue($response->headers->has('X-Assets-Dev'));
3334
}
3435

3536
public function test404OnUnknownAsset()
@@ -39,6 +40,7 @@ public function test404OnUnknownAsset()
3940
$client->request('GET', '/assets/unknown.css');
4041
$response = $client->getResponse();
4142
$this->assertSame(404, $response->getStatusCode());
43+
$this->assertFalse($response->headers->has('X-Assets-Dev'));
4244
}
4345

4446
public function test404OnInvalidDigest()

src/Symfony/Component/AssetMapper/Tests/ImportMap/JavaScriptImportTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\AssetMapper\Tests\ImportMap;
413

514
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)
0