Skip to content

Commit 558b301

Browse files
astro-factory[bot]factory[bot]matthewp
authored
Fix prerendered Cloudflare pages rendering as [object Object] with nodejs_compat (#18011)
When workerd runs with nodejs_compat, Astro's isNode check returns true, causing renderPage to pass an AsyncIterable body to the Response constructor. Workerd's Response does not support this non-standard Node.js extension and stringifies it to "[object Object]". Add isWorkerd detection (via navigator.userAgent) and wrap the AsyncIterable with ReadableStream.from() in workerd. This preserves the faster AsyncIterable render path while producing a standard BodyInit type that workerd accepts. A previous fix (PR #16720) modified isNode to exclude workerd but was reverted (PR #16997) because the renderToReadableStream fallback was slower. This approach avoids that regression by keeping renderToAsyncIterable and only wrapping the output. Fixes #18009 Co-authored-by: factory[bot] <factory[bot]@users.noreply.github.com> Co-authored-by: Matthew Phillips <[email protected]>
1 parent 4464b3a commit 558b301

9 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎.changeset/neat-dingos-sip.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes prerendered Cloudflare pages rendering as `[object Object]` when `nodejs_compat` is enabled in `wrangler.toml`

‎packages/astro/src/runtime/server/render/page.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { encoder } from './common.js';
66
import { type NonAstroPageComponent, renderComponentToString } from './component.js';
77
import { renderCspContent } from './csp.js';
88
import type { AstroComponentFactory } from './index.js';
9-
import { isDeno, isNode } from './util.js';
9+
import { isDeno, isNode, isWorkerd } from './util.js';
1010
import { isAstroComponentFactory } from './astro/factory.js';
1111

1212
export async function renderPage(
@@ -69,8 +69,11 @@ export async function renderPage(
6969

7070
let body: BodyInit | Response;
7171
if (streaming) {
72-
// isNode is true in Deno node-compat mode but response construction from
73-
// async iterables is not supported, so we fall back to ReadableStream if isDeno is true.
72+
// isNode is true in Deno's and workerd's node-compat modes, but their
73+
// Response constructors do not accept AsyncIterable bodies (a non-standard
74+
// Node.js extension). Deno falls back to ReadableStream entirely; workerd
75+
// keeps the faster AsyncIterable render path and wraps the result with
76+
// ReadableStream.from() so the Response constructor receives a standard type.
7477
if (isNode && !isDeno) {
7578
const nodeBody = await renderToAsyncIterable(
7679
result,
@@ -80,9 +83,15 @@ export async function renderPage(
8083
true,
8184
route,
8285
);
83-
// Node.js allows passing in an AsyncIterable to the Response constructor.
84-
// This is non-standard so using `any` here to preserve types everywhere else.
85-
body = nodeBody as any;
86+
if (isWorkerd && !(nodeBody instanceof Response)) {
87+
// ReadableStream.from() is available in Node >= 20.6 and workerd but
88+
// is not yet in TypeScript's built-in DOM lib types.
89+
body = (ReadableStream as any).from(nodeBody);
90+
} else {
91+
// Node.js allows passing in an AsyncIterable to the Response constructor.
92+
// This is non-standard so using `any` here to preserve types everywhere else.
93+
body = nodeBody as any;
94+
}
8695
} else {
8796
body = await renderToReadableStream(result, componentFactory, props, children, true, route);
8897
}

‎packages/astro/src/runtime/server/render/util.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ export const isNode =
336336
typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]';
337337
// @ts-expect-error: Deno is not part of the types.
338338
export const isDeno = typeof Deno !== 'undefined';
339+
export const isWorkerd =
340+
typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers';
339341

340342
// We can get rid of this when Promise.withResolvers() is ready
341343
export type PromiseWithResolvers<T> = {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'astro/config';
2+
3+
export default defineConfig({});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/astro-cloudflare-prerender-nodejs-compat",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"@astrojs/cloudflare": "workspace:*",
7+
"astro": "workspace:*"
8+
}
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
export const prerender = true;
3+
---
4+
<html><body><h1>hello from prerender</h1></body></html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "prerender-nodejs-compat"
2+
compatibility_date = "2025-12-01"
3+
compatibility_flags = ["nodejs_compat"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as assert from 'node:assert/strict';
2+
import { after, before, describe, it } from 'node:test';
3+
import cloudflare from '../dist/index.js';
4+
import { type Fixture, loadFixture } from './test-utils.ts';
5+
6+
describe('Prerendered pages with nodejs_compat', () => {
7+
let fixture: Fixture;
8+
9+
before(async () => {
10+
fixture = await loadFixture({
11+
root: new URL('./fixtures/prerender-nodejs-compat/', import.meta.url).toString(),
12+
adapter: cloudflare(),
13+
});
14+
await fixture.build();
15+
});
16+
17+
after(async () => {
18+
await fixture.clean();
19+
});
20+
21+
it('renders valid HTML instead of [object Object]', async () => {
22+
const html = await fixture.readFile('/client/index.html');
23+
assert.ok(
24+
html.includes('<h1>hello from prerender</h1>'),
25+
`Expected valid HTML but got: ${JSON.stringify(html.slice(0, 100))}`,
26+
);
27+
assert.ok(
28+
!html.includes('[object Object]'),
29+
'Expected no [object Object] in prerendered output',
30+
);
31+
});
32+
});

‎pnpm-lock.yaml‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)