Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prerender fails on build for cloudflare environment #60

Closed
Geb-algebra opened this issue Jan 11, 2025 · 5 comments · Fixed by #63
Closed

prerender fails on build for cloudflare environment #60

Geb-algebra opened this issue Jan 11, 2025 · 5 comments · Fixed by #63
Assignees
Labels
bug Something isn't working waiting feedback on PR

Comments

@Geb-algebra
Copy link

Thank you for creating and maintaining this excellent package.

I encountered build errors when attempting to create an app with pre-rendered pages for the Cloudflare environment.

Steps to reproduce

  1. Clone https://github.com/Geb-algebra/repro-rrhs-build-error-with-prerender (created by adding prerender setup to examples/cloudflare/simple in the package's repository)
  2. Run npm install then npm run build

Observed errors

in v2.6.4, I got this error:

main* $ npm run build

> build
> NODE_OPTIONS="--conditions worker" react-router build

vite v6.0.7 building for production...
✓ 49 modules transformed.
...
✓ built in 647ms
vite v6.0.7 building SSR bundle for production...
✓ 99 modules transformed.
build/server/.vite/manifest.json            0.44 kB
build/server/assets/tailwind-BOKyPWGS.css   6.77 kB
build/server/index.js                      60.24 kB
x Build failed in 295ms
[react-router] Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at groupRoutesByParentId2 (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/vite.js:2706:10)
    at createPrerenderRoutes (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/vite.js:2717:76)
    at handlePrerender (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/vite.js:2533:16)
    at Object.handler (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/vite.js:2040:13)
    at async Promise.all (index 0)
    at PluginDriver.hookParallel (file:///path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20864:9)
    at file:///path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:21907:13
    at catchUnfinishedHookActions (file:///path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:21282:16)
    at buildEnvironment (file:///path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/dep-BJP6rrE_.js:50905:16)
    at viteBuild (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/cli/index.js:1213:5)
    at async Promise.all (index 0)
    at build (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/cli/index.js:1233:3)
    at build2 (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/cli/index.js:1448:5)
    at run2 (/path/to/repro-rrhs-build-error-with-prerender/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]_r_ncrscbwfhco3ryvqp4whomalo4/node_modules/@react-router/dev/dist/cli/index.js:1713:7) {
  code: 'PLUGIN_ERROR',
  plugin: 'react-router',
  hook: 'writeBundle'
}

I also tried in v2.5.0, which ended up in another error:

main* $ npm run build

> build
> NODE_OPTIONS="--conditions worker" react-router build

vite v6.0.7 building for production...
✓ 49 modules transformed.
...
✓ built in 687ms
vite v6.0.7 building SSR bundle for production...
✓ 99 modules transformed.
build/server/.vite/manifest.json            0.44 kB
build/server/assets/tailwind-BOKyPWGS.css   6.77 kB
build/server/index.js                      58.47 kB
x Build failed in 263ms
[react-router] Cannot access 'serverBuild' before initialization
    at file:///path/to/repro-rrhs-build-error-with-prerender/build/server/index.js:1630:52
    at createHonoServer (file:///path/to/repro-rrhs-build-error-with-prerender/build/server/index.js:1630:17)
    at file:///path/to/repro-rrhs-build-error-with-prerender/build/server/index.js:1641:16 {
  code: 'PLUGIN_ERROR',
  plugin: 'react-router',
  hook: 'writeBundle'
}
@rphlmr rphlmr self-assigned this Jan 14, 2025
@rphlmr rphlmr added the bug Something isn't working label Jan 14, 2025
@rphlmr
Copy link
Owner

rphlmr commented Jan 14, 2025

Thanks for reporting!

I think I know why: for Cloudflare, React Router creates a bundle including Hono and React Router, while Node and Bun adapters create a separate file.

I will try to find a fix asap!

@rphlmr
Copy link
Owner

rphlmr commented Jan 14, 2025

So, I'm not sure how to solve that. I can't even pre-render a Cloudflare project without react-router-hono-server.

Cloudflare needs a entry.server.tsx with renderToReadableStream but RR can't pre-render that:

import { renderToReadableStream } from "react-dom/server";
         ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'renderToReadableStream' not found. The requested module 'react-dom/server' is a CommonJS module, which may not support all module.exports as named exports.

If we omit this file, it works but then wrangler can't start:

 The package "node:stream" wasn't found on the file system but is built into node.

That's because by default RR will add its own version of entry.server.tsx that uses renderToPipeableStream and PassThrough

@rphlmr
Copy link
Owner

rphlmr commented Jan 15, 2025

Ok, maybe I found a way:

npm i https://pkg.pr.new/rphlmr/react-router-hono-server@63

#63

@Geb-algebra
Copy link
Author

Thanks for investigating and fixing this! I don’t have time to test it right now, but I’ll check it out in a few days.

@rphlmr
Copy link
Owner

rphlmr commented Jan 26, 2025

Published https://github.com/rphlmr/react-router-hono-server/releases/tag/v2.8.1 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting feedback on PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants