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] Unify prefetch API #52550

Merged
merged 1 commit into from
Aug 22, 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
14 changes: 13 additions & 1 deletion src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,23 @@ public function usePreloadTagAttributes($attributes)
}

/**
* Use the "waterfall" prefetching strategy.
* Eagerly prefetch assets.
*
* @param int|null $concurrency
* @return $this
*/
public function prefetch($concurrency = null)
{
return $concurrency === null
? $this->usePrefetchStrategy('aggressive')
: $this->usePrefetchStrategy('waterfall', ['concurrency' => $concurrency]);
Comment on lines +293 to +294

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
? $this->usePrefetchStrategy('aggressive')
: $this->usePrefetchStrategy('waterfall', ['concurrency' => $concurrency]);
? $this->useAggressivePrefetching()
: $this->useWaterfallPrefetching($concurrency);

While this works, it would've been a good idea to use these methods the class already provides.

}

/**
* Use the "waterfall" prefetching strategy.
*
* @return $this
*/
public function useWaterfallPrefetching(?int $concurrency = null)
{
return $this->usePrefetchStrategy('waterfall', [
Expand Down
14 changes: 7 additions & 7 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ public function testItCanPrefetchEntrypoint()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->usePrefetchStrategy('waterfall')->toHtml();
$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->prefetch(concurrency: 3)->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'fetchpriority' => 'low'],
Expand Down Expand Up @@ -1381,7 +1381,7 @@ public function testItHandlesSpecifyingPageWithAppJs()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js', 'resources/js/Pages/Auth/Login.vue'])->useBuildDirectory($buildDir)->usePrefetchStrategy('waterfall')->toHtml();
$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js', 'resources/js/Pages/Auth/Login.vue'])->useBuildDirectory($buildDir)->prefetch(concurrency: 3)->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'fetchpriority' => 'low'],
Expand Down Expand Up @@ -1411,7 +1411,7 @@ public function testItCanSpecifyWaterfallChunks()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->useWaterfallPrefetching(concurrency: 10)->toHtml();
$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->prefetch(concurrency: 10)->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'fetchpriority' => 'low'],
Expand Down Expand Up @@ -1447,7 +1447,7 @@ public function testItCanPrefetchAggressively()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->useAggressivePrefetching()->toHtml();
$html = (string) ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->prefetch()->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'fetchpriority' => 'low'],
Expand Down Expand Up @@ -1501,7 +1501,7 @@ public function testAddsAttributesToPrefetchTags()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->usePrefetchStrategy('waterfall'))->useCspNonce('abc123')->toHtml();
$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js'])->useBuildDirectory($buildDir)->prefetch(concurrency: 3))->useCspNonce('abc123')->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'nonce' => 'abc123', 'fetchpriority' => 'low'],
Expand Down Expand Up @@ -1537,7 +1537,7 @@ public function testItNormalisesAttributes()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js']))->useBuildDirectory($buildDir)->usePrefetchStrategy('waterfall')->usePreloadTagAttributes([
$html = (string) tap(ViteFacade::withEntryPoints(['resources/js/app.js']))->useBuildDirectory($buildDir)->prefetch(concurrency: 3)->usePreloadTagAttributes([
'key' => 'value',
'key-only',
'true-value' => true,
Expand Down Expand Up @@ -1580,7 +1580,7 @@ public function testItPrefetchesCss()
$this->makeViteManifest($manifest, $buildDir);
app()->usePublicPath(__DIR__);

$html = (string) ViteFacade::withEntryPoints(['resources/js/admin.js'])->useBuildDirectory($buildDir)->usePrefetchStrategy('waterfall')->toHtml();
$html = (string) ViteFacade::withEntryPoints(['resources/js/admin.js'])->useBuildDirectory($buildDir)->prefetch(concurrency: 3)->toHtml();

$expectedAssets = Js::from([
['rel' => 'prefetch', 'href' => "https://example.com/{$buildDir}/assets/ConfirmPassword-CDwcgU8E.js", 'fetchpriority' => 'low'],
Expand Down