admin管理员组文章数量:1316839
I have a nextjs app that needs to make api requests based on the user's inputs. Now the api requests need a token that I would like to generate at the start of the app and anytime that i need to based on the user data from getServerSideProps or something. I looked at a lot of options like saving it in cookies and session but i don't think for my use case that serves the purpose plus i would also like to learn how it should be done in nextjs. Basically I need a
const data = {}
accessible from each request on the server so that when the server gets a request it does not have to refetch the data it already has.
I have a nextjs app that needs to make api requests based on the user's inputs. Now the api requests need a token that I would like to generate at the start of the app and anytime that i need to based on the user data from getServerSideProps or something. I looked at a lot of options like saving it in cookies and session but i don't think for my use case that serves the purpose plus i would also like to learn how it should be done in nextjs. Basically I need a
const data = {}
accessible from each request on the server so that when the server gets a request it does not have to refetch the data it already has.
Share Improve this question edited May 27, 2021 at 10:24 Sakibul Alam asked May 25, 2021 at 14:10 Sakibul AlamSakibul Alam 911 gold badge1 silver badge7 bronze badges 3- 1 It sounds a lot like you're trying to implement authentication, so I would strongly suggest reading the documentation available on the NextJS website here. – Labu Commented May 25, 2021 at 14:21
- no. Iam not trying to implement any auth. Basically my users will all essentially need access to the same data. But if one user decides to update something, I want that to be updated for all users if that makes sense. – Sakibul Alam Commented May 25, 2021 at 15:15
- 2 @SakibulAlam, are you needing this data to be accessible instantly without refreshes? A better description of what you're trying to achieve (maybe provide an example of what you'd expect to happen) would help others help you. – William Park Commented May 26, 2021 at 11:37
1 Answer
Reset to default 3This is a good question.
Static data
If you have a static data, then your solution speaks for itself. Just export that so that all other files can import.
const data = {}
export default data
Client data
However in your case, you are speaking of static, but the data isn't, ex. the user info have to be resolved by one of the API and then stored somewhere as "static" from now on. So it's not static.
In case it's a client side (cookie), you can wire with that variable directly, such as in Next Auth. https://next-auth.js/getting-started/example
Server data
If it's server data, it needs to be persisted via one of your API /pages/api
. And after that yes, you can put it via getServerSideProps
to the _app.js
.
本文标签: javascriptHow do i store data in nextjs that is accessible from anywhereStack Overflow
版权声明:本文标题:javascript - How do i store data in nextjs that is accessible from anywhere - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014514a2413516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论