Skip to content

Commit 7142b68

Browse files
committed
bug #115 Reset assets on FINISH_REQUEST. (Warxcell)
This PR was merged into the main branch. Discussion ---------- Reset assets on FINISH_REQUEST. Fixes #90, #94 Commits ------- 029b01b Reset caches on FINISH_REQUEST. Fixes #90, #94
2 parents b6827c0 + 029b01b commit 7142b68

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
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): void
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
@@ -66,5 +66,10 @@
6666
<tag name="kernel.event_subscriber" />
6767
<argument type="service" id="webpack_encore.tag_renderer" />
6868
</service>
69+
70+
<service id="Symfony\WebpackEncoreBundle\EventListener\ResetAssetsEventListener">
71+
<tag name="kernel.event_subscriber" />
72+
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
73+
</service>
6974
</services>
7075
</container>

tests/IntegrationTest.php

Lines changed: 45 additions & 1 deletion
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);
@@ -524,6 +547,22 @@ public function renderFoo()
524547
{
525548
return new Response('I am a page!');
526549
}
550+
551+
public function renderSubRequests(Request $request, HttpKernelInterface $httpKernel)
552+
{
553+
$subRequest = Request::create('/render');
554+
$subRequest->attributes->set('template', $request->attributes->get('template'));
555+
556+
$response0 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
557+
$response1 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
558+
559+
return new Response($response0->getContent() . $response1->getContent());
560+
}
561+
562+
public function renderTwig(Environment $twig, Request $request)
563+
{
564+
return new Response($twig->render($request->attributes->get('template')));
565+
}
527566
}
528567

529568
if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) {
@@ -532,18 +571,23 @@ class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegratio
532571
protected function configureRouting(RoutingConfigurator $routes): void
533572
{
534573
$routes->add('/foo', 'kernel::renderFoo');
574+
$routes->add('/render', 'kernel::renderTwig');
575+
$routes->add('/render-sub-requests', 'kernel::renderSubRequests');
535576
}
536577
}
537578
} else {
538579
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel
539580
{
540581
protected function configureRoutes(RouteCollectionBuilder $routes)
541582
{
542-
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
583+
$routes->add('/foo', 'kernel:::renderFoo');
584+
$routes->add('/render', 'kernel::renderTwig');
585+
$routes->add('/render-sub-requests', 'kernel::renderSubRequests');
543586
}
544587
}
545588
}
546589

590+
547591
class WebpackEncoreCacheWarmerTester
548592
{
549593
private $entrypointCacheWarmer;

0 commit comments

Comments
 (0)