Skip to content
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

Asynchronously init choo stores before list routes #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tornqvist
Copy link
Member

This is a 🙋 feature

Checklist

  • 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 (choojs/choo#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 choojs/choo#649 which allows for custom state to be passed into stores.

Semver Changes

Minor

@goto-bus-stop
Copy link
Member

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

This was referenced Sep 25, 2018
Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol sorry for ignoring this so long. the code LGTM, but the Choo 7 upgrade introduced some merge conflicts.

@tornqvist
Copy link
Member Author

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

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

Successfully merging this pull request may close these issues.

2 participants