diff --git a/docs/dynamic.js b/docs/dynamic.js new file mode 100644 index 0000000..3e76460 --- /dev/null +++ b/docs/dynamic.js @@ -0,0 +1,37 @@ +import React from 'react' +import { Link } from 'react-router-dom' + +const routes = [ + { path: '/dynamic' }, + { path: '/dynamic/hello' }, + { path: '/dynamic/hi' }, +] + +export default class extends React.Component { + static getInitialProps = async ({ path }) => { + let title = 'dynamic' + switch (path) { + case '/dynamic/hello': + title = 'hello' + break + case '/dynamic/hi': + title = 'hi' + break + } + return { + routes, + path: '/dynamic/:id*', + title, + } + } + + render () { + return
+
dynamic routing
+ Home + Dynamic Routes + Hello + Hi +
+ } +} diff --git a/lib/build.js b/lib/build.js index fef193d..bc9e2b0 100644 --- a/lib/build.js +++ b/lib/build.js @@ -125,16 +125,29 @@ const getRoutes = async (App) => { const routes = await App.getRoutes() const dynamicRoutes = [] - routes.forEach(route => { - if (route.props.routes) { - route.props.routes.forEach(subroute => dynamicRoutes.push( - Object.assign({}, route, subroute) - )) - } - }) + await Promise.all( + routes.map(async route => { + if (!route.props.routes) return null + await Promise.all( + route.props.routes.map(async subroute => { + const pageProps = typeof route.Component.getInitialProps === 'function' + ? await route.Component.getInitialProps(subroute) + : {} + dynamicRoutes.push( + Object.assign({}, route, subroute, { + props: Object.assign({}, route.props, pageProps) + }) + ) + return + }) + ) + return + }) + ) + const staticRoutes = [ ...routes.filter(route => !route.props.routes), - ...dynamicRoutes + ...dynamicRoutes.filter(route => !!route) ] return { routes, staticRoutes } } diff --git a/package.json b/package.json index b2a41b8..b7f9eac 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "start": "./cli.js docs -op 8888", - "build": "./cli.js build docs --basename '/x0'", + "build": "./cli.js build docs", "test": "nyc ava", "cover": "nyc report --reporter=html --reporter=lcov" }, @@ -69,7 +69,7 @@ }, "x0": { "title": "Compositor x0", - "_basename": "/x0", + "basename": "/x0", "meta": [ { "name": "description", "content": "Zero-config React development environment and static site generator" }, { "name": "twitter:card", "content": "summary" },