Refactor routes

This commit is contained in:
LordMathis 2018-12-27 12:01:10 +01:00
parent 93d2822600
commit 7670ade223
3 changed files with 32 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { Route, Switch } from 'react-router-dom'; import { Route, Switch } from 'react-router-dom';
import { Home, NotFoundWrapper } from '.'; import { NotFoundWrapper } from '.';
import { MainContainer, PostContainer } from '../containers'; import { MainContainer, PostContainer } from '../containers';
export const App = () => ( export const App = () => (

26
src/utils/routes.js Normal file
View File

@ -0,0 +1,26 @@
import { MainContainer, PostContainer } from '../containers'
import { NotFoundWrapper } from '../components';
import getData from './api'
const routes = [
{
path: '/',
exact: true,
component: MainContainer,
getData: (path = '') => getData(
path.split('/').pop()
)
},
{
path: '/post/:postname',
component: PostContainer,
getData: (path = '') => getData(
path.split('/').pop()
)
},
{
component: NotFoundWrapper,
}
]
export default routes

View File

@ -4,8 +4,13 @@ import { renderToString } from 'react-dom/server'
import { StaticRouter as Router } from 'react-router-dom' import { StaticRouter as Router } from 'react-router-dom'
import { App } from '../components/App' import { App } from '../components/App'
import manifest from '../../public/static/manifest.json' import manifest from '../../public/static/manifest.json'
import routes from './routes'
function serverRender(req, res) { function serverRender(req, res) {
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
let markup = ''; let markup = '';
let status = 200; let status = 200;