1
1
mirror of https://github.com/c8r/x0.git synced 2024-09-11 13:45:52 +03:00

Adjust static routing

This commit is contained in:
Brent Jackson 2018-05-21 22:23:33 -04:00
parent 013210f5aa
commit ae8c55c391
3 changed files with 28 additions and 9 deletions

20
docs/posts.js Normal file
View File

@ -0,0 +1,20 @@
import React from 'react'
export default class extends React.Component {
static getInitialProps = async () => {
return {
routes: [
{ path: '/posts/beep' },
{ path: '/posts/boop' },
],
path: '/posts/:name'
}
}
render () {
const { name } = this.props.match.params
return (
<h1>{name}</h1>
)
}
}

View File

@ -120,22 +120,21 @@ const remove = filename => {
}
const getRoutes = async (App) => {
const baseRoutes = await App.getRoutes()
const routes = await App.getRoutes()
// todo clean up
const dynamicRoutes = []
baseRoutes.forEach(route => {
routes.forEach(route => {
if (route.props.routes) {
route.props.routes.forEach(subroute => dynamicRoutes.push(
Object.assign({}, route, subroute)
))
}
})
const routes = [
...baseRoutes.filter(route => !route.props.routes),
const staticRoutes = [
...routes.filter(route => !route.props.routes),
...dynamicRoutes
]
return routes
return { routes, staticRoutes }
}
module.exports = async (opts) => {
@ -160,10 +159,10 @@ module.exports = async (opts) => {
if (!fs.existsSync(opts.tempdir)) fs.mkdirSync(opts.tempdir)
const App = await getApp(opts)
const routes = await getRoutes(App)
const { routes, staticRoutes } = await getRoutes(App)
const template = createTemplate(opts)
const pages = routes.map(route => renderHTML(
const pages = staticRoutes.map(route => renderHTML(
Object.assign({}, route, {
opts,
App,

View File

@ -63,7 +63,7 @@ export const getRoutes = async (components = initialComponents) => {
const props = Component.getInitialProps
? await Component.getInitialProps({ path: pathname })
: {}
if (IS_CLIENT) pathname = props.path || pathname
pathname = props.path || pathname
return {
key: name,
name,