fetch-event-source
fetch-event-source copied to clipboard
Error: document is not defined
I'm trying to use this library in browser extension service worker. And it gives me error "document is not defined".
me too
me too
me too
me too i set openWhenHidden to true then this error disappeared but "window is not defined" occured
i saw source codes contained document and window object
May I know if browser extension service worker is supported? Thank you.
It works fine by:
- Set openWhenHidden to true.
- Mock a window constant.
if (!globalThis.window) {
globalThis.window = {
fetch: globalThis.fetch,
setTimeout: globalThis.setTimeout,
clearTimeout: globalThis.clearTimeout,
}
}
if (!globalThis.window) { globalThis.window = { fetch: globalThis.fetch, setTimeout: globalThis.setTimeout, clearTimeout: globalThis.clearTimeout, } }
to make it more robust:
if (!globalThis.window) {
globalThis.window = {
fetch: globalThis.fetch,
setTimeout: (fn, timeout) => globalThis.setTimeout(fn, timeout),
clearTimeout: (t) => globalThis.clearTimeout(t),
};
}
if (!globalThis.document)
globalThis.document = { removeEventListener: () => {} };
me too
Any updates?
I am trying to use it in Next.js 14's app handler and can face this issue since the code is running on the server, which is Node.js. But I wonder why fetch-event-source needs to read the document.
Same error here ! but when I add the fix above, then when I refresh the page I got a TypeError: Cannot destructure property 'protocol' of 'window.location' as it is undefined.
I am going mad...