Skip to content

Commit

Permalink
Improve prefetch conditions (#5244)
Browse files Browse the repository at this point in the history
* Improve prefetch

* Add changeset
  • Loading branch information
deeprobin committed Nov 1, 2022
1 parent 2f8c9a9 commit 6ad91bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-spies-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/prefetch': patch
---

Do not prefetch if browser is offline or uses 3G
34 changes: 18 additions & 16 deletions packages/integrations/prefetch/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function shouldPreload({ href }: { href: string }) {
window.location.pathname !== url.pathname &&
!preloaded.has(href)
);
} catch {}
} catch { }

return false;
}
Expand Down Expand Up @@ -44,12 +44,11 @@ function onLinkEvent({ target }: Event) {

async function preloadHref(link: HTMLAnchorElement) {
unobserve(link);

const { href } = link;

try {
const contents = await fetch(href).then((res) => res.text());
parser = parser || new DOMParser();
parser ||= new DOMParser();

const html = parser.parseFromString(contents, 'text/html');
const styles = Array.from(html.querySelectorAll<HTMLLinkElement>('link[rel="stylesheet"]'));
Expand All @@ -62,7 +61,7 @@ async function preloadHref(link: HTMLAnchorElement) {
return fetch(el.href);
})
);
} catch {}
} catch { }
}

export interface PrefetchOptions {
Expand All @@ -84,14 +83,21 @@ export default function prefetch({
selector = 'a[href][rel~="prefetch"]',
throttle = 1,
}: PrefetchOptions) {
const conn = navigator.connection;
// If the navigator is offline, it is very unlikely that a request can be made successfully
if (!navigator.onLine) {
return Promise.reject(new Error('Cannot prefetch, no network connection'));
}

if (typeof conn !== 'undefined') {
// Don't prefetch if using 2G or if Save-Data is enabled.
if (conn.saveData) {
// `Navigator.connection` is an experimental API and is not supported in every browser.
if ('connection' in navigator) {
const connection = (navigator as any).connection;
// Don't prefetch if Save-Data is enabled.
if (connection.saveData) {
return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled'));
}
if (/2g/.test(conn.effectiveType)) {

// Do not prefetch if using 2G or 3G
if (/(2|3)g/.test(connection.effectiveType)) {
return Promise.reject(new Error('Cannot prefetch, network conditions are poor'));
}
}
Expand All @@ -109,12 +115,8 @@ export default function prefetch({
});

requestIdleCallback(() => {
const links = Array.from(document.querySelectorAll<HTMLAnchorElement>(selector)).filter(
shouldPreload
);

for (const link of links) {
observe(link);
}
const links = [...document.querySelectorAll<HTMLAnchorElement>(selector)]
.filter(shouldPreload);
links.forEach(observe);
});
}
2 changes: 1 addition & 1 deletion packages/integrations/prefetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function (options: PrefetchOptions = {}): AstroIntegration {
return {
name: '@astrojs/prefetch',
hooks: {
'astro:config:setup': ({ updateConfig, addRenderer, injectScript }) => {
'astro:config:setup': ({ injectScript }) => {
// Inject the necessary polyfills on every page (inlined for speed).
injectScript(
'page',
Expand Down

0 comments on commit 6ad91bd

Please sign in to comment.