-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Possible race condition leading to "TypeError: Cannot read property 'context' of undefined" #3220
Comments
I managed to reproduce the bug here : https://github.com/PaulBlanche/preact-bug-reproduction |
Does anybody have some pointer for where i should look to try and understand the problem ? |
I think I've just encountered this same error in a different context. I've managed to narrow it down to a very simple failing example: https://github.com/dancras/preact-bug
|
@dancras Looking at that output it seems like Reported it upstream at vitest-dev/vitest#747 |
Closing as this should be fixed upstream by preact-testing-library adding the ESM build |
It sounds like my React-router problem might be related to this! For me the error always happens. As I commented on the issue above, when I use react-router 5.x the app works, including with SSR. But it fails the moment I try to get a hook from the router, be it I verified and, in fact, the error is happening on the CJS Edit: I think I found the problem, here is the solution - remix-run/react-router#10175 |
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)
Describe the bug
There seems to be a race condition related to
useContext
withSuspense
andpreact-ssr-prepass
. In my application i useusePromise
(fromreact-promise-suspense
) to await on certain computation server-side.Sometimes (but not systematically) the ssr prepass fails with this error :
If i remove the longest computations (the ones that would need to be awaited on the most during the ssr prepass), the error disappears. If i replace the computation with a simple timeout, the error still happens. That's why i suspect a race condition. I don't know if this is an issue with
preact
orpreact-ssr-prepass
.To Reproduce
I could not reproduce this issue with a simplified setup on CodeSandbox. I could share my codebase if needed but there is a lot of moving parts.
Expected behavior
When a value is passed to a
context.Provider
, auseContext
should return this value even in the context of an ssr prepass.The text was updated successfully, but these errors were encountered: