Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow prefetch to start on custom event #52574

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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