bankai icon indicating copy to clipboard operation
bankai copied to clipboard

Asynchronously init choo stores before list routes

Open tornqvist opened this issue 6 years ago • 2 comments

This is a 🙋 feature

Checklist

  • [x] tests pass

Context

This allows for asynchronously registering routes using choo stores. There's been a lot of talk lately about async render in choo (https://github.com/choojs/choo/pull/646) and I believe we should look into async initialization at the same time. This PR would allow us to start experimenting with that without having to commit to breaking changes in the API.

Here's an example choo store which, when registered, adds a promise to the _experimental_prefetch array which resolves once it has fetched all posts from some CMS and added them as routes to the app. These routes are included in static builds and enjoy proper SSR.

module.exports = store

function store (state, emitter, app) {
  state._experimental_prefetch.push(
    getPostsFromCMS().then(function (posts) {
      posts.forEach(function (post) {
        // add route for each post
        app.route(`/posts/${post.slug}`, require('../views/post'))
      })
    })
  )
}

This depends on https://github.com/choojs/choo/pull/649 which allows for custom state to be passed into stores.

Semver Changes

Minor

tornqvist avatar Mar 22 '18 13:03 tornqvist

gotta bump this up my queue. i should get to it thursday morning!

goto-bus-stop avatar Sep 25 '18 21:09 goto-bus-stop

Ohh, I had forgotten about this. I'll try and resolve the conflicts sometime later this week.

tornqvist avatar Aug 15 '19 09:08 tornqvist