Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

Need instructions on how to make things work with prererendering #6

Open
catmando opened this issue Oct 18, 2016 · 2 comments
Open

Comments

@catmando
Copy link
Contributor

@adamcreekroad can you help with this... how do you do it at catprint / change requests app

@adamcreekroad
Copy link
Contributor

To use the router with prerendering, you have to use memory history while prerendering. To do this you will have to pass your path up from the rails controller, and then redefined the history memory to use the correct history.

For example:

module Components
  class Router < React::Router
    param :url_path

    def history
      if React::IsomorphicHelpers.on_opal_client?
        browser_history
      else
        path = params.url_path
        `ReactRouter.createMemoryHistory(#{path});`
      end
    end
  end
end

This would be awesome to just add this in to the reactrb-router code, but here we are dependent on the url param being passed down from the Rails controller.

@catmando
Copy link
Contributor Author

catmando commented Oct 18, 2016

So to clarify the above... you would also have to pass the current_path from the controller/layout through your components to the router.

i.e. <% render_component "App", path: @path %>

I believe we can fully automate this approx like this:

... in React::Router class ...
include IsomorphicHelpers
class << self
  attr_reader :path
end
before_first_mount do |context|
  # capture the path from the controller if we are on the server
 @path = context.controller.request['path'] if on_opal_server?
end
...

# later in browser_history definition:

def browser_history
  if Router.on_opal_server?
    path = Router.path.gsub... blah blah (strip it down to whatever part of the path the history deals with
    `ReactRouter.createMemoryHistory(#{path})`
  else
     ... do the normal stuff
  end
end

# repeat for the other predefined history methods...

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

No branches or pull requests

2 participants