Skip to content

Commit 99ae397

Browse files
authored
Allow prefetch to happen on custom event (#52574)
1 parent d16ce25 commit 99ae397

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Illuminate/Foundation/Vite.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ class Vite implements Htmlable
111111
*/
112112
protected $prefetchConcurrently = 3;
113113

114+
/**
115+
* The name of the event that should trigger prefetching. The event must be dispatched on the `window`.
116+
*
117+
* @var string
118+
*/
119+
protected $prefetchEvent = 'load';
120+
114121
/**
115122
* Get the preloaded assets.
116123
*
@@ -285,10 +292,13 @@ public function usePreloadTagAttributes($attributes)
285292
* Eagerly prefetch assets.
286293
*
287294
* @param int|null $concurrency
295+
* @param string $event
288296
* @return $this
289297
*/
290-
public function prefetch($concurrency = null)
298+
public function prefetch($concurrency = null, $event = 'load')
291299
{
300+
$this->prefetchEvent = $event;
301+
292302
return $concurrency === null
293303
? $this->usePrefetchStrategy('aggressive')
294304
: $this->usePrefetchStrategy('waterfall', ['concurrency' => $concurrency]);
@@ -486,7 +496,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
486496
'waterfall' => new HtmlString($base.<<<HTML
487497
488498
<script{$this->nonceAttribute()}>
489-
window.addEventListener('load', () => window.setTimeout(() => {
499+
window.addEventListener('{$this->prefetchEvent}', () => window.setTimeout(() => {
490500
const makeLink = (asset) => {
491501
const link = document.createElement('link')
492502
@@ -529,7 +539,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
529539
'aggressive' => new HtmlString($base.<<<HTML
530540
531541
<script{$this->nonceAttribute()}>
532-
window.addEventListener('load', () => window.setTimeout(() => {
542+
window.addEventListener('{$this->prefetchEvent}', () => window.setTimeout(() => {
533543
const makeLink = (asset) => {
534544
const link = document.createElement('link')
535545

tests/Foundation/FoundationViteTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,23 @@ public function testSupportCspNonceInPrefetchScript()
16761676
$this->cleanViteManifest($buildDir);
16771677
}
16781678

1679+
public function testItCanConfigureThePrefetchTriggerEvent()
1680+
{
1681+
$manifest = json_decode(file_get_contents(__DIR__.'/fixtures/prefetching-manifest.json'));
1682+
$buildDir = Str::random();
1683+
$this->makeViteManifest($manifest, $buildDir);
1684+
app()->usePublicPath(__DIR__);
1685+
1686+
$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js']))
1687+
->useBuildDirectory($buildDir)
1688+
->prefetch(event: 'vite:prefetch')
1689+
->toHtml();
1690+
$this->assertStringNotContainsString("window.addEventListener('load', ", $html);
1691+
$this->assertStringContainsString("window.addEventListener('vite:prefetch', ", $html);
1692+
1693+
$this->cleanViteManifest($buildDir);
1694+
}
1695+
16791696
protected function cleanViteManifest($path = 'build')
16801697
{
16811698
if (file_exists(public_path("{$path}/manifest.json"))) {

0 commit comments

Comments
 (0)