Skip to content

Commit 401f026

Browse files
authored
Unify prefetch API (#52550)
1 parent 326d805 commit 401f026

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Illuminate/Foundation/Vite.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,23 @@ public function usePreloadTagAttributes($attributes)
282282
}
283283

284284
/**
285-
* Use the "waterfall" prefetching strategy.
285+
* Eagerly prefetch assets.
286286
*
287287
* @param int|null $concurrency
288288
* @return $this
289289
*/
290+
public function prefetch($concurrency = null)
291+
{
292+
return $concurrency === null
293+
? $this->usePrefetchStrategy('aggressive')
294+
: $this->usePrefetchStrategy('waterfall', ['concurrency' => $concurrency]);
295+
}
296+
297+
/**
298+
* Use the "waterfall" prefetching strategy.
299+
*
300+
* @return $this
301+
*/
290302
public function useWaterfallPrefetching(?int $concurrency = null)
291303
{
292304
return $this->usePrefetchStrategy('waterfall', [

tests/Foundation/FoundationViteTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ public function testItCanPrefetchEntrypoint()
13051305
$this->makeViteManifest($manifest, $buildDir);
13061306
app()->usePublicPath(__DIR__);
13071307

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)