How to use `getServerSideProps` in persistent layouts shared by multiple pages in Next.js?

Viewed 88

I have shared layouts for several logical groups of pages, for example:

  • DashboardLayout:
    • /dashboard/foo
    • /dashboard/bar
  • UserLayout:
    • /user/settings
    • /user/profile

Both DashboardLayout and UserLayout use data that has to be fetched on the server-side (e.g. from a database server).

How can I:

  • fetch this data from the layout components when getServerSideProps only works in page components; and
  • keep the fetching logic tied to the layouts and not the page components or the App component; and
  • keep the layouts persistent (Next.js docs), i.e. prevent re-fetching/re-rendering when navigating between pages using the same layout (e.g. from /user/settings to /user/profile); and
  • ideally not use any third-party libraries nor shallow routing?
0 Answers
Related