Skip to content

Commit a1ce87e

Browse files
committed
bug #172 Fixing reset assets trigger on sub-requests (TarikAmine)
This PR was squashed before being merged into the main branch. Discussion ---------- Fixing reset assets trigger on sub-requests `1.14.0` is broken when a page contains sub requests, all the files managed by the package run twice. The event `KernelEvents::FINISH_REQUEST` should be handled for MainRequest only. Sadly we are stuck at 1.13.2 until it's fixed Commits ------- 2dd0ffc Fixing reset assets trigger on sub-requests
2 parents 1729c31 + 2dd0ffc commit a1ce87e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/EventListener/ResetAssetsEventListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\WebpackEncoreBundle\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1516
use Symfony\Component\HttpKernel\KernelEvents;
1617
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
1718

@@ -33,8 +34,14 @@ public static function getSubscribedEvents()
3334
];
3435
}
3536

36-
public function resetAssets()
37+
public function resetAssets(FinishRequestEvent $event)
3738
{
39+
// Handle deprecated `KernelEvent::isMasterRequest() - Can be removed when Symfony < 5.3 support is dropped.
40+
$mainRequestMethod = method_exists($event, 'isMainRequest') ? 'isMainRequest' : 'isMasterRequest';
41+
42+
if (!$event->$mainRequestMethod()) {
43+
return;
44+
}
3845
foreach ($this->buildNames as $name) {
3946
$this->entrypointLookupCollection->getEntrypointLookup($name)->reset();
4047
}

tests/IntegrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ public function testEntriesExistsWhenDoingSubRequestIntegration()
114114
$html = $response->getContent();
115115

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

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

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

126126
public function testCacheWarmer()

0 commit comments

Comments
 (0)