Skip to content

Commit a8872b8

Browse files
leepowelldevLee Powell
andauthored
Fix: Cannot read property 'then' of undefined (#188)
* prefetch returns promise instead of undefined * prefetch returns promise instead of undefined Co-authored-by: Lee Powell <[email protected]>
1 parent 172275b commit a8872b8

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/chunks.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ export function listen(options) {
121121
export function prefetch(url, isPriority, conn) {
122122
if (conn = navigator.connection) {
123123
// Don't prefetch if using 2G or if Save-Data is enabled.
124-
if (conn.saveData || /2g/.test(conn.effectiveType)) return;
124+
if (conn.saveData) {
125+
return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled'));
126+
}
127+
if (/2g/.test(conn.effectiveType)) {
128+
return Promise.reject(new Error('Cannot prefetch, network conditions are poor'));
129+
}
125130
}
126131

127132
// Dev must supply own catch()
@@ -138,4 +143,4 @@ export function prefetch(url, isPriority, conn) {
138143
}
139144
})
140145
);
141-
}
146+
}

src/index.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export function listen(options) {
114114
export function prefetch(url, isPriority, conn) {
115115
if (conn = navigator.connection) {
116116
// Don't prefetch if using 2G or if Save-Data is enabled.
117-
if (conn.saveData || /2g/.test(conn.effectiveType)) return;
117+
if (conn.saveData) {
118+
return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled'));
119+
}
120+
if (/2g/.test(conn.effectiveType)) {
121+
return Promise.reject(new Error('Cannot prefetch, network conditions are poor'));
122+
}
118123
}
119124

120125
// Dev must supply own catch()

0 commit comments

Comments
 (0)