Skip to content

[Bug]: Vite + Preact + SSR: TypeError: Cannot read properties of undefined (reading 'context') #10172

@rhengles

Description

@rhengles

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() and useNavigate() hooks.

Actual Behavior

When I start the app, I get this error:

TypeError: Cannot read properties of undefined (reading 'context')
    at Object.exports.useContext (/project/home/rhengles/workspace/node_modules/preact/hooks/dist/hooks.js:1:2680)
    at Object.Routes (/project/home/rhengles/workspace/node_modules/react-router/dist/umd/react-router.development.js:1177:46)
    at file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:6332
    at F (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:6359)
    at F (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:6434)
    at F (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:6434)
    at F (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:6434)
    at F (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:7454)
    at Proxy.S (file:///project/home/rhengles/workspace/node_modules/preact-render-to-string/dist/index.mjs:1:4833)
    at render (entry-server.jsx:8:4)
    at file:///project/home/rhengles/workspace/server.js:76:23

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() or useHistory() hooks from 5.x, I get exactly the same error as above as [email protected].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions