最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i store data in nextjs that is accessible from anywhere - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 3

This 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.

发布评论

评论列表(0)

  1. 暂无评论