c8r/x0

Programmatic routes for static builds

johno opened this issue · 1 comments

johno commented

Currently we only support specifying routes under the x0 key in the package.json. There are scenarios where we want to build all possible routes as part of the build process.

For example, consider the following:

import React from 'react'
import fetch from 'isomorphic-fetch'

import {
  StaticRouter,
  BrowserRouter,
  Route
} from 'react-router-dom'

const Router = typeof document === 'undefined' ? StaticRouter : BrowserRouter

const Blog = props =>
  <Router>
    <div>
      <Route exact path='/' render={() => <h1>Hello, world!</h1>} />
      <Route path='/posts/:post' render={() => <h1>Post</h1>} />
    </div>
  </Router>

Blog.getInitialProps = async () => {
  const posts = await fetch('https://example.com/foo/bar')
  return { posts }
}

export default Blog

Ideally we'd be able to run a prebuild script which fetches any routes and writes to a json file that x0 can optionally read during a static build. This would handle scenarios where we have dynamic routing data we want static builds for. So, upon build, any current posts would have their respective pages generated.

Closing this in favor of #70 since the API has changed since this was opened