From a3169c8aafcb49a2b30a8dab367cafa607454fd5 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 22 Aug 2024 09:13:19 +1000 Subject: [PATCH] Unify prefetch API --- src/Illuminate/Foundation/Vite.php | 14 +++++++++++++- tests/Foundation/FoundationViteTest.php | 14 +++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 747da7b14d08..dcd89477b9fa 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -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', [ diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 8f19c8e7d379..94b7332d947e 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -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'], @@ -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'], @@ -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'], @@ -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'], @@ -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'], @@ -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, @@ -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'],