Despite we ended up leaving Next.js for this and other reasons that made us decide Next.js was not the best option for our use case, we kind of mitigated this problem as follows while be sticked to it, I hope it makes sense to anyone. Also, by now maybe Next.js provides a better way to do this so I would read the Next.js docs before using my approach. Final note: I don't have access to the code anymore since I change to a different company so maybe there are some points that won't be 100% as we made it.
There is goes:
- We created a module that was responsible to request the config file and keep the results in a variable. At the moment of importing this module, we ensure that the variable is not already present in window.
__NEXT_DATA__. If it is, we recover it, if it's not, we request it to the remote server (this will be helpful in the clint side rendering).
- We created a
server.jsfile as described by Next.js docs. In this file we make the call to get the config file and store it in memory.
- In the body of the function passed to
createServerwe add the config file into the
reqobject to make it accesible to the app in the
getInitialPropsfunctions server side.
- We made sure that the getInitialProps using the config file returns it, so that it will be passed to the components as props and also serialized by Next.js and made available to the client in the
- Given that the config ended up in the
__NEXT_DATA__variable in the server, using the trick described in the step 1 makes the app not request the config for a second time.
You are correct in that anything in
_document.js will be run on every server request. Once you get your config, you can pass it down to the components in
pages as props. From there, I think you have two choices:
- Now that the app is bootstrapped, run the rest of the app as a SPA client-side. This would prevent any future SSR from happening. Obviously, you lose the benefits of SSR and the initial page load is likely longer, but then it would be snappy afterwards.
- After getting the config in
_app.js, send it as a cookie (assuming it's not too big to be a cookie?). On future requests to the server, the cookie will be sent automatically and you would check the cookie first - if it doesn't exist, get the config. If it does exist, skip that more expensive bootstrapping because the app is bootstrapped.
So I think it really depends on whether you want a single page application bootstrapped on the server but then entirely client side after that (option 1) or server side rendering per page while minimizing expensive bootstrapping (option 2).
Nothing prevents you from sending multiple cookies from the server if that makes sense for your app and bootstrapping. And remember not to make it a HTTP-Only cookie because you'll want to read that cookie client side - after all, that's what you're looking for - the client side configuration generated on the server.
- How to fetch data only once in a Next.js app and make it accesible to all the app, both in server and client
- How to fetch (Express) data ONLY once the (React) form-submitted data has been successfully received and served?
- How to fetch data only once and never again in reacthooks and redux
- How to use data stored in cache with SWR hooks, and how to make SWR fetch only one time
- How to fetch data and make a route with the same endpoint React
- How to make dynamic checkbox and post the data using fetch post?
- Fetch data and set the state. Then make the data available to the app using the context api
- How can I hide my navbar on the homepage and make it only visible once the user has logged in?
- How to programmatically add a className and a onLoad function to all the images in the project at once in React app without Jqyery
- How do I target each image seperately, and fetch API data from them, instead of collect it all at once
- Working code below to fetch all the data from my MongoDb databse but now I want to only fetch based on lets say courses category. How can I do it?
- ReactJS: how to call useEffect hook only once to fetch API data
- In React and Material-UI how do I make my Grid items take up 100% of available horizontal space w/o wrapping to the next row?
- react js how to import only the required function from a file and not the all functions
- How to fetch data only when the modal loads
- How to make the Service Worker cache data from API and update the cache when needed
- How to retrieve data from the server using fetch get method and show it in a table
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- how to properly replace axios api with fetch api and map over the received data in nodeJS?
- How to make an http call in parent component to setState and then send the state to all child components?
- how to delete all data and the update the page without refreshing using useEffect
- How to make useEffect hook call the provided effect only if all the elements in inputs change?
- How to get data from an API only once when the page is loaded using axios.get method?
- React: How do i make a hidden page, that is only accessible from a login screen, when the username and password is correct?
- How to make sure the function "window.location.reload()" only fires once instead of having an infinite loop?
- How to fetch data only on first click via react-query and then disable refetch on subsequent clicks
- Why does data from my api only display once and when I refresh the page it gives an error
- How do i make the data in the input feild of my form in next js stay after refresh of the page?
- How to Re-render Component Only Once after the data is changed?
- How to take the result from a fetch request and insert data into jsx?
More Query from same tag
- Google SignIn SDK is failing by throwing error, A non-recoverable sign in failure occurred -catch error: React Native
- radio buttons in react, ignore null value
- How to send files to Django REST Framework from React?
- How to set properties to a object within a react hook?
- React + Google Maps JS API, dynamically render markers from state
- isomorphic reactjs cdn assets
- DELETE return ERROR 405(Method Not allowed) I'm using React and ASP.Net Core
- How to get input value use React.ChangeEventHandler<HTMLInputElement> in react typescript
- How to handle multiple menu state with Material-UI Menu component?
- Why isn't react-router Link working (ReactJS + Redux)?
- sessionStorage.getItem null in react
- a React-Redux Action Update Multiple Reducers
- Problems in Grid when using material UI
- Want to redirect after first statment finish execution in react
- Material UI make 2 elements the same height
- How can I add a CORS header to this Lambda function
- Weird error after updating package.json of old project
- stopping audio when switching pages with react router
- How can I disable react-number-format input
- Clean way to get my app version in react-native
- React TransitionGroup and React.cloneElement do not send updated props
- Howler JS & React AudioContext console warning
- How to import webp image in react typescript
- Adding and removing tracks from a Spotify playlist app
- Different height columns in Material UI need fixing
- how to display a div with multiple conditions in reactjs
- Why files are not being uploaded in the back-end from react-front-end
- Reactjs TextField helperText
- How to make Sider selected key be responsive?
- How to use react-redux with HOC component to wait for data fetching?