From b8bd686d15c22753a69b4430d7ed324eea57a7d0 Mon Sep 17 00:00:00 2001 From: Lee Powell Date: Thu, 21 May 2020 20:52:07 +0100 Subject: [PATCH 1/2] prefetch returns promise instead of undefined --- src/index.mjs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index ccdb78af..0c0b8589 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -114,21 +114,26 @@ export function listen(options) { export function prefetch(url, isPriority, conn) { if (conn = navigator.connection) { // Don't prefetch if using 2G or if Save-Data is enabled. - if (conn.saveData || /2g/.test(conn.effectiveType)) return; + 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')); + } } // Dev must supply own catch() return Promise.all( - [].concat(url).map(str => { - if (!toPrefetch.has(str)) { - // Add it now, regardless of its success - // ~> so that we don't repeat broken links - toPrefetch.add(str); + [].concat(url).map(str => { + if (!toPrefetch.has(str)) { + // Add it now, regardless of its success + // ~> so that we don't repeat broken links + toPrefetch.add(str); - return (isPriority ? priority : supported)( - new URL(str, location.href).toString() - ); - } - }) + return (isPriority ? priority : supported)( + new URL(str, location.href).toString() + ); + } + }) ); } From 2e8e4ea3ca7ea0f57b840c3d89314c309ea7b192 Mon Sep 17 00:00:00 2001 From: Lee Powell Date: Thu, 21 May 2020 20:52:07 +0100 Subject: [PATCH 2/2] prefetch returns promise instead of undefined --- .vscode/settings.json | 7 +++++++ src/chunks.mjs | 9 +++++++-- src/index.mjs | 9 +++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..e7962eba --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "editor.codeActionsOnSave": { + "source.fixAll.eslint": false, + "source.organizeImports": false + }, + "editor.formatOnSave": true +} diff --git a/src/chunks.mjs b/src/chunks.mjs index 5d617e43..fe849007 100644 --- a/src/chunks.mjs +++ b/src/chunks.mjs @@ -121,7 +121,12 @@ export function listen(options) { export function prefetch(url, isPriority, conn) { if (conn = navigator.connection) { // Don't prefetch if using 2G or if Save-Data is enabled. - if (conn.saveData || /2g/.test(conn.effectiveType)) return; + 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')); + } } // Dev must supply own catch() @@ -138,4 +143,4 @@ export function prefetch(url, isPriority, conn) { } }) ); -} \ No newline at end of file +} diff --git a/src/index.mjs b/src/index.mjs index ccdb78af..4363cbcb 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -114,7 +114,12 @@ export function listen(options) { export function prefetch(url, isPriority, conn) { if (conn = navigator.connection) { // Don't prefetch if using 2G or if Save-Data is enabled. - if (conn.saveData || /2g/.test(conn.effectiveType)) return; + 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')); + } } // Dev must supply own catch() @@ -126,7 +131,7 @@ export function prefetch(url, isPriority, conn) { toPrefetch.add(str); return (isPriority ? priority : supported)( - new URL(str, location.href).toString() + new URL(str, location.href).toString() ); } })