From 890288cc352e0239c799f359087aed768d4426f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= <30424139+wtto00@users.noreply.github.com> Date: Mon, 25 Dec 2023 01:13:28 +0800 Subject: [PATCH 1/9] [enhance:prefetch] add global ignoreSlowConnection and add none to defaultStrategy enum --- .../prefetch/src/pages/prefetch-none.astro | 1 + packages/astro/e2e/prefetch.test.js | 95 +++++++++++++++++++ packages/astro/src/@types/astro.ts | 26 ++++- packages/astro/src/core/config/schema.ts | 3 +- packages/astro/src/prefetch/index.ts | 39 ++++++-- .../src/prefetch/vite-plugin-prefetch.ts | 6 +- 6 files changed, 159 insertions(+), 11 deletions(-) create mode 100644 packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro new file mode 100644 index 000000000000..493fd2aba727 --- /dev/null +++ b/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro @@ -0,0 +1 @@ +

Prefetch none

diff --git a/packages/astro/e2e/prefetch.test.js b/packages/astro/e2e/prefetch.test.js index a19c87680eca..fafe136bd014 100644 --- a/packages/astro/e2e/prefetch.test.js +++ b/packages/astro/e2e/prefetch.test.js @@ -95,6 +95,12 @@ test.describe('Prefetch (default)', () => { await page.locator('#prefetch-manual').click(); expect(reqUrls.filter((u) => u.includes('/prefetch-manual')).length).toEqual(1); }); + + test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).toContainEqual('/prefetch-none'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + }); }); test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { @@ -182,4 +188,93 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { expect(reqUrls).toContainEqual('/prefetch-viewport'); expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); }); + + test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).toContainEqual('/prefetch-none'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + }); +}); + +test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { + let devServer; + /** @type {string[]} */ + const reqUrls = []; + + test.beforeAll(async ({ astro }) => { + devServer = await astro.startDevServer({ + prefetch: { + prefetchAll: true, + defaultStrategy: 'none', + }, + }); + }); + + test.beforeEach(async ({ page }) => { + page.on('request', (req) => { + const urlObj = new URL(req.url()); + reqUrls.push(urlObj.pathname + urlObj.search); + }); + }); + + test.afterEach(() => { + reqUrls.length = 0; + }); + + test.afterAll(async () => { + await devServer.stop(); + }); + + test('Link without data-astro-prefetch should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).toContainEqual('/prefetch-default'); + }); + + test('data-astro-prefetch="false" should not prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/prefetch-false'); + }); + + test('Link with search param should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).toContainEqual('/?search-param=true'); + }); + + test('data-astro-prefetch="tap" should prefetch on tap', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/prefetch-tap'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-tap').click(), + ]); + expect(reqUrls).toContainEqual('/prefetch-tap'); + }); + + test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/prefetch-hover'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-hover').hover(), + ]); + expect(reqUrls).toContainEqual('/prefetch-hover'); + }); + + test('data-astro-prefetch="viewport" should prefetch on viewport', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/prefetch-viewport'); + // Scroll down to show the element + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-viewport').scrollIntoViewIfNeeded(), + ]); + expect(reqUrls).toContainEqual('/prefetch-viewport'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); + }); + + test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).toContainEqual('/prefetch-none'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + }); }); diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 2893a20eab23..e32a30088023 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -947,7 +947,7 @@ export interface AstroUserConfig { /** * @docs * @name prefetch.defaultStrategy - * @type {'tap' | 'hover' | 'viewport'} + * @type {'tap' | 'hover' | 'viewport' | 'none'} * @default `'hover'` * @description * The default prefetch strategy to use when the `data-astro-prefetch` attribute is set on a link with no value. @@ -955,6 +955,7 @@ export interface AstroUserConfig { * - `'tap'`: Prefetch just before you click on the link. * - `'hover'`: Prefetch when you hover over or focus on the link. (default) * - `'viewport'`: Prefetch as the links enter the viewport. + * - `'none'`: Prefetch the link without any restrictions. * * You can override this default value and select a different strategy for any individual link by setting a value on the attribute. * @@ -962,7 +963,28 @@ export interface AstroUserConfig { * About * ``` */ - defaultStrategy?: 'tap' | 'hover' | 'viewport'; + defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'none'; + + /** + * @docs + * @name prefetch.ignoreSlowConnection + * @type {boolean} + * @description + * Ignore slow connection detection. + * + * ```js + * prefetch: { + * ignoreSlowConnection: true + * } + * ``` + * + * When set to `true`, you can enable slow connection detection by adding `{ ignoreSlowConnection: false }` to the parameters of `prefetch` manually . + * + * ```js + * prefetch('/about', { ignoreSlowConnection: false }); + *``` + */ + ignoreSlowConnection?: boolean; }; /** diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 08910720a13e..358241e23291 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -182,7 +182,8 @@ export const AstroConfigSchema = z.object({ z.boolean(), z.object({ prefetchAll: z.boolean().optional(), - defaultStrategy: z.enum(['tap', 'hover', 'viewport']).optional(), + defaultStrategy: z.enum(['tap', 'hover', 'viewport', 'none']).optional(), + ignoreSlowConnection: z.boolean().optional(), }), ]) .optional(), diff --git a/packages/astro/src/prefetch/index.ts b/packages/astro/src/prefetch/index.ts index 15f4ef0ccd95..3048d11ee25b 100644 --- a/packages/astro/src/prefetch/index.ts +++ b/packages/astro/src/prefetch/index.ts @@ -16,10 +16,13 @@ const listenedAnchors = new WeakSet(); let prefetchAll: boolean = __PREFETCH_PREFETCH_ALL__; // @ts-expect-error injected global let defaultStrategy: string = __PREFETCH_DEFAULT_STRATEGY__; +// @ts-expect-error injected global +let ignoreSlowConnection: boolean = __PREFETCH_IGNORE_SLOW_CONNECTION__; interface InitOptions { defaultStrategy?: string; prefetchAll?: boolean; + ignoreSlowConnection?: boolean; } let inited = false; @@ -40,11 +43,13 @@ export function init(defaultOpts?: InitOptions) { // Fallback default values if not set by user config prefetchAll ??= defaultOpts?.prefetchAll ?? false; defaultStrategy ??= defaultOpts?.defaultStrategy ?? 'hover'; + ignoreSlowConnection ??= defaultOpts?.ignoreSlowConnection ?? false; // In the future, perhaps we can enable treeshaking specific unused strategies initTapStrategy(); initHoverStrategy(); initViewportStrategy(); + initNoneStrategy(); } /** @@ -104,7 +109,7 @@ function initHoverStrategy() { clearTimeout(timeout); } timeout = setTimeout(() => { - prefetch(href, { with: 'fetch' }); + prefetch(href, { with: 'fetch', ignoreSlowConnection }); }, 80) as unknown as number; } @@ -155,7 +160,7 @@ function createViewportIntersectionObserver() { setTimeout(() => { observer.unobserve(anchor); timeouts.delete(anchor); - prefetch(anchor.href, { with: 'link' }); + prefetch(anchor.href, { with: 'link', ignoreSlowConnection }); }, 300) as unknown as number ); } else { @@ -169,6 +174,20 @@ function createViewportIntersectionObserver() { }); } +/** + * Prefetch all links with lower priority + */ +function initNoneStrategy() { + onPageLoad(() => { + for (const anchor of document.getElementsByTagName('a')) { + if (elMatchesStrategy(anchor, 'none')) { + // Prefetch every link in this page + prefetch(anchor.href, { with: 'link', ignoreSlowConnection }); + } + } + }); +} + export interface PrefetchOptions { /** * How the prefetch should prioritize the URL. (default `'link'`) @@ -194,8 +213,8 @@ export interface PrefetchOptions { * @param opts Additional options for prefetching. */ export function prefetch(url: string, opts?: PrefetchOptions) { - const ignoreSlowConnection = opts?.ignoreSlowConnection ?? false; - if (!canPrefetchUrl(url, ignoreSlowConnection)) return; + const willIgnoreSlowConnection = opts?.ignoreSlowConnection ?? ignoreSlowConnection ?? false; + if (!canPrefetchUrl(url, willIgnoreSlowConnection)) return; prefetchedUrls.add(url); const priority = opts?.with ?? 'link'; @@ -216,11 +235,11 @@ export function prefetch(url: string, opts?: PrefetchOptions) { } } -function canPrefetchUrl(url: string, ignoreSlowConnection: boolean) { +function canPrefetchUrl(url: string, willIgnoreSlowConnection: boolean) { // Skip prefetch if offline if (!navigator.onLine) return false; // Skip prefetch if using data saver mode or slow connection - if (!ignoreSlowConnection && isSlowConnection()) return false; + if (!willIgnoreSlowConnection && isSlowConnection()) return false; // Else check if URL is within the same origin, not the current page, and not already prefetched try { const urlObj = new URL(url, location.href); @@ -244,7 +263,13 @@ function elMatchesStrategy(el: EventTarget | null, strategy: string): el is HTML } // Fallback to tap strategy if using data saver mode or slow connection - if (strategy === 'tap' && (attrValue != null || prefetchAll) && isSlowConnection()) { + // If global ignoreSlowConnection is true, it will not fallback + if ( + strategy === 'tap' && + (attrValue != null || prefetchAll) && + !ignoreSlowConnection && + isSlowConnection() + ) { return true; } diff --git a/packages/astro/src/prefetch/vite-plugin-prefetch.ts b/packages/astro/src/prefetch/vite-plugin-prefetch.ts index 83c3c3ff7c59..7ac763f43ae5 100644 --- a/packages/astro/src/prefetch/vite-plugin-prefetch.ts +++ b/packages/astro/src/prefetch/vite-plugin-prefetch.ts @@ -49,7 +49,11 @@ export default function astroPrefetch({ settings }: { settings: AstroSettings }) if (id.includes(prefetchInternalModuleFsSubpath)) { return code .replace('__PREFETCH_PREFETCH_ALL__', JSON.stringify(prefetch?.prefetchAll)) - .replace('__PREFETCH_DEFAULT_STRATEGY__', JSON.stringify(prefetch?.defaultStrategy)); + .replace('__PREFETCH_DEFAULT_STRATEGY__', JSON.stringify(prefetch?.defaultStrategy)) + .replace( + '__PREFETCH_IGNORE_SLOW_CONNECTION__', + JSON.stringify(prefetch?.ignoreSlowConnection) + ); } }, }; From ba78be66418ef7fe7b22a840eb8748298b61823c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= <30424139+wtto00@users.noreply.github.com> Date: Mon, 25 Dec 2023 01:19:48 +0800 Subject: [PATCH 2/9] changeset --- .changeset/two-hats-arrive.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/two-hats-arrive.md diff --git a/.changeset/two-hats-arrive.md b/.changeset/two-hats-arrive.md new file mode 100644 index 000000000000..19c7e491d0ca --- /dev/null +++ b/.changeset/two-hats-arrive.md @@ -0,0 +1,5 @@ +--- +'astro': major +--- + +Add global ignoreSlowConnection and 'none' to defaultStrategy enum in prefetch From 447aa097951b2026193e8b75c90e09001f76eb2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= <30424139+wtto00@users.noreply.github.com> Date: Mon, 25 Dec 2023 01:51:51 +0800 Subject: [PATCH 3/9] change defaultStrategy enum 'none' to 'all', and fix e2e test --- .changeset/two-hats-arrive.md | 2 +- .../prefetch/src/pages/prefetch-all.astro | 1 + .../prefetch/src/pages/prefetch-none.astro | 1 - packages/astro/e2e/prefetch.test.js | 42 ++++++++++++++----- packages/astro/src/@types/astro.ts | 6 +-- packages/astro/src/core/config/schema.ts | 2 +- packages/astro/src/prefetch/index.ts | 6 +-- 7 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-all.astro delete mode 100644 packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro diff --git a/.changeset/two-hats-arrive.md b/.changeset/two-hats-arrive.md index 19c7e491d0ca..556e3266f119 100644 --- a/.changeset/two-hats-arrive.md +++ b/.changeset/two-hats-arrive.md @@ -2,4 +2,4 @@ 'astro': major --- -Add global ignoreSlowConnection and 'none' to defaultStrategy enum in prefetch +Add global ignoreSlowConnection and 'all' to defaultStrategy enum in prefetch diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-all.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-all.astro new file mode 100644 index 000000000000..fae2f68c9846 --- /dev/null +++ b/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-all.astro @@ -0,0 +1 @@ +

Prefetch all

diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro deleted file mode 100644 index 493fd2aba727..000000000000 --- a/packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-none.astro +++ /dev/null @@ -1 +0,0 @@ -

Prefetch none

diff --git a/packages/astro/e2e/prefetch.test.js b/packages/astro/e2e/prefetch.test.js index fafe136bd014..bd5a5aafa20c 100644 --- a/packages/astro/e2e/prefetch.test.js +++ b/packages/astro/e2e/prefetch.test.js @@ -96,10 +96,14 @@ test.describe('Prefetch (default)', () => { expect(reqUrls.filter((u) => u.includes('/prefetch-manual')).length).toEqual(1); }); - test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).toContainEqual('/prefetch-none'); - expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + expect(reqUrls).not.toContainEqual('/prefetch-all'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + ]); + expect(reqUrls).toContainEqual('/prefetch-all'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); }); @@ -189,14 +193,18 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); }); - test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).toContainEqual('/prefetch-none'); - expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + expect(reqUrls).not.toContainEqual('/prefetch-all'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + ]); + expect(reqUrls).toContainEqual('/prefetch-all'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); }); -test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { +test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'all')", () => { let devServer; /** @type {string[]} */ const reqUrls = []; @@ -205,7 +213,7 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { devServer = await astro.startDevServer({ prefetch: { prefetchAll: true, - defaultStrategy: 'none', + defaultStrategy: 'all', }, }); }); @@ -227,6 +235,10 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { test('Link without data-astro-prefetch should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/prefetch-default'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + ]); expect(reqUrls).toContainEqual('/prefetch-default'); }); @@ -237,6 +249,10 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { test('Link with search param should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/?search-param=true'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + ]); expect(reqUrls).toContainEqual('/?search-param=true'); }); @@ -272,9 +288,13 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'none')", () => { expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); }); - test('data-astro-prefetch="none" should prefetch', async ({ page, astro }) => { + test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).toContainEqual('/prefetch-none'); - expect(page.locator('link[rel="prefetch"][href$="/prefetch-none"]')).toBeDefined(); + expect(reqUrls).not.toContainEqual('/prefetch-all'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + ]); + expect(reqUrls).toContainEqual('/prefetch-all'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); }); diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index e32a30088023..a19bdacdd861 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -947,7 +947,7 @@ export interface AstroUserConfig { /** * @docs * @name prefetch.defaultStrategy - * @type {'tap' | 'hover' | 'viewport' | 'none'} + * @type {'tap' | 'hover' | 'viewport' | 'all'} * @default `'hover'` * @description * The default prefetch strategy to use when the `data-astro-prefetch` attribute is set on a link with no value. @@ -955,7 +955,7 @@ export interface AstroUserConfig { * - `'tap'`: Prefetch just before you click on the link. * - `'hover'`: Prefetch when you hover over or focus on the link. (default) * - `'viewport'`: Prefetch as the links enter the viewport. - * - `'none'`: Prefetch the link without any restrictions. + * - `'all'`: Prefetch the link without any restrictions. * * You can override this default value and select a different strategy for any individual link by setting a value on the attribute. * @@ -963,7 +963,7 @@ export interface AstroUserConfig { * About * ``` */ - defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'none'; + defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'all'; /** * @docs diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 358241e23291..64fd7d59f00f 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -182,7 +182,7 @@ export const AstroConfigSchema = z.object({ z.boolean(), z.object({ prefetchAll: z.boolean().optional(), - defaultStrategy: z.enum(['tap', 'hover', 'viewport', 'none']).optional(), + defaultStrategy: z.enum(['tap', 'hover', 'viewport', 'all']).optional(), ignoreSlowConnection: z.boolean().optional(), }), ]) diff --git a/packages/astro/src/prefetch/index.ts b/packages/astro/src/prefetch/index.ts index 3048d11ee25b..8dcc1f44f385 100644 --- a/packages/astro/src/prefetch/index.ts +++ b/packages/astro/src/prefetch/index.ts @@ -49,7 +49,7 @@ export function init(defaultOpts?: InitOptions) { initTapStrategy(); initHoverStrategy(); initViewportStrategy(); - initNoneStrategy(); + initAllStrategy(); } /** @@ -177,10 +177,10 @@ function createViewportIntersectionObserver() { /** * Prefetch all links with lower priority */ -function initNoneStrategy() { +function initAllStrategy() { onPageLoad(() => { for (const anchor of document.getElementsByTagName('a')) { - if (elMatchesStrategy(anchor, 'none')) { + if (elMatchesStrategy(anchor, 'all')) { // Prefetch every link in this page prefetch(anchor.href, { with: 'link', ignoreSlowConnection }); } From c26d90d50b67a73a175a6e85f0cd14c30db29f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= Date: Mon, 25 Dec 2023 11:27:05 +0800 Subject: [PATCH 4/9] test:e2e prefetch --- .../fixtures/prefetch/src/pages/index.astro | 4 +++- packages/astro/e2e/prefetch.test.js | 18 ++---------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro index 88ce196ae22f..fbd72d4ae0a0 100644 --- a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro +++ b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro @@ -20,6 +20,8 @@ Scroll down to trigger viewport prefetch
+ all +
viewport - \ No newline at end of file + diff --git a/packages/astro/e2e/prefetch.test.js b/packages/astro/e2e/prefetch.test.js index bd5a5aafa20c..9684b6014eca 100644 --- a/packages/astro/e2e/prefetch.test.js +++ b/packages/astro/e2e/prefetch.test.js @@ -98,10 +98,6 @@ test.describe('Prefetch (default)', () => { test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).not.toContainEqual('/prefetch-all'); - await Promise.all([ - page.waitForEvent('request'), // wait prefetch request - ]); expect(reqUrls).toContainEqual('/prefetch-all'); expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); @@ -195,10 +191,6 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).not.toContainEqual('/prefetch-all'); - await Promise.all([ - page.waitForEvent('request'), // wait prefetch request - ]); expect(reqUrls).toContainEqual('/prefetch-all'); expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); @@ -235,11 +227,8 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'all')", () => { test('Link without data-astro-prefetch should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).not.toContainEqual('/prefetch-default'); - await Promise.all([ - page.waitForEvent('request'), // wait prefetch request - ]); expect(reqUrls).toContainEqual('/prefetch-default'); + expect(page.locator('link[rel="prefetch"][href$="/prefetch-default"]')).toBeDefined(); }); test('data-astro-prefetch="false" should not prefetch', async ({ page, astro }) => { @@ -252,6 +241,7 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'all')", () => { expect(reqUrls).not.toContainEqual('/?search-param=true'); await Promise.all([ page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-search-param').hover(), ]); expect(reqUrls).toContainEqual('/?search-param=true'); }); @@ -290,10 +280,6 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'all')", () => { test('data-astro-prefetch="all" should prefetch', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); - expect(reqUrls).not.toContainEqual('/prefetch-all'); - await Promise.all([ - page.waitForEvent('request'), // wait prefetch request - ]); expect(reqUrls).toContainEqual('/prefetch-all'); expect(page.locator('link[rel="prefetch"][href$="/prefetch-all"]')).toBeDefined(); }); From 9ca3639dcc556cb2f681fc253adcb4df6305682b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= Date: Mon, 25 Dec 2023 11:34:09 +0800 Subject: [PATCH 5/9] update changeset --- .changeset/two-hats-arrive.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/two-hats-arrive.md b/.changeset/two-hats-arrive.md index 556e3266f119..3801fef2cb7e 100644 --- a/.changeset/two-hats-arrive.md +++ b/.changeset/two-hats-arrive.md @@ -1,5 +1,5 @@ --- -'astro': major +'astro': patch --- -Add global ignoreSlowConnection and 'all' to defaultStrategy enum in prefetch +Add global `ignoreSlowConnection` in prefetch config and `'all'` to `defaultStrategy` enum From 10ca42f2ce22dcc172373d0d64866d0ac1ca382e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= Date: Tue, 26 Dec 2023 19:35:42 +0800 Subject: [PATCH 6/9] rename defaultStrategy.all to defaultStrategy.load --- .changeset/two-hats-arrive.md | 2 +- .../fixtures/prefetch/src/pages/index.astro | 2 +- .../prefetch/src/pages/prefetch-all.astro | 2 +- packages/astro/e2e/prefetch.test.js | 22 +++++++++---------- packages/astro/src/@types/astro.ts | 6 ++--- packages/astro/src/core/config/schema.ts | 2 +- packages/astro/src/prefetch/index.ts | 8 +++---- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.changeset/two-hats-arrive.md b/.changeset/two-hats-arrive.md index 3801fef2cb7e..f7c9f992f6d0 100644 --- a/.changeset/two-hats-arrive.md +++ b/.changeset/two-hats-arrive.md @@ -2,4 +2,4 @@ 'astro': patch --- -Add global `ignoreSlowConnection` in prefetch config and `'all'` to `defaultStrategy` enum +Add global `ignoreSlowConnection` in prefetch config and `'load'` to `defaultStrategy` enum diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro index fbd72d4ae0a0..63a30031ea8a 100644 --- a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro +++ b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro @@ -20,7 +20,7 @@ Scroll down to trigger viewport prefetch
- all + load
viewport