Next.js Discord

Discord Forum

Store global variables from the server on client?

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I have some of the settings in database that should be used in my next.js client application. For example something like this:

const phoneNumber = "123/456-7891"
const menu = [item1, item2, item3]

My client will not change his menu or phone number often but it may happen so that's the reason why I will keep it in a database instead of hardcoding it. I am struggling to think of a pattern that will not call fetch for every request, and page navigation, but still preserve the self-updating when these values change.

What is the best way to sort of fetch this thing when the user first connects to a web-page and only sometimes to update it.
Answered by B33fb0n3
You can fetch the stuff normally using fetch or however you fetch that data. Then apply „force-static“ to it. Like that, only one version of the page will be created and no additional call will be made. If you client updates this data now, invalidate that path and a new version (with the new data) will be fetched
View full answer

3 Replies