[ci] format
This commit is contained in:
parent
79fe09fa30
commit
cb3fcdde6a
6 changed files with 48 additions and 25 deletions
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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
|
||||
)});`
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,6 +5,6 @@
|
|||
"allowJs": true,
|
||||
"module": "ES2020",
|
||||
"outDir": "./dist",
|
||||
"target": "ES2020",
|
||||
"target": "ES2020"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue