diff --git a/packages/integrations/prefetch/@types/network-information.d.ts b/packages/integrations/prefetch/@types/network-information.d.ts index 8932e1f1a..bf1d787f3 100644 --- a/packages/integrations/prefetch/@types/network-information.d.ts +++ b/packages/integrations/prefetch/@types/network-information.d.ts @@ -1,4 +1,4 @@ -export { }; +export {}; declare global { interface NetworkInformation { @@ -9,7 +9,7 @@ declare global { } var NetworkInformation: { - prototype: NetworkInformation; - new(): NetworkInformation; + prototype: NetworkInformation; + new (): NetworkInformation; }; } diff --git a/packages/integrations/prefetch/src/client.ts b/packages/integrations/prefetch/src/client.ts index 46a688f9d..9f951c8dd 100644 --- a/packages/integrations/prefetch/src/client.ts +++ b/packages/integrations/prefetch/src/client.ts @@ -66,24 +66,27 @@ export interface PrefetchOptions { selector?: string; /** * The number of pages that can be prefetched concurrently. - * + * * @default 1 */ throttle?: number; } -export default function prefetch({ selector = 'a[href][rel~="prefetch"]', throttle = 1 }: PrefetchOptions) { +export default function prefetch({ + selector = 'a[href][rel~="prefetch"]', + throttle = 1, +}: PrefetchOptions) { const conn = navigator.connection; if (typeof conn !== 'undefined') { - // Don't prefetch if using 2G or if Save-Data is enabled. - if (conn.saveData) { - return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled')); - } - if (/2g/.test(conn.effectiveType)) { - return Promise.reject(new Error('Cannot prefetch, network conditions are poor')); - } - } + // Don't prefetch if using 2G or if Save-Data is enabled. + if (conn.saveData) { + return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled')); + } + if (/2g/.test(conn.effectiveType)) { + return Promise.reject(new Error('Cannot prefetch, network conditions are poor')); + } + } const [toAdd, isDone] = throttles(throttle); diff --git a/packages/integrations/prefetch/src/index.ts b/packages/integrations/prefetch/src/index.ts index 15f52272c..a15f88747 100644 --- a/packages/integrations/prefetch/src/index.ts +++ b/packages/integrations/prefetch/src/index.ts @@ -9,9 +9,11 @@ export default function (options: PrefetchOptions = {}): AstroIntegration { // Inject the necessary polyfills on every page (inlined for speed). injectScript( 'page', - `import prefetch from "@astrojs/prefetch/client.js"; prefetch(${JSON.stringify(options)});` + `import prefetch from "@astrojs/prefetch/client.js"; prefetch(${JSON.stringify( + options + )});` ); - } - } + }, + }, }; } diff --git a/packages/integrations/prefetch/test/basic-prefetch.test.js b/packages/integrations/prefetch/test/basic-prefetch.test.js index 897c00590..0dbb571be 100644 --- a/packages/integrations/prefetch/test/basic-prefetch.test.js +++ b/packages/integrations/prefetch/test/basic-prefetch.test.js @@ -25,8 +25,14 @@ test.describe('Basic prefetch', () => { await page.waitForLoadState('networkidle'); - await expect(requests.has(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy(); - await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/about')), + '/about was prefetched' + ).toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/contact')), + '/contact was prefetched' + ).toBeTruthy(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); }); }); @@ -55,8 +61,14 @@ test.describe('Basic prefetch', () => { await page.waitForLoadState('networkidle'); - await expect(requests.has(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy(); - await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/about')), + '/about was prefetched' + ).toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/contact')), + '/contact was prefetched' + ).toBeTruthy(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); }); }); diff --git a/packages/integrations/prefetch/test/custom-selectors.test.js b/packages/integrations/prefetch/test/custom-selectors.test.js index 2ed5b9753..64ac5fc63 100644 --- a/packages/integrations/prefetch/test/custom-selectors.test.js +++ b/packages/integrations/prefetch/test/custom-selectors.test.js @@ -6,9 +6,9 @@ const test = testFactory({ root: './fixtures/basic-prefetch/', integrations: [ prefetch({ - selector: 'a[href="/contact"]' + selector: 'a[href="/contact"]', }), - ] + ], }); test.describe('Custom prefetch selectors', () => { @@ -34,7 +34,10 @@ test.describe('Custom prefetch selectors', () => { await page.waitForLoadState('networkidle'); await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); - await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/contact')), + '/contact was prefetched' + ).toBeTruthy(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); }); }); @@ -64,7 +67,10 @@ test.describe('Custom prefetch selectors', () => { await page.waitForLoadState('networkidle'); await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); - await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); + await expect( + requests.has(astro.resolveUrl('/contact')), + '/contact was prefetched' + ).toBeTruthy(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); }); }); diff --git a/packages/integrations/prefetch/tsconfig.json b/packages/integrations/prefetch/tsconfig.json index a67ee6859..ae0c4a876 100644 --- a/packages/integrations/prefetch/tsconfig.json +++ b/packages/integrations/prefetch/tsconfig.json @@ -5,6 +5,6 @@ "allowJs": true, "module": "ES2020", "outDir": "./dist", - "target": "ES2020", + "target": "ES2020" } }