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

v4 SSR gives incorrect urls #4523

Closed
osenvosem opened this issue Feb 13, 2017 · 5 comments
Closed

v4 SSR gives incorrect urls #4523

osenvosem opened this issue Feb 13, 2017 · 5 comments

Comments

@osenvosem
Copy link

osenvosem commented Feb 13, 2017

Version

4.0.0-beta.5

Expected Behavior

I expect it to render correct urls. 🙂

Actual Behavior

Renders a tags with incorrect url.

Steps to reproduce

If I disable Javascript in browser and the app renders only on the server, incorrect urls render with double slashes.

<Link to="/">Home</Link> // url renders as '//'
<Link to="/one">One</Link> // renders as '//one'
// etc...

screen shot 2017-02-13 at 18 34 09

Server side code

  const renderLayout = (html, state = {}) => {
    return `
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>My lovely app</title>
      </head>
      <body>
        <div id="root"><div>${html}</div></div>

        <script>window.__INITIAL_STATE__ = ${JSON.stringify(state)}</script>
        <script src="/assets/manifest.bundle.js"></script>
        <script src="/assets/vendor.bundle.js"></script>
        <script src="/assets/main.bundle.js"></script>
      </body>
      </html>
    `
  }

  app.use('*', (req, res) => {
    const context = {}
    const renderedLayout = renderLayout(
      renderToString(
        <StaticRouter location={req.url} context={context}>
          <Root />
        </StaticRouter>
      ), {}
    )

    if (context.url) {
      return res.redirect(302, context.url)
    } else {
      return res.send(renderedLayout)
    }
  }) 
@pshrmn
Copy link
Contributor

pshrmn commented Feb 13, 2017

Fixed in #4484

@pshrmn pshrmn closed this as completed Feb 13, 2017
@osenvosem
Copy link
Author

@pshrmn sorry for maybe a silly question, but how can I install fixed version? The '4.0.0-beta.5' version I'm currently using is the latest one in react-router and react-router-dom.

@pshrmn
Copy link
Contributor

pshrmn commented Feb 13, 2017

There isn't a good answer to that. Until a new release is put out so that it can be installed through npm, the most correct solution would be to clone the repo, build the v4 branch, and npm link it. That is a lot of work for something with a simple fix. The hot-fix solution would be to find node_modules/react-router/StaticRouter.js and modify line 105 (hopefully that is the correct number) to be:

return (0, _PathUtils.addLeadingSlash)(_this.props.basename + createURL(path));

That moves the closing parentheses for the addLeadingSlash function, which is the fix.

That isn't ideal, but it will prevent your code from generating the extra slash until the fix is released.

@sponia-joker
Copy link

I aslo encountered then problem,thanks solved problem

@sponia-joker
Copy link

when the value is 'javascript:;',the problem is still exist.
(client) "cursor-hand" href="javascript:;" data-r
(server) "cursor-hand" href="/javascript:;" data-
Is there a better solution here?
thanks

@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants