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

Add replaceRoutes on store #88

Closed
etienne-dldc opened this issue Sep 21, 2016 · 3 comments
Closed

Add replaceRoutes on store #88

etienne-dldc opened this issue Sep 21, 2016 · 3 comments

Comments

@etienne-dldc
Copy link

etienne-dldc commented Sep 21, 2016

Hi,

Have you ever though about a replaceRoutes method on the store ?
The case study I have in mind is code splitting. In fact this case study is not only in my mind :
I am working on a web app (Typescript / React / Redux) and I'm currently switching from react-router-redux to redux-little-router (because it will simplify a lot of stuff). With react-router the routes object wasn't split because react-router need all routes to boot.
So with redux-little-router, I can do the same thing but I feel that maybe a replaceRoutes is possible.
What we could get is something like this :

  • You first pass a initial route object to redux-little-router with one (or more) route ending with * (like myapp/private/*).
  • Then in the render that match this route, we fetch a some new routes, compute a new routes object by replacing the * by some sub routes and then call store.replaceRoutes(newRoutes) that replace the route object internally and dispatch a route event.

This would allow to:

  • expose only public routes of the app
  • isolate routes in each split part.
  • make hot reloading possible
@tptee
Copy link
Contributor

tptee commented Sep 21, 2016

Ooh, I dig this idea! I'll let you know when I start exploring it 👍

@etienne-dldc etienne-dldc changed the title replaceRoutes on store Add replaceRoutes on store Sep 21, 2016
@dpwrussell
Copy link

dpwrussell commented Oct 5, 2016

I implemented something to do exactly this a while ago, but I ran into some issues because of #96 initially, and even once I had patched middleware back in, it still is not sufficient to completely resolve this.

This is what I did:

I implemented some middleware to do split routes where a request which hits a route with a certain property would be intercepted, the bundle loaded and then on successful load, would replay the action. The problem with this is that it is difficult to access routes or matchRoute from the middleware as the store accessible in the middleware does not have access to these properties.

The only way I've found to do this is to build the middleware with a closure, like this.

const middleware = routes => store => next => action => {
...
}

but then updating the route (Like this issue suggests) is tricky as you'd really need to replace store.routes, but there is no access to store to do that replace and as the routes are a closure and not accessed from the store, even if it was updated, the closure would not change unless that routes was a container object for the actual routes.

It seems like there is a need to be able to get ahold of the router (i.e. the whole shebang, routes, matcher, history instance, etc) from just about anywhere. At the moment it's only possible through closures or if you have the enhanced store in scope.

Ideas very welcome!

@tptee
Copy link
Contributor

tptee commented Jul 9, 2017

Implemented a replaceRoutes action creator in v14.2.0! You'll want to look at the advanced docs for usage.

@tptee tptee closed this as completed Jul 9, 2017
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

3 participants