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 Roadmap from './roadmaps/[roadmap]'; |
||||||
import roadmapsList from "../data/roadmaps.json"; |
import roadmapsList from "../data/roadmaps.json"; |
||||||
|
import { serverOnlyProps } from '../lib/server'; |
||||||
|
|
||||||
// Fallback page to handle the old roadmap pages implementation
|
// Fallback page to handle the old roadmap pages implementation
|
||||||
const OldRoadmap = () => { |
const OldRoadmap = ({ roadmap }) => { |
||||||
const router = useRouter(); |
if (roadmap) { |
||||||
const { fallback } = router.query; |
return <Roadmap roadmap={ roadmap } /> |
||||||
|
|
||||||
// Render the roadmap if it exists, otherwise 404
|
|
||||||
const roadmapExists = !!roadmapsList.find(roadmap => roadmap.slug === fallback); |
|
||||||
if (roadmapExists) { |
|
||||||
return <Roadmap roadmap={ fallback } /> |
|
||||||
} |
} |
||||||
|
|
||||||
return <h1>404</h1>; |
return <h1>404</h1> |
||||||
}; |
}; |
||||||
|
|
||||||
|
OldRoadmap.getInitialProps = serverOnlyProps(({ req }) => { |
||||||
|
return { |
||||||
|
roadmap: roadmapsList.find(roadmap => roadmap.slug === req.url), |
||||||
|
}; |
||||||
|
}); |
||||||
|
|
||||||
|
|
||||||
export default OldRoadmap; |
export default OldRoadmap; |
@ -1,27 +1,24 @@ |
|||||||
import { useRouter } from 'next/router'; |
import roadmaps from "../../data/roadmaps"; |
||||||
import DefaultLayout from '../../layouts/default/index'; |
import DefaultLayout from '../../layouts/default/index'; |
||||||
import PageHeader from '../../components/page-header/index'; |
import PageHeader from '../../components/page-header/index'; |
||||||
// import roadmaps from "../../data/roadmaps";
|
import { serverOnlyProps } from '../../lib/server'; |
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
const Roadmap = ({ roadmap }) => { |
||||||
return ( |
return ( |
||||||
<DefaultLayout> |
<DefaultLayout> |
||||||
<PageHeader /> |
<PageHeader /> |
||||||
<div className="container"> |
<div className="container"> |
||||||
{/*<img src={ roadmap.picture } alt="" />*/} |
<img src={ roadmap.picture } alt="" /> |
||||||
</div> |
</div> |
||||||
</DefaultLayout> |
</DefaultLayout> |
||||||
); |
); |
||||||
}; |
}; |
||||||
|
|
||||||
|
Roadmap.getInitialProps = serverOnlyProps(({ req }) => { |
||||||
|
const normalizedUrl = req.url.replace('roadmaps/', ''); |
||||||
|
return { |
||||||
|
roadmap: roadmaps.find(roadmap => roadmap.slug === normalizedUrl), |
||||||
|
}; |
||||||
|
}); |
||||||
|
|
||||||
export default Roadmap; |
export default Roadmap; |
Loading…
Reference in new issue