So basicly we have an initial fetch in our custom pages/_app.jsx
that first fetches sitesettings & navigation for header/footer. This data I would like to be only fetched once however it gets fetched every routechange clientside. Is there anyway to persist some data after the initial fetch in the app container?
Here's my code:
import React from 'react'
import App, { Container } from 'next/app'
import Header from '../components/Header';
import Footer from '../components/Footer';
class MyApp extends App {
static async getInitialProps({ Component, router, ctx }) {
let pageProps = {}
const res = await fetch(`http://localhost:3000/navigation`)
const nav = await res.json();
const ss = await fetch(`http://localhost:3000/site-settings`);
const settings = await ss.json();
var culture = "en-us";
var root = {};
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
if (pageProps && pageProps.pageContext) {
culture = pageProps.pageContext.culture || "en-us";
root = nav.message.find(nav => !nav.parentPage && nav.culture === culture);
}
}
return {
pageProps,
navigation: nav.message,
settings,
culture,
root
}
}
And the issue is that the getInitialProps of my _app.jsx
is being run at every route making unecessary requests to fetch data that client already have.
_document
instead – evgeni fotia