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

[Bug]: SSR rendering error document is not defined #9077

Closed
ygs-code opened this issue Jul 20, 2022 · 2 comments
Closed

[Bug]: SSR rendering error document is not defined #9077

ygs-code opened this issue Jul 20, 2022 · 2 comments
Labels

Comments

@ygs-code
Copy link

ygs-code commented Jul 20, 2022

What version of React Router are you using?

v6 6.3.0

Steps to Reproduce

When I was rendering with SSR,Props Introduced to the history. But instead of using the history I passed in, the Router uses \node_modules\react-router-dom\node_modules\history\umd\history.production.min.js the history. occurred document is not defined.

code:

import express from 'express';
import { createMemoryHistory } from 'history';
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import * as ReactDOMServer from 'react-dom/server';
const app = express();
const htmlTemplate = function (root) {
    return `
  <!DOCTYPE HTML>
  <html>
    <head>
    </head>
    <body>
      <div id="app">${root}</div>
      <script src="index.js"></script>
    </body>
  </html>
  `;
};

const Home = () => <div>Home</div>;
const About = () => <div>About</div>;

const App = (props) => (
    <Router history={props.history}>
        <Suspense fallback={<div>Loading...</div>}>
            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="/about" element={<About />} />
            </Routes>
        </Suspense>
    </Router>
);

app.get('/', (req, res) => {
    let history = createMemoryHistory({ initialEntries: [req.url] });
    console.log('history=', history);
    const root = ReactDOMServer.renderToString(<App history={history} />);
    res.setHeader('Content-Type', 'text/html');
    res.end(htmlTemplate(root));
});
const PORT = 3000;
app.listen(PORT, () => {
    console.log('SSR server start at ', PORT);
});



Expected Behavior

In SSR I can use the history I passed in

Actual Behavior

When I was rendering with SSR,Props Introduced to the history. But instead of using the history I passed in, the Router uses \node_modules\react-router-dom\node_modules\history\umd\history.production.min.js the history. occurred document is not defined.

@ygs-code ygs-code added the bug label Jul 20, 2022
@timdorr
Copy link
Member

timdorr commented Jul 20, 2022

history is not a prop on BrowserRouter. You should also be using MemoryRouter in the server context.

@ygs-code
Copy link
Author

What do I need to write code,You can write me a demo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants