Skip to content

Instantly share code, notes, and snippets.

@JPWallhorn
Created April 29, 2020 06:47
Show Gist options
  • Save JPWallhorn/3d2f091815f6c6b9737b8c97b4480b75 to your computer and use it in GitHub Desktop.
Save JPWallhorn/3d2f091815f6c6b9737b8c97b4480b75 to your computer and use it in GitHub Desktop.
MyApp.getInitialProps = async ({ Component, ctx }) => {
try {
// Retrieve content documents
let pageProps = {};
let navContent = {};
let contactContent = {};
navContent = await Client.getSingle('nav_bar');
contactContent = await Client.getSingle('contact_form');
if (Component.getServerSideProps) {
pageProps = await Component.getServerSideProps(ctx);
return {
navContent: navContent.data,
contactContent: contactContent.data,
pageProps
};
}
return {
navContent: navContent.data,
contactContent: contactContent.data
};
} catch (error) {
console.error(error);
return error;
}
};
// You can use any data fetching library
import fetch from 'node-fetch'
// posts will be populated at build time by getStaticProps()
function Blog({ posts }) {
return (
<ul>
{posts.map(post => (
<li>{post.title}</li>
))}
</ul>
)
}
// This function gets called at build time in the Node.js environment.
// It won't be called on client-side, so you can even do
// direct database queries. See the "Technical details" section.
export async function getStaticProps() {
// Call an external API endpoint to get posts.
const res = await fetch('https://.../posts')
const posts = await res.json()
// By returning { props: posts }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts
}
}
}
export default Blog
import { Home } from '../src/pages'
export default { Home, getStaticProps };
export { Home as default, getStaticProps } from '../src/pages'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment