简体   繁体   English

Next.js - _app.js state 在页面刷新/SSR 时丢失

[英]Next.js - _app.js state is lost on page refresh / SSR

Not sure if this is a feature or a side-effect of rendering server side but any state stored in the exposed _app.js page is lost whenever I either refresh page or visit page directly from url input.不确定这是渲染服务器端的功能还是副作用,但是每当我刷新页面或直接从 url 输入访问页面时,存储在暴露的 _app.js 页面中的任何 state 都会丢失。 Next.js proclaim the _app.js page is for "Keeping state when navigating pages", so this should work. Next.js 声明 _app.js 页面用于“在导航页面时保持 state”,所以这应该有效。

All state is kept when routing client side with the <Link> component.当使用<Link>组件路由客户端时,所有 state 都会保留。

I'm trying to store non-sensitive data without using cookie/session/local storage.我正在尝试在不使用 cookie/会话/本地存储的情况下存储非敏感数据。

Can anyone validate if this is the best approach or should I just be using one of these techniques?任何人都可以验证这是否是最好的方法,还是我应该只使用其中一种技术?

Happy to post code if necessary.如有必要,很高兴发布代码。

Next.js apps uses SRR that means your application is universal. Next.js 应用程序使用 SRR,这意味着您的应用程序是通用的。 When you are navigating through pages without refresh this occurs in client-side, when you refresh your page this occurs in the server-side.当您在没有刷新的情况下浏览页面时,这发生在客户端,当您刷新页面时,这发生在服务器端。 You can use cookies to persist the data what you need because you can access the cookies both client and server, so when you refresh the page you can check if the cookie exist and do some stuff according to this.您可以使用 cookie 来持久化您需要的数据,因为您可以访问客户端和服务器的 cookie,因此当您刷新页面时,您可以检查 cookie 是否存在并根据此执行一些操作。

I was in the same situation as you.我和你的情况一样。 So I wrote a boilerplate example of this.所以我写了一个样板示例。 Go visit the example and the code to implement this in your app.去访问示例和代码以在您的应用程序中实现它。 https://github.com/fazlulkarimweb/with-next-redux-wrapper-redux-persist https://github.com/fazlulkarimweb/with-next-redux-wrapper-redux-persist

反例

There are no official examples from the Next community for this for now. Next 社区目前还没有关于此的官方示例。 I think people who are trying to find a solution will be helped by the boilerplate.我认为那些试图找到解决方案的人会得到样板的帮助。

If your application is large enough, then use redux for state management.如果您的应用程序足够大,那么使用 redux 进行状态管理。 For getting back states on page refresh, you can use redux-persist which can be integrated with next-redux-wrapper.为了在页面刷新时恢复状态,您可以使用 redux-persist,它可以与 next-redux-wrapper 集成。 Basically redux-persist save your redux store in local storage.基本上 redux-persist 将您的 redux 存储保存在本地存储中。 You can refer these links:您可以参考这些链接:

https://github.com/rt2zz/redux-persist https://github.com/kirill-konshin/next-redux-wrapper https://github.com/rt2zz/redux-persist https://github.com/kirill-konshin/next-redux-wrapper

When you refresh the page, _app.js kicks in, and its code reexecuted.当您刷新页面时, _app.js启动,并重新执行其代码。 Every state you set inside _app.is only for the current browser session.您在 _app.is 中设置的每个_app.is仅适用于当前浏览器 session。 If you want to persist the state, you have to use cookies or use Localstorage如果要持久化 state,则必须使用 cookies 或使用 Localstorage

But if you are re-routed to a new page, your app does not get rerender so _app.js code does not get executed.但是,如果您被重新路由到新页面,您的应用程序不会重新呈现,因此_app.js代码不会被执行。 That is why you might see a warning by next.js if you are navigating with <a> element.这就是为什么如果您使用<a>元素导航,您可能会看到 next.js 的警告。 "Do not use an <a> element to navigate to / . Use <Link /> ". “不要使用<a>元素导航到/ 。使用<Link /> ”。 That is why next.js proclaims:"Keeping state when navigating pages"这就是为什么 next.js 宣称:“在导航页面时保持 state”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM