(I've recently started working with Next.js and inherited a project built using it, so please forgive me if this is something obvious that I'm missing)
I have a set of data that needs to be loaded server-side on each request. Initially, I had implemented this in getServerSideProps
for a single page and it worked well. However, I now need to expand this to multiple pages with shared components throughout the component hierarchy. I require a way to create a globally accessible variable that can be accessed from any component, which should also be populated server-side. Repeating the same code in getServerSideProps
for every page and passing the data as props becomes too repetitive and inefficient, so I am looking for a centralized/static/hook-based solution.
I am making an API call in middleware.js
during the request phase to retrieve the necessary data. How can I pass this data down to all components in the hierarchy? If it were purely client-side, I could use a Context/store, but I am struggling to find a way to transfer server-side data to client-side components.