score:4
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.js
file 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
createserver
we add the config file into thereq
object to make it accesible to the app in thegetinitialprops
functions 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
__next_data__
global javascript variable. - 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.
score:5
you are correct in that anything in getinitialprops
in _app.js
or _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.
Source: stackoverflow.com
Related Query
- 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
- Break map() function in React JS based on condition
- Marker not showing @react-google-maps/api on localhost next.js
- Read public gists from Github and storing values in Reactapp
- How to pass the props to react functional component using react table and typescript?
- Get total width of JSX Element
- Test React component with async function with mockResolvedValue - the promise is not resolved
- Attempted import error: './redux/reducers/configureStore' does not contain a default export (imported as 'configureStore')
- Get HTML from database into React
- Prevent about:blank tab from opening, should send user straight to specified URL without delay
- Making an animation between states with React Transition Group, the animations overlap with each other
- require.context from svg file gives different result
- Executing two POST Requests together
- how to configure Ant Design with Webpack 3
- Icon className doesn't update on state change
- How can I contain the styles of a Chrome Extension content script?
- How can I call function from div and render the json object returned?
- Render formatted (unminified) HTML in React (SSR with Next.js)
- Mocking Window with Sinon, Mocha, Enzyme, and React
- How to get the DOM node from a Class Component ref with the React.createRef() API
- Jest ES6 Error unexpected token import?
- Conditional className not updated when boolean updated
- Function runs forever in React Component
- React ANTD DatePicker HTMLCollection not displaying Live values when extracting elements/values
- I am trying to make my program list blog posts in order by the number of likes each blog has
- super expression must either be null or a function?? React
- Fetching 'title' from WP REST API with React
- storage eventlistener not working alongside any update to localstorage
- Make tab switching conditional
- Reactstrap - Carousel caption to right of image
- Firebase renders every document inside a collection