Created
April 29, 2020 06:47
-
-
Save JPWallhorn/3d2f091815f6c6b9737b8c97b4480b75 to your computer and use it in GitHub Desktop.
This file contains 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
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; | |
} | |
}; |
This file contains 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
// 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 |
This file contains 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 { Home } from '../src/pages' | |
export default { Home, getStaticProps }; |
This file contains 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
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