Skip to content

Commit

Permalink
Allow prefetch to happen on custom event (#52574)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Aug 26, 2024
1 parent d16ce25 commit 99ae397
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ class Vite implements Htmlable
*/
protected $prefetchConcurrently = 3;

/**
* The name of the event that should trigger prefetching. The event must be dispatched on the `window`.
*
* @var string
*/
protected $prefetchEvent = 'load';

/**
* Get the preloaded assets.
*
Expand Down Expand Up @@ -285,10 +292,13 @@ public function usePreloadTagAttributes($attributes)
* Eagerly prefetch assets.
*
* @param int|null $concurrency
* @param string $event
* @return $this
*/
public function prefetch($concurrency = null)
public function prefetch($concurrency = null, $event = 'load')
{
$this->prefetchEvent = $event;

return $concurrency === null
? $this->usePrefetchStrategy('aggressive')
: $this->usePrefetchStrategy('waterfall', ['concurrency' => $concurrency]);
Expand Down Expand Up @@ -486,7 +496,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
'waterfall' => new HtmlString($base.<<<HTML
<script{$this->nonceAttribute()}>
window.addEventListener('load', () => window.setTimeout(() => {
window.addEventListener('{$this->prefetchEvent}', () => window.setTimeout(() => {
const makeLink = (asset) => {
const link = document.createElement('link')
Expand Down Expand Up @@ -529,7 +539,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
'aggressive' => new HtmlString($base.<<<HTML
<script{$this->nonceAttribute()}>
window.addEventListener('load', () => window.setTimeout(() => {
window.addEventListener('{$this->prefetchEvent}', () => window.setTimeout(() => {
const makeLink = (asset) => {
const link = document.createElement('link')
Expand Down
17 changes: 17 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,23 @@ public function testSupportCspNonceInPrefetchScript()
$this->cleanViteManifest($buildDir);
}

public function testItCanConfigureThePrefetchTriggerEvent()
{
$manifest = json_decode(file_get_contents(__DIR__.'/fixtures/prefetching-manifest.json'));
$buildDir = Str::random();
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js']))
->useBuildDirectory($buildDir)
->prefetch(event: 'vite:prefetch')
->toHtml();
$this->assertStringNotContainsString("window.addEventListener('load', ", $html);
$this->assertStringContainsString("window.addEventListener('vite:prefetch', ", $html);

$this->cleanViteManifest($buildDir);
}

protected function cleanViteManifest($path = 'build')
{
if (file_exists(public_path("{$path}/manifest.json"))) {
Expand Down

0 comments on commit 99ae397

Please sign in to comment.