Getting data for the page template #13769
Unanswered
JonCognioDigital
asked this question in
Help
Replies: 1 comment 7 replies
-
Yeah that's basically it. To avoid repeating too much code you can create the function that will fetch from the CMS in the MasterTemplate component and use it in a page like this: // pages/example.js
import MasterTemplate, { getMasterTemplateProps } from '../components/MasterTemplate'
const ExamplePage = ({ templateProps }) => (
<MasterTemplate {...templateProps}>
page content
</MasterTemplate>
)
export default ExamplePage
export async const getServerSideProps = () => {
const templateProps = await getMasterTemplateProps()
return {
props: { templateProps },
}
} If you don't need any more props you could make it even shorter with this: // pages/example.js
export async const getServerSideProps = masterTemplateServerSideProps; |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
Most of the pages on my site (probably all) will share the same header and footer. so my plan is to create a masterTemplate component which will be used on all pages, the page content being inserted as props.childern.
The template contains data from a CSM (category links etc.) so am I going to need to go to the CMS in the getServerProps (or getStaticProps) function of every single page and pass them down to the template?
Ideally I'd get the data inside the template component and if the template doesn't change when the user navigates between pages then the data would only be fetched ne. However this isn't possible with next because data can't be fetched in child component (not with SSR anyway).
What do other people do, get all data in every page? Or maybe cache it in the browser?
Beta Was this translation helpful? Give feedback.
All reactions