Skip to content

Commit

Permalink
bug #115 Reset assets on FINISH_REQUEST. (Warxcell)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weaverryan committed Feb 10, 2022
2 parents b6827c0 + 029b01b commit 7142b68
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/DependencyInjection/WebpackEncoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Symfony\WebpackEncoreBundle\EventListener\ResetAssetsEventListener;

final class WebpackEncoreExtension extends Extension
{
Expand Down Expand Up @@ -57,6 +58,9 @@ public function load(array $configs, ContainerBuilder $container): void

$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));

$container->getDefinition(ResetAssetsEventListener::class)
->setArgument(1, array_keys($factories));
if (false !== $config['output_path']) {
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
}
Expand Down
35 changes: 35 additions & 0 deletions src/EventListener/ResetAssetsEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Symfony\WebpackEncoreBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;

class ResetAssetsEventListener implements EventSubscriberInterface
{
private $entrypointLookupCollection;
private $buildNames;

public function __construct(EntrypointLookupCollection $entrypointLookupCollection, array $buildNames)
{
$this->entrypointLookupCollection = $entrypointLookupCollection;
$this->buildNames = $buildNames;
}

public static function getSubscribedEvents()
{
return [
KernelEvents::FINISH_REQUEST => 'resetAssets',
];
}

public function resetAssets()
{
foreach ($this->buildNames as $name) {
$this->entrypointLookupCollection->getEntrypointLookup($name)->reset();
}
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@
<tag name="kernel.event_subscriber" />
<argument type="service" id="webpack_encore.tag_renderer" />
</service>

<service id="Symfony\WebpackEncoreBundle\EventListener\ResetAssetsEventListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
</service>
</services>
</container>
46 changes: 45 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Log\Logger;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
Expand All @@ -30,6 +31,7 @@
use Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer;
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
use Twig\Environment;

class IntegrationTest extends TestCase
{
Expand Down Expand Up @@ -100,6 +102,27 @@ public function testEntriesAreNotRepeatedWhenAlreadyOutputIntegration()
);
}

public function testEntriesExistsWhenDoingSubRequestIntegration()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();

$request = Request::create('/render-sub-requests');
$request->attributes->set('template', '@integration_test/template.twig');
$response = $kernel->handle($request);

$html = $response->getContent();

$containsCount0 = substr_count($html, '<script src="/build/file1.js"');
$this->assertSame(2, $containsCount0);

$containsCount1 = substr_count($html, '<link rel="stylesheet" href="/build/styles3.css"');
$this->assertSame(2, $containsCount1);

$containsCount2 = substr_count($html, '<link rel="stylesheet" href="/build/styles4.css"');
$this->assertSame(2, $containsCount2);
}

public function testCacheWarmer()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
Expand Down Expand Up @@ -524,6 +547,22 @@ public function renderFoo()
{
return new Response('I am a page!');
}

public function renderSubRequests(Request $request, HttpKernelInterface $httpKernel)
{
$subRequest = Request::create('/render');
$subRequest->attributes->set('template', $request->attributes->get('template'));

$response0 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
$response1 = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);

return new Response($response0->getContent() . $response1->getContent());
}

public function renderTwig(Environment $twig, Request $request)
{
return new Response($twig->render($request->attributes->get('template')));
}
}

if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) {
Expand All @@ -532,18 +571,23 @@ class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegratio
protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('/foo', 'kernel::renderFoo');
$routes->add('/render', 'kernel::renderTwig');
$routes->add('/render-sub-requests', 'kernel::renderSubRequests');
}
}
} else {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel
{
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
$routes->add('/foo', 'kernel:::renderFoo');
$routes->add('/render', 'kernel::renderTwig');
$routes->add('/render-sub-requests', 'kernel::renderSubRequests');
}
}
}


class WebpackEncoreCacheWarmerTester
{
private $entrypointCacheWarmer;
Expand Down

0 comments on commit 7142b68

Please sign in to comment.