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

Link not working with server side rendering #572

Closed
walidargh opened this issue Oct 21, 2016 · 5 comments
Closed

Link not working with server side rendering #572

walidargh opened this issue Oct 21, 2016 · 5 comments

Comments

@walidargh
Copy link

walidargh commented Oct 21, 2016

I am having trouble getting a link tag to work when using server-side rendering with react router

I essentially copied the router code from the example https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ServerRouterApp.jsx:

My Routes:

export default (
  <Route path="react_router" component={RouterLayout}>
    <Route path="first_page" component={RouterFirstPage} />
    <Route path="second_page" component={RouterSecondPage} />
  </Route>
);
import React from 'react';
import { match, RouterContext } from 'react-router';
import routes from '../routes/routes';

export default (props, railsContext) => {
  let error;
  let redirectLocation;
  let routeProps;
  const { location } = railsContext;

  match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
    error = _error;
    redirectLocation = _redirectLocation;
    routeProps = _routeProps
  });

  if (error || redirectLocation) {
    return { error, redirectLocation };
  }

  return (
      <RouterContext {...routeProps} />
  );
};

And in my rails view I am rendering the ServerApp:

<%=react_component("ServerRouter", props: {posts: @posts}, prerender: true) %>
import React, { PropTypes } from 'react';
import {Link} from 'react-router';

export default class Tile extends React.Component {
  constructor(props, context) {
    super(props, context);
  }

  static contextTypes = {
    router: PropTypes.object,
  }

  render() {
    return (
        <div className="tile-con">
          <Link to="listings/5">Hello</Link>
          <h2>{this.props.post.title}</h2>
          <p>{this.props.post.body}</p>
        </div>
    );
  }
}

I can get this working fine by using my Client Router and setting prerender to false:

export default (props, context) => (
  <Router history={browserHistory}  {...props}>
    <Route path="react" component={GridLayout}>
      <Route path="listings" component={Grid} />
      <Route path="listings/:listingId" component={ListingDetails} />
    </Route>
  </Router>
);

Not sure what I am doing wrong. Any help is appreciated!

@justin808
Copy link
Member

@walidargh are you using turbolinks? You probably don't want to use it with react-router. See #573

@walidargh
Copy link
Author

@justin808 Thanks for the quick response, I am not currently using turbolinks.

@justin808
Copy link
Member

@justin808
Copy link
Member

@walidargh any update? Can we close this issue?

@justin808
Copy link
Member

@walidargh I'd like to get this issue closed if possible.

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

No branches or pull requests

2 participants