[ci] format

This commit is contained in:
tony-sull 2022-06-27 18:28:16 +00:00 committed by github-actions[bot]
parent 79fe09fa30
commit cb3fcdde6a
6 changed files with 48 additions and 25 deletions

View file

@ -1,4 +1,4 @@
export { }; export {};
declare global { declare global {
interface NetworkInformation { interface NetworkInformation {
@ -9,7 +9,7 @@ declare global {
} }
var NetworkInformation: { var NetworkInformation: {
prototype: NetworkInformation; prototype: NetworkInformation;
new(): NetworkInformation; new (): NetworkInformation;
}; };
} }

View file

@ -66,24 +66,27 @@ export interface PrefetchOptions {
selector?: string; selector?: string;
/** /**
* The number of pages that can be prefetched concurrently. * The number of pages that can be prefetched concurrently.
* *
* @default 1 * @default 1
*/ */
throttle?: number; 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; const conn = navigator.connection;
if (typeof conn !== 'undefined') { if (typeof conn !== 'undefined') {
// Don't prefetch if using 2G or if Save-Data is enabled. // Don't prefetch if using 2G or if Save-Data is enabled.
if (conn.saveData) { if (conn.saveData) {
return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled')); return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled'));
} }
if (/2g/.test(conn.effectiveType)) { if (/2g/.test(conn.effectiveType)) {
return Promise.reject(new Error('Cannot prefetch, network conditions are poor')); return Promise.reject(new Error('Cannot prefetch, network conditions are poor'));
} }
} }
const [toAdd, isDone] = throttles(throttle); const [toAdd, isDone] = throttles(throttle);

View file

@ -9,9 +9,11 @@ export default function (options: PrefetchOptions = {}): AstroIntegration {
// Inject the necessary polyfills on every page (inlined for speed). // Inject the necessary polyfills on every page (inlined for speed).
injectScript( injectScript(
'page', 'page',
`import prefetch from "@astrojs/prefetch/client.js"; prefetch(${JSON.stringify(options)});` `import prefetch from "@astrojs/prefetch/client.js"; prefetch(${JSON.stringify(
options
)});`
); );
} },
} },
}; };
} }

View file

@ -25,8 +25,14 @@ test.describe('Basic prefetch', () => {
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy(); await expect(
await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); 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(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
}); });
}); });
@ -55,8 +61,14 @@ test.describe('Basic prefetch', () => {
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy(); await expect(
await expect(requests.has(astro.resolveUrl('/contact')), '/contact was prefetched').toBeTruthy(); 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(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
}); });
}); });

View file

@ -6,9 +6,9 @@ const test = testFactory({
root: './fixtures/basic-prefetch/', root: './fixtures/basic-prefetch/',
integrations: [ integrations: [
prefetch({ prefetch({
selector: 'a[href="/contact"]' selector: 'a[href="/contact"]',
}), }),
] ],
}); });
test.describe('Custom prefetch selectors', () => { test.describe('Custom prefetch selectors', () => {
@ -34,7 +34,10 @@ test.describe('Custom prefetch selectors', () => {
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); 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(); 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 page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); 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(); await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
}); });
}); });

View file

@ -5,6 +5,6 @@
"allowJs": true, "allowJs": true,
"module": "ES2020", "module": "ES2020",
"outDir": "./dist", "outDir": "./dist",
"target": "ES2020", "target": "ES2020"
} }
} }