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

Need to update docs on react-router and server rendering. #809

Closed
justin808 opened this issue Apr 18, 2017 · 4 comments
Closed

Need to update docs on react-router and server rendering. #809

justin808 opened this issue Apr 18, 2017 · 4 comments

Comments

@justin808
Copy link
Member

justin808 commented Apr 18, 2017

Code for server rendering a react component does nothing with the redirectLocation. The problem is that the redirect location cannot be used once we're in the rendering from the controller.

Doc problems to fix

  1. This page needs updating: react-router.md. This code example does not reflect the v4.x react-router docs.
  2. The only purpose of setting the redirectLocation is that it prints an informational message on the server. The client HTML still renders.
  3. The code from react-router will do the redirect.

Questions:

  1. Should we remove the redirectLocation option since it doesn't do anything? Or should we have it render a script tag with window.location = redirectLocation?

export default function serverRenderReactComponent(options) {

export default function serverRenderReactComponent(options) {
  const { name, domNodeId, trace, props, railsContext } = options;

  let htmlResult = '';
  let hasErrors = false;

  try {
    const componentObj = ComponentRegistry.get(name);
    if (componentObj.isRenderer) {
      throw new Error(`\
Detected a renderer while server rendering component '${name}'. \
See https://github.com/shakacode/react_on_rails#renderer-functions`);
    }

    const reactElementOrRouterResult = createReactElement({
      componentObj,
      domNodeId,
      trace,
      props,
      railsContext,
    });

    if (isCreateReactElementResultNonReactComponent(reactElementOrRouterResult)) {
      // We let the client side handle any redirect
      // Set hasErrors in case we want to throw a Rails exception
      hasErrors = !!reactElementOrRouterResult.routeError;

      if (hasErrors) {
        console.error(
          `React Router ERROR: ${JSON.stringify(reactElementOrRouterResult.routeError)}`,
        );
      }

      if (reactElementOrRouterResult.redirectLocation) {
        if (trace) {
          const redirectLocation = reactElementOrRouterResult.redirectLocation;
          const redirectPath = redirectLocation.pathname + redirectLocation.search;
          console.log(`\
ROUTER REDIRECT: ${name} to dom node with id: ${domNodeId}, redirect to ${redirectPath}`,
          );
        }
        // For redirects on server rendering, we can't stop Rails from returning the same result.
        // Possibly, someday, we could have the rails server redirect.
      } else {
        htmlResult = reactElementOrRouterResult.renderedHtml;
      }
    } else {
      htmlResult = ReactDOMServer.renderToString(reactElementOrRouterResult);
    }
  } catch (e) {
    hasErrors = true;
    htmlResult = handleError({
      e,
      name,
      serverSide: true,
    });
  }

CC: @robwise @alexfedoseev

@justin808 justin808 added the bug label Apr 18, 2017
@justin808
Copy link
Member Author

https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/package.json#L88 uses Router v4 with React on Rails v9.

Does anybody want to take a crack at a PR to update the docs on this?

@NinjaAwesome
Copy link

NinjaAwesome commented Apr 19, 2019

@justin808 I am Gezane Vega from Upwork.
In my opinion, it is necessary versioning up.
In Package.json file, it shold edit react version and add react-router.

@Handy117
Copy link

Handy117 commented Apr 22, 2019

@justin808
Server-side rendering, namely, renders the React components on the server. The output is HTML content.
window.location is client-side rendering.
Perhaps, window.location is not fine.
In my opinion;
there is some ways, one of all is by create a fresh Redux Store on every request.

How about my opinion?
Thanks.

@shahidjutt72
Copy link

@justin808 i am shahid nawaz from upwork.

From opinion we should not use 'window.location' as this is not a good approach to use window, document or some JQuery code. By doing some R&D i suggest to follow this.

http://redux.js.org/docs/recipes/ServerRendering.html

Thanks

@Judahmeek Judahmeek closed this as not planned Won't fix, can't repro, duplicate, stale Jan 14, 2023
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

5 participants