parent
3e71b7aca8
commit
0fa1a6642c
5 changed files with 47 additions and 33 deletions
@ -0,0 +1,14 @@ |
||||
/** |
||||
* Makes sure that the props are fetched only on server and not in browser |
||||
* @param callback |
||||
* @returns {Function} |
||||
*/ |
||||
export const serverOnlyProps = (callback) => { |
||||
return async (props) => { |
||||
if (process.browser) { |
||||
return __NEXT_DATA__.props.pageProps; |
||||
} |
||||
|
||||
return await callback(props) |
||||
}; |
||||
}; |
@ -1,19 +1,21 @@ |
||||
import { useRouter } from 'next/router'; |
||||
import Roadmap from './roadmaps/[roadmap]'; |
||||
import roadmapsList from "../data/roadmaps.json"; |
||||
import { serverOnlyProps } from '../lib/server'; |
||||
|
||||
// Fallback page to handle the old roadmap pages implementation
|
||||
const OldRoadmap = () => { |
||||
const router = useRouter(); |
||||
const { fallback } = router.query; |
||||
|
||||
// Render the roadmap if it exists, otherwise 404
|
||||
const roadmapExists = !!roadmapsList.find(roadmap => roadmap.slug === fallback); |
||||
if (roadmapExists) { |
||||
return <Roadmap roadmap={ fallback } /> |
||||
const OldRoadmap = ({ roadmap }) => { |
||||
if (roadmap) { |
||||
return <Roadmap roadmap={ roadmap } /> |
||||
} |
||||
|
||||
return <h1>404</h1>; |
||||
return <h1>404</h1> |
||||
}; |
||||
|
||||
OldRoadmap.getInitialProps = serverOnlyProps(({ req }) => { |
||||
return { |
||||
roadmap: roadmapsList.find(roadmap => roadmap.slug === req.url), |
||||
}; |
||||
}); |
||||
|
||||
|
||||
export default OldRoadmap; |
@ -1,27 +1,24 @@ |
||||
import { useRouter } from 'next/router'; |
||||
import roadmaps from "../../data/roadmaps"; |
||||
import DefaultLayout from '../../layouts/default/index'; |
||||
import PageHeader from '../../components/page-header/index'; |
||||
// import roadmaps from "../../data/roadmaps";
|
||||
|
||||
const Roadmap = (props) => { |
||||
const router = useRouter(); |
||||
const { |
||||
roadmap: slug, |
||||
} = router.query; |
||||
|
||||
console.log(router); |
||||
|
||||
// @todo handle 404
|
||||
// const roadmap = roadmaps.find(roadmap => roadmap.slug === slug);
|
||||
import { serverOnlyProps } from '../../lib/server'; |
||||
|
||||
const Roadmap = ({ roadmap }) => { |
||||
return ( |
||||
<DefaultLayout> |
||||
<PageHeader /> |
||||
<div className="container"> |
||||
{/*<img src={ roadmap.picture } alt="" />*/} |
||||
<img src={ roadmap.picture } alt="" /> |
||||
</div> |
||||
</DefaultLayout> |
||||
); |
||||
}; |
||||
|
||||
Roadmap.getInitialProps = serverOnlyProps(({ req }) => { |
||||
const normalizedUrl = req.url.replace('roadmaps/', ''); |
||||
return { |
||||
roadmap: roadmaps.find(roadmap => roadmap.slug === normalizedUrl), |
||||
}; |
||||
}); |
||||
|
||||
export default Roadmap; |
Loading…
Reference in new issue