@@ -111,6 +111,13 @@ class Vite implements Htmlable
111
111
*/
112
112
protected $ prefetchConcurrently = 3 ;
113
113
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
+
114
121
/**
115
122
* Get the preloaded assets.
116
123
*
@@ -285,10 +292,13 @@ public function usePreloadTagAttributes($attributes)
285
292
* Eagerly prefetch assets.
286
293
*
287
294
* @param int|null $concurrency
295
+ * @param string $event
288
296
* @return $this
289
297
*/
290
- public function prefetch ($ concurrency = null )
298
+ public function prefetch ($ concurrency = null , $ event = ' load ' )
291
299
{
300
+ $ this ->prefetchEvent = $ event ;
301
+
292
302
return $ concurrency === null
293
303
? $ this ->usePrefetchStrategy ('aggressive ' )
294
304
: $ this ->usePrefetchStrategy ('waterfall ' , ['concurrency ' => $ concurrency ]);
@@ -486,7 +496,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
486
496
'waterfall ' => new HtmlString ($ base .<<<HTML
487
497
488
498
<script {$ this ->nonceAttribute ()}>
489
- window.addEventListener('load ', () => window.setTimeout(() => {
499
+ window.addEventListener(' { $ this -> prefetchEvent } ', () => window.setTimeout(() => {
490
500
const makeLink = (asset) => {
491
501
const link = document.createElement('link')
492
502
@@ -529,7 +539,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
529
539
'aggressive ' => new HtmlString ($ base .<<<HTML
530
540
531
541
<script {$ this ->nonceAttribute ()}>
532
- window.addEventListener('load ', () => window.setTimeout(() => {
542
+ window.addEventListener(' { $ this -> prefetchEvent } ', () => window.setTimeout(() => {
533
543
const makeLink = (asset) => {
534
544
const link = document.createElement('link')
535
545
0 commit comments