I’m developing a NextJS project and I’m fetching some necessary data in the getInitialProps of _app.js and setting it into cookies. However, I was wondering if there is any way to share this data with getServerSideProps of any page as for the first time I cannot access data from req.cookies in getServerSideProps.
_app.js
import cookies from 'js-cookie';
const App = ({ Component, pageProps }) => {
return <Component {...pageProps} />;
};
App.getInitialProps = async ({ ctx }) => {
const { req, res, query } = ctx;
const data = await fetchSomeData();
// Set data into cookies
cookies.set('myData', data);
return { ...appProps, data };
};
export default MyApp;
[…products].js
export async function getServerSideProps({ params, req, res, query }) {
const ids = // expecting data from intial props
const products = getProductByIds(ids)
return {
props: {
products,
},
};
}
I want to access the data which was fetched in getInitialProps to proceed further to fetch products.