-
Notifications
You must be signed in to change notification settings - Fork 45
Conversation
"fetch" variable needs to be declared before being assigned, otherwise you get a "Uncaught ReferenceError" error
src/client.js
Outdated
if (isNode) { | ||
if (isNode && !haveNativeFetch || | ||
// The test mocks do not return a body with getReader | ||
typeof response.body.getReader === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we can remove the condition on isNode
and haveNativeFetch
as the second part of the condition will catch this anyway
src/client.js
Outdated
|
||
initializeFetch(); | ||
const configuredFetch = isNode && !haveNativeFetch ? | ||
(await import('node-fetch')).default : globalThis.fetch; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we should only use the haveNativeFetch
condition no? Is there a case where isNode
would be false
and haveNativeFetch
be false
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Back on laptop :) Thanks for the prodding
const configuredFetch = globalThis.fetch ??
(await import('node-fetch')).default;
Went with this and killed off haveNativeFetch/isNode entirely by just using feature detection, and adding a comment
// When using node-fetch or test mocks, getReader is not defined
if (typeof response.body.getReader === 'undefined') {
On mobile/email.
I believe you are correct regarding the logic but it would depend on how
offended you are by not reducing as to whether it should be corrected. As I
age I find more explicit code easier to work with. A mix of pragmatism and
slowing.
That said, can change it.
… Message ID: ***@***.***>
|
src/client.js
Outdated
} | ||
|
||
initializeFetch(); | ||
const configuredFetch = globalThis.fetch ?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work now in service workers (mv3 web ext), and in newer versions of node with fetch available natively
src/client.js
Outdated
|
||
initializeFetch(); | ||
const configuredFetch = globalThis.fetch ?? | ||
(await import('node-fetch')).default; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this (top level await) won't work with commonjs (which this library should ideally support in the future)
Superceded by #66
Closes:
Update fetch detection logic (See: #63 (comment)):
Use top level await, per here and here (this won't work in commonjs)