Skip to content

Commit

Permalink
Unify prefetch API
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Aug 21, 2024
1 parent 326d805 commit a3169c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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]);
}

/**
* 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

0 comments on commit a3169c8

Please sign in to comment.