-
Notifications
You must be signed in to change notification settings - Fork 597
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
Separate route matching from rendering #613
Separate route matching from rendering #613
Conversation
index.js
Outdated
this.state.query = nanoquery(location) | ||
var html = this.router(location) | ||
var href = location.replace(/\?.+$/, '') | ||
this._matchRoute(href, location) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could move this into the pushState
/popState
/replaceState
methods.
But there might be other side effects if we do that.
e.g.
var route = this._matchRoute(href)
self.emitter.emit(self._events.NAVIGATE, route)
or this (if we keep the mutation of state
)
this._matchRoute(href)
self.emitter.emit(self._events.NAVIGATE)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should leave this the way it is for this PR, and consider the changes in a seperate PR? That might keep the changes nicely contained, and easier to follow along.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I completely agree here. This definitely needs more elaboration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question here was whether we should prevent _matchRoute
from mutating the state. It should only return the new params and mutate the state after the NAVIGATE
event got triggered.
This would allow us to keep the current state but already pass route params to some function to preload something.
This could end up in something similar to the angular resolve
object where it's possible to preload some resources based on route params. In there we could also cancel the route change if a resolve
gets rejected.
Maybe this would make everything more complicated. I'll think about it and will create a new issue if I think it's a good idea in the longer term. Here's a link to the resolve reference documentation in case you don't know it:
https://angular.io/api/router/Resolve
And a few more readable examples:
https://www.codementor.io/max_diachenko/angular-2-route-resolves--typescript-du1089pdm
https://medium.com/@dazcyril/resolving-data-in-angular-2-4-and-5-refactoring-components-and-moving-to-ngrx-store-15e15d8be3dd#9968
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! Yeah, also all for deprecating the curry
statement in nanorouter
.
btw. Don't merge just yet 😉 |
index.js
Outdated
|
||
Choo.prototype._prerender = function (state) { | ||
var routeTiming = nanotiming("choo.prerender('" + state.route + "')") | ||
var res = state._handler(state, this.emitter.emit.bind(this.emitter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we cache that this.emitter.emit.bind(this.emitter)
variable somewhere? e.g. in the Choo constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that'd be great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Directly as choo.emit
? Then we would have choo.emitter
and choo.emit
.
993867a
to
049d57e
Compare
This is ready now for another review. We could wait for some hours until choojs/nanorouter#13 is merged. With that we would have a simplified nanorouter module by removing the curry option. I'd also consider renaming the new methods to make the code more readable. Better names for the Let me also know if you want to make the For the @@ -189,7 +189,7 @@ Choo.prototype.toString = function (location, state) {
var href = location.replace(/\?.+$/, '')
this._matchRoute(href, location)
- var html = this._prerender(this.state)
+ var html = prerender(this.state, this.emit)
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)
return html.toString()
}
@@ -206,9 +206,9 @@ Choo.prototype._matchRoute = function (location, queryString) {
return this.state
}
-Choo.prototype._prerender = function (state, emit) {
+function prerender (choo, state) {
var routeTiming = nanotiming("choo.prerender('" + state.route + "')")
- var res = state._handler(state, this.emit)
+ var res = state._handler(state, emit)
routeTiming()
return res
} |
index.js
Outdated
|
||
Choo.prototype._matchRoute = function (location, queryString) { | ||
if (location === undefined) location = this._createLocation() | ||
if (queryString === undefined) queryString = window.location.search |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to myself: I shoud add a comment about that queryString
is only used in the browser.
Or even move it up to the function call... depending how we'd like to handle it in #609
I'm really eager to get this released! :D |
Nanorouter just got published. So this could land soon in master |
- This fixes choojs#530 - Adds support for wayfarer/nanorouter subrouters
049d57e
to
9aa1015
Compare
@yoshuawuyts this is ready to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rad!
How should we publish this? Semver... minor, right? |
@yoshuawuyts I'd argue major - this fixes #530, which is definitely not a backwards-compatible patch, as it changes behaviour that some apps may rely on... Although, choo 7 has it's own goals and releasing that early won't be fun, so minor could work. |
It could be a patch as it fixes only the state of the So to sum up, there are the following changes in this PR:
|
Sounds like it would also fix #463 😸 |
Cool, going to release as minor then! - Seems reasonable given the conversation. It's always this weird situation where one person's bug is another person's feature. |
❤️ Thanks 🎉 |
|
For future reference and people who still want to access the old state within the navigate method, |
This PR
navigate
event. Modifies the state before the render event is triggered. Therefore this fixes State out of date when switching routes #530, Fire route event after render if the url has changed #553, state.query not set until navigate #549, Navigate event should enable action on "page entry" #610.emit
as an alias to.emitter.emit
Additionally to the changes I did in here I think we should
(I guess this is done by now as nanorouter contains some electron-, and browser specific logic)
curry
support in nanorouter. People could just wrap their method by themselvesPlease let me know what you think
_handler
differently on the state if we decide to go down this path. In case we want to be able to cancel a navigate event in the future, we could also make it more stateless and pass the new route as object to the navigate events instead of modifying the state.