question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Cloudflare pages/workers

See original GitHub issue

I have a project that’s deployed to Cloudflare. The runtime environment behaves like a service worker, so it has fetch but not window, hence getting this error:

ReferenceError: window is not defined
    at node_modules/isomorphic-form-data/lib/browser.js

There’s a PR to fix isomorphic-form-data, I doubt it’s getting merged: https://github.com/form-data/isomorphic-form-data/pull/6

There’s also no XHR (some specific technical reasons for that), so importing masto/fetch also fails:

TypeError: globalThis.XMLHttpRequest is not a constructor
    at node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js

I wonder if there’s an easy fix here, maybe a bundle that doesn’t use any polyfills?

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
neetcommented, Dec 19, 2022

Thank you for explaining. I thought it was about FromData, but now I understand it is Mastodon’s undocumented behaviour. I don’t know it if is intended, but as you described, setting Content-Type to multipart/form-data doesn’t seem to make sense.

I made a PR that removes Content-Type when the body is an instance of FormData regardless if it’s containing a Blob #758. It will be released soon

1reaction
assafcommented, Dec 13, 2022

Essentially they’re running server-side code in V8 as service workers. A service worker can listen and respond to HTTP requests as fetch events: https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent

Service workers don’t have access to window, the global context is globalThis, which also exists in Node.

I get the same error using Masto from a Chrome extension, it runs code in the background like a service worker. But Chrome is fine with masto/fetch, I’m guessing the XHR issue is specific to Cloudflare so they throttle outbound requests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloudflare Pages
Build fast sites. In record time. Cloudflare Pages is a JAMstack platform for frontend developers to collaborate and deploy websites. Sign Up.
Read more >
Cloudflare Pages Goes Full Stack
Welcome to the future, everyone. We're thrilled to announce that Pages is now a Full Stack platform with help from Cloudflare Workers!
Read more >
Functions · Cloudflare Pages docs
With Pages, you can now build full-stack applications by executing code on the Cloudflare network with help from Cloudflare Workers Open ...
Read more >
Migrating from Workers Sites to Pages - Cloudflare Docs
You can migrate your redirects to Pages, by creating a _redirects file in your output directory. Pages currently offers limited support for ...
Read more >
Cloudflare Workers®
Build your next application with Cloudflare Workers. ... within milliseconds of your users worldwide; Say goodbye to cold starts—support for 0ms worldwide.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found