Runtime Differences
Where fetch() and Request in a deployed worker behave differently from browsers and Node.js, and how to write code that works in both.
Runtime Differences
Workers run on a server runtime, not in a browser or in Node.js. Most web APIs
behave the same, but a few fetch() and Request options are rejected or
ignored. TypeScript with the dom lib accepts all of them, and Node.js runs
them, so code that works in tests and in next dev can still fail once it is
deployed.
redirect: "error" is not supported
// Fails on Ploy
await fetch(url, { redirect: "error" });redirect accepts only "follow" (the default) and "manual". With
"error", fetch() rejects and new Request() throws:
TypeError: Invalid redirect value, must be one of "follow" or "manual" ("error" won't be implemented since it does not make sense at the edge; use "manual" and check the response status code).The request is never sent. If the call sits inside a try/catch that logs
and moves on, nothing reaches the other side and nothing looks wrong. Webhooks,
notifications and analytics calls fail this way.
Use "manual" and treat a redirect as the error:
const response = await fetch(url, { method: "POST", body, redirect: "manual" });
if (response.status >= 300 && response.status < 400) {
throw new Error(`Unexpected redirect to ${response.headers.get("location")}`);
}
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}With "manual", a worker receives the redirect response itself: its real
status (for example 302) and its Location header. A browser instead
returns an opaque response with status 0. Code that treats any non-2xx
response as a failure handles both.
cache accepts only "no-store" and "no-cache"
Leave cache out for the default behavior. Every other value, including
"default", "reload", "force-cache" and "only-if-cached", throws:
TypeError: Unsupported cache mode: defaultintegrity must be empty
Subresource integrity checks are not implemented. A non-empty integrity
throws TypeError: Subrequest integrity checking is not implemented.
Options that are ignored
mode, credentials, referrer, referrerPolicy, keepalive and
priority are accepted and have no effect. A worker has no cookie jar, no
CORS checks and no page to send a referrer from, so these options have nothing
to control. The matching Request properties (request.mode,
request.credentials, request.referrer, request.referrerPolicy) are
undefined.
To send cookies or credentials, set the Cookie or Authorization header
yourself.
How Ploy catches these early
- Types:
@meetploy/typesleaves"error"out ofRequestRedirectand narrowsRequestCacheto the supported values, soploy typesprojects without thedomlib get a type error. With thedomlib, the DOM definitions take precedence;fetch(url, { redirect: "error" })is then marked as deprecated in your editor, and reading its result (for exampleresponse.ok) fails to typecheck. - Builds: when a deployment's worker bundle sets
redirect: "error", the build log shows a warning naming the files. It is a warning rather than a failure because a bundle can include code that never runs on the server, such as browser-only modules. If the flagged code only runs in the browser, you can ignore it. - Local development:
ploy devruns plain workers on the same runtime as production. Undernext dev,initPloyForDev()makesfetch()andnew Request()rejectredirect: "error"with the sameTypeError. It does not check the other options above.
The browser bundle of your app (client components, static assets) runs in the browser, where all of these options work as usual.
How is this guide?
Last updated on