React-rails: react_component not rendering on browser back navigation

Created on 1 Feb 2015  路  10Comments  路  Source: reactjs/react-rails

I've tried a few different things, but I'm unable to get my React components to render when navigating using the browser back button (sometimes even the forward button).

It doesn't seem to matter whether I prerender them server-side or if I render them client-side. E.g., this:

<%= react_component('TopicLinker', {topicName: @topic.name, topicId: @topic.slug}, {prerender: true}) %>

The entire mount node just doesn't show up in the DOM when navigating using the browser back button - multiple browsers, too - Safari (latest), Chrome (latest), etc - so I don't think this is an issue with React.js itself - feels more like it has to do with react-rails.

Thoughts? I know this is vague, but I'm really not sure what I'm doing wrong (only thing I can seem to think is that Turbolinks might be breaking this somehow). Everything else with react-rails works perfect, except with browser back navigation.

Most helpful comment

Is this back? I'm seeing something that might be related.. when I click on an anchor link my component gets removed.

All 10 comments

Did some more testing, and it looks like it seems to come down to Turbolinks. If I disable Turbolinks (e.g. data-no-turbolink on the body element in my layouts), the problem sort of goes away - at least for Chrome. But on Safari, it becomes even more unpredictable.

I'd prefer to not disable Turbolinks... but I'm worried that this is going to be a limitation of react-rails.

Looks like this resolves this for Safari - it's combination of disabling Turbolinks, and the Safari window.onunload trick:

<body data-no-turbolink data-no-transition-cache>
...
<script type="text/javascript">
    window.onunload = function(){};
</script>
</body>

Yes, I think some versions of react-rails + some versions of Turbolinks have this bug:

  • when a page is restored from the Turbolinks cache, components aren't mounted

I think the solutions are:

  • Disable the turbolinks cache with Turbolinks.pagesCached(0)
  • use react-rails master + the latest version of Turbolinks

See related #89

I had similar issues and my solution was to remove the data-react-class attribute in the component rendered by react_component. Something along these lines inside the component:

componentDidMount: function() {
  $(this.getDOMNode()).parent().removeAttr('data-react-class');
},

The root of the issue seemed to be Turbolinks saving page state when leaving the page and after component unmounting in rails_ujs. I'm not sure what kind of long-term issues this might cause but seems to work so far.

The problem is with this line https://github.com/reactjs/react-rails/blob/master/lib/assets/javascripts/react_ujs.js.erb#L51 which when unmounting the React component also deletes the root node, so when coming back there is nothing on the page to mount again. I'm not sure why its doing that but if I remove that line things work for me.

Wow, I didn't realize turbolinks cached the actual _document_ before changing pages. I always assumed it cached the server's response for the URL.

Turbolinks caching the document as-is
which happens before fetching a new page

Le sigh, better sort that out

Taking a pass at it here: #190, can someone checkout that commit and confirm it solves the problem?

I've merged #190, please update to master and reopen if you experience this issue again. Thanks for reporting it!

Is this back? I'm seeing something that might be related.. when I click on an anchor link my component gets removed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justin808 picture justin808  路  27Comments

SylarRuby picture SylarRuby  路  48Comments

edelgado picture edelgado  路  14Comments

BrunoQuaresma picture BrunoQuaresma  路  16Comments

abitdodgy picture abitdodgy  路  22Comments