-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
[Bug]: Vite + Preact + SSR: TypeError: Cannot read properties of undefined (reading 'context') #10172
Comments
This doesn't look like a bug in this library. We're using React APIs. If Preact doesn't emulate them properly, that's not really something we have to fix for. That should be handled upstream. |
This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2]. During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code. Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs". (node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. [1] preactjs/preact#3220 (comment) [2] vitest-dev/vitest#747 (comment)
This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2]. During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code. Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs". (node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. [1] preactjs/preact#3220 (comment) [2] vitest-dev/vitest#747 (comment)
If I understand correctly, I have experienced the same issue. This issue can reproduced with just Node.js and Preact, it does not require an external bundle like Vite to be observed. I added a minimum example here: Preact does implement all of the React APIs needed including the Context/useContext APIs. The issue is in module format and resolution. Preact is a dual format package (CommonJS + ESM). While react-router and react-router-dom have some CommonJS and ESM-ish files they do not provide the ESM files in a format that lets vanilla Node.js consume react-router in pure ESM mode. Note that Node.js only looks at the While react-router does not use
That server.mjs contains:
And Node.js resolves that import to react-router-dom/dist/main.js, a CommonJS file. To be more "hygenic", ideally |
@patdx Awesome work! This information is gold. Thank you for this detailed explanation. |
Related issue: preactjs/preset-vite#73
What version of React Router are you using?
6.8.2 (latest)
Steps to Reproduce
I started with Vite and @preact/preset-vite.
I want my project to be fully compatible with React, but using @preact/compat.
Expected Behavior
The router works in the server and the browser, including the
useLocation()
anduseNavigate()
hooks.Actual Behavior
When I start the app, I get this error:
Link to CodeSandbox
I've managed to get it almost working with [email protected] if I don't try to use any of the router hooks.
If I get the
useLocation()
oruseHistory()
hooks from 5.x, I get exactly the same error as above as [email protected].The text was updated successfully, but these errors were encountered: