Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Last active October 13, 2021 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanoverna/e8e138bf16adc02b1243f184986bbe6a to your computer and use it in GitHub Desktop.
Save stefanoverna/e8e138bf16adc02b1243f184986bbe6a to your computer and use it in GitHub Desktop.
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>
);
}
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