nextjs initializes some data after the service starts, only once,and does not re-execute as the page
Answered
Air Penguin posted this in #help-forum
I have some data that needs to be pulled after the service is started, and there will be a polling retrieve mechanism for a fixed period of time. Is there such a mechanism to implement in nextjs, it will not be re-executed as the user refreshes the page
Answered by B33fb0n3
I mean, that curl can create a request every X hours to your server. Then the server does his job (whatever this is in your case). Of course send some secret inside the header to be safe 👍
17 Replies
... after the service is startedso you are on clientside? If so, use a clientside fetching library like SWR or react query
No, I am that the server needs to obtain some data from other services after startup, and will do a scheduled task to periodically pull this data.
when using it on the server and loading it via SSR the server will only be active during the request time. So it builds everything together and send it once to the client (or stream it).
To periodically pull data it must be a client component that creates a new call to the server in a specific period. Of course this "request" can also be made by the server for example using cron in combination with curl. Other services that replace cron are also possible
To periodically pull data it must be a client component that creates a new call to the server in a specific period. Of course this "request" can also be made by the server for example using cron in combination with curl. Other services that replace cron are also possible
。
Yes, I found the Custom server part after checking the nextjs document, which basically met my needs. However, during debugging, I found that the running environment of custom server would be incompatible with the middleware edge environment of the original project, so I had to consider other ways
... so I had to consider other wayscan you tell us what other ways? Others might be interested in your solution as well
Since custom server can associate two services by importing next from 'next ', this doesn't seem to work due to middleware issues (or maybe I haven't found a solution). All I can think of is opening a separate service, which will run independently, but with that, there will be more resources to maintain, which will be more unstable. So to be honest, we're still working on a better way...
hmm understandable. What do you think about my two solutions that I provided here: https://nextjs-forum.com/post/1317306626771779604#message-1317445873978052700 <---- click
?
?
I am not sure if I can use the above scheme, my scenario is that I have a very large configuration content (AB test), I need my service to pull other services, store in the server's memory, re-pull data every 24 hours, and update the data in the memory, when the user visits our website, the server will follow the request header, Through a series of rules sent to the client, in fact, AB test
So it's really about solving next's service pulling configurations on other services, because as you said: when using it on the server and loading it via SSR the server will only be active during the request time. So it builds everything together and send it once to the client (or stream it).
I need a time that will only be executed once (like when the next service starts)
I need a time that will only be executed once (like when the next service starts)
Of course this "request" can also be made by the server for example using cron in combination with curl. Other services that replace cron are also possiblethen I only see an extra service as solution. If you using docker this is a just a simple docker container
You mean to write the logic that curl uses to fetch data into docker?
I mean, that curl can create a request every X hours to your server. Then the server does his job (whatever this is in your case). Of course send some secret inside the header to be safe 👍
Answer
get it, I'll have a try.
when will you try it?
@Air Penguin solved?