Last active
October 13, 2021 17:20
-
-
Save stefanoverna/e8e138bf16adc02b1243f184986bbe6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import withLayoutProps from 'utils/withLayoutProps'; | |
export const getStaticProps = withLayoutProps(async ({ params: { id } }) => { | |
const pokemon = await api.getPokemonById(id); | |
return { | |
props: { pokemon }, | |
}; | |
}); | |
export default function PokemonDetail({ pokemon, layoutProps }) { | |
return ( | |
<Layout {...layoutProps}> | |
<div className="container"> | |
<Link href="/"> | |
<a>back</a> | |
</Link> | |
<Pokemon {...pokemon} /> | |
</div> | |
</Layout> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import path from 'path'; | |
const CACHE_PATH = path.join(__dirname, '.layout-props-cache'); | |
async function fetchLayoutProps() { | |
// qui chiamate Dato per ottenere le informazioni | |
// che vi servono per il layout! | |
return { foobar: 1 }; | |
} | |
const maybeFetchLayoutProps = async () => { | |
try { | |
return JSON.parse(fs.readFileSync(CACHE_PATH, 'utf8')); | |
} catch (error) { | |
const layoutProps = await fetchLayoutProps(); | |
fs.writeFileSync(CACHE_PATH, JSON.stringify(layoutProps), 'utf8'); | |
return layoutProps; | |
} | |
}; | |
function withLayoutProps(originalGetStaticProps = null) { | |
return async function newGetStaticProps(...args) { | |
const result = originalGetStaticProps ? await originalGetStaticProps(...args) : { props: {} }; | |
result.props.layoutProps = await maybeFetchLayoutProps(); | |
return result; | |
}; | |
} | |
export default withLayoutProps; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment