Skip to content

Commit dbc29fa

Browse files
committed
Reset caches on FINISH_REQUEST.
Fixes #90, #94
1 parent 395b60a commit dbc29fa

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
2121
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
2222
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
23+
use Symfony\WebpackEncoreBundle\EventListener\ResetAssetsEventListener;
2324

2425
final class WebpackEncoreExtension extends Extension
2526
{
@@ -57,6 +58,9 @@ public function load(array $configs, ContainerBuilder $container)
5758

5859
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
5960
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
61+
62+
$container->getDefinition(ResetAssetsEventListener::class)
63+
->setArgument(1, array_keys($factories));
6064
if (false !== $config['output_path']) {
6165
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
6266
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\WebpackEncoreBundle\EventListener;
6+
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use Symfony\Component\HttpKernel\KernelEvents;
9+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
10+
11+
class ResetAssetsEventListener implements EventSubscriberInterface
12+
{
13+
private $entrypointLookupCollection;
14+
private $buildNames;
15+
16+
public function __construct(EntrypointLookupCollection $entrypointLookupCollection, array $buildNames)
17+
{
18+
$this->entrypointLookupCollection = $entrypointLookupCollection;
19+
$this->buildNames = $buildNames;
20+
}
21+
22+
public static function getSubscribedEvents()
23+
{
24+
return [
25+
KernelEvents::FINISH_REQUEST => 'resetAssets',
26+
];
27+
}
28+
29+
public function resetAssets()
30+
{
31+
foreach ($this->buildNames as $name) {
32+
$this->entrypointLookupCollection->getEntrypointLookup($name)->reset();
33+
}
34+
}
35+
}

src/Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,10 @@
6767
<tag name="kernel.event_subscriber" />
6868
<argument type="service" id="webpack_encore.tag_renderer" />
6969
</service>
70+
71+
<service id="Symfony\WebpackEncoreBundle\EventListener\ResetAssetsEventListener">
72+
<tag name="kernel.event_subscriber" />
73+
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
74+
</service>
7075
</services>
7176
</container>

tests/IntegrationTest.php

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Reference;
2121
use Symfony\Component\HttpFoundation\Request;
2222
use Symfony\Component\HttpFoundation\Response;
23+
use Symfony\Component\HttpKernel\HttpKernelInterface;
2324
use Symfony\Component\HttpKernel\Kernel;
2425
use Symfony\Component\HttpKernel\Log\Logger;
2526
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
@@ -30,6 +31,7 @@
3031
use Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer;
3132
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
3233
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
34+
use Twig\Environment;
3335

3436
class IntegrationTest extends TestCase
3537
{
@@ -100,6 +102,27 @@ public function testEntriesAreNotRepeatedWhenAlreadyOutputIntegration()
100102
);
101103
}
102104

105+
public function testEntriesExistsWhenDoingSubRequestIntegration()
106+
{
107+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
108+
$kernel->boot();
109+
110+
$request = Request::create('/render-sub-requests');
111+
$request->attributes->set('template', '@integration_test/template.twig');
112+
$response = $kernel->handle($request);
113+
114+
$html = $response->getContent();
115+
116+
$containsCount0 = substr_count($html, '<script src="/build/file1.js"');
117+
$this->assertSame(2, $containsCount0);
118+
119+
$containsCount1 = substr_count($html, '<link rel="stylesheet" href="/build/styles3.css"');
120+
$this->assertSame(2, $containsCount1);
121+
122+
$containsCount2 = substr_count($html, '<link rel="stylesheet" href="/build/styles4.css"');
123+
$this->assertSame(2, $containsCount2);
124+
}
125+
103126
public function testCacheWarmer()
104127
{
105128
$kernel = new WebpackEncoreIntegrationTestKernel(true);
@@ -390,24 +413,43 @@ public function renderFoo()
390413
{
391414
return new Response('I am a page!');
392415
}
416+
417+
public function renderSubRequests(Request $request, HttpKernelInterface $httpKernel)
418+
{
419+
$subRequest = Request::create('/render');
420+
$subRequest->attributes->set('template', $request->attributes->get('template'));
421+
422+
$response0 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
423+
$response1 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
424+
425+
return new Response($response0->getContent() . $response1->getContent());
426+
}
427+
428+
public function renderTwig(Environment $twig, Request $request)
429+
{
430+
return new Response($twig->render($request->attributes->get('template')));
431+
}
393432
}
394433

395-
if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) {
396-
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
397-
protected function configureRouting(RoutingConfigurator $routes): void
398-
{
399-
$routes->add('/foo', 'kernel::renderFoo');
400-
}
434+
435+
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel
436+
{
437+
protected function configureRouting(RoutingConfigurator $routes): void
438+
{
439+
$routes->add('/foo', 'kernel::renderFoo');
440+
$routes->add('/render', 'kernel::renderTwig');
441+
$routes->add('/render-sub-requests', 'kernel::renderSubRequests');
401442
}
402-
} else {
403-
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
404-
protected function configureRoutes(RouteCollectionBuilder $routes)
405-
{
406-
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
407-
}
443+
444+
protected function configureRoutes(RouteCollectionBuilder $routes)
445+
{
446+
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
447+
$routes->add('/render', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderTwig');
448+
$routes->add('/render-sub-requests', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderSubRequests');
408449
}
409450
}
410451

452+
411453
class WebpackEncoreCacheWarmerTester
412454
{
413455
private $entrypointCacheWarmer;

0 commit comments

Comments
 (0)