-
Notifications
You must be signed in to change notification settings - Fork 18
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
Lazy routes support #62
Comments
This is heaps cool; personally I'm not a fan of promises but having some way of doing this would be neat I reckon - perhaps doing it through a I've never attempted code splitting before as it's a very late-stage optimization, but I believe |
Yeah, the same, I wish I had a way to do it without wrapping everything in a Promise, it makes me sad 🤕 I'll try to find an alternative. What to you mean by a I've not that much experience with browserify but it is a nice occasion to try myself at it. Oh by the way, I've managed to simplify the previous code. // from choo
function createRoute (routeFn) {
return function (route, inline, child) {
var wrapped = inline
if (typeof inline === 'function' && inline.lazy) {
const wrap_lazy = (_route) => (_child) => wrap(_child, _route)
wrapped = () => inline(wrap_lazy(route))
wrapped.lazy = true
}
else if (typeof inline === 'function') {
wrapped = wrap(inline, route)
}
return routeFn(route, wrapped, child)
}
function wrap (child, route) {
// ... no changes
}
} // from wayfarer
function emit() {
// ... unchanged code ..
if (node && node.cb && node.cb.lazy) {
args[0] = node.params
return node.cb().then(view => view.apply(null, args))
}
// ... unchanged code ...
} Usageconst lazyLoadedView = (wrap) => System.import('./dependency.js').then(module => wrap(module.view))
app.router((route) => [
route('/', view),
route('/lazy', markAslazy(lazyLoadedView)) // <-- mark view cb as lazy view
])
app.start()
.then(domTree => document.querySelector("#choo").appendChild(domTree)) The function lazy (fn) {
fn.lazy = true
return fn
} I hope this would make a transition to v4 easier. I keep updating this repo as I keep experimenting with it and when we have something satisfactory, I'll make a pull request to each package impacted by these changes 😉 |
I have been playing around with choo last night and it made me dive into the internals of choo, sheet-router, and wayfarer!
Your modules have a pretty nice api and are small enough to be easy to be grokked, good job man 😊
I have seen that the version used in choo currently is 3.1.0 whereas the latest version is 4.1.1.
I'm a avid Webpack user and as I was playing around, I managed to add support for lazy-loading of views on the router.
I ended up modifying wayfarer and sheet-router for that purpose.
Based on version 3.1.0 (used in choo), I've added a parameter called
createLazyRoute
to the sheet-router constructor:I did a slight modification on wayfarer to handle Promise<view()> :
And in the end on the choo part, I can register lazy routes as is :
Now on the user side, registering lazy routes is done like that :
Here is the dependency.js file :
All of this was possible because of the
route()
arg needed to register routes on thesheetRouter
tree.I could create a
lazyRoute()
arg that has the same behavior asroute()
but for views wrapped in a Promise.I've noticed that in v4.1.1, there is no more
route()
arg since this commit.So here I am today :
Cheers 😊
EDIT : I've added an example project if you want to have a more concrete look on what I did
https://github.com/YannickDot/lazy-routes-choo-example
The text was updated successfully, but these errors were encountered: