Azure / fetch-event-source

A better API for making Event Source requests, with all the features of fetch()
MIT License
1.82k stars 143 forks source link

Error: document is not defined #39

Open rendomnet opened 1 year ago

rendomnet commented 1 year ago

I'm trying to use this library in browser extension service worker. And it gives me error "document is not defined".

zhl5213 commented 1 year ago

me too

yaxiaoliu commented 1 year ago

me too

jeromewe commented 1 year ago

me too

luichooy commented 1 year ago

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.

imrockpan commented 1 year ago

It works fine by:

  1. Set openWhenHidden to true.
  2. Mock a window constant.
    if (!globalThis.window) {
    globalThis.window = {
      fetch: globalThis.fetch,
      setTimeout: globalThis.setTimeout,
      clearTimeout: globalThis.clearTimeout,
    }
    }
pond918 commented 1 year ago
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: () => {} };
HereOrCode commented 10 months ago

me too

Any updates?

LikeDreamwalker commented 4 months ago

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.

CharlesFarhat commented 3 months ago

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...