如何避免在整个NextJS应用程序中重复呼叫?
这似乎是一件非常简单的事情,但是我很难找到正确的体系结构来做到这一点。
这是场景:
- 我们有一个API路由
API/模板
,理论上应该在应用程序的每个路由/页面中发生。它获取了所有不同的模板,并且应用程序中的所有数据都属于其中一个模板。这些是动态的,可以随着时间的推移而变化,因此它们不是“导入的JSON”, - 每个页面都应该在负载上获得这些资产,但是...
- 一旦加载了这些资产,并且您开始通过页面导航,该应用程序不应重新提取它们在每个页面导航上,
- 我们将在模板更改数据库中的已加载端客户端的情况下实现套接字通知,
因此问题是,由于每个页面都需要这一点,因此SSR仍然需要能够在每个页面上访问此信息我们的SEO策略要求服务器端渲染将这些页面完全渲染发送给客户端。
因此,我们正在寻找的是:
- 要有一个“有条件的” geterversideprops,如果它是一个完整的重新加载,它会得到的,但是,如果它已经在客户端的内存中,它会跳过
- 我们研究了SWR,从理论上讲,这仍然可以使API称为事后思想,在客户方面有所帮助,但击败了不实际打电话的目标,以使后端不会因不必要的不必要而“负担” 坦白地说
,这看起来像是一种非常“常见”的模式,但我完全未能在NextJS应用程序环境中实现适当的解决方案。也许这是一个“反模式”,我们不应该这样做?
This seems like a really simple thing to do, yet I am having trouble finding the right architecture to do this.
Here's the scenario:
- We have an API route
api/templates
that should, in theory, happen in every single route/page of the App. It fetches all the different templates and all the data in the app belongs to one of those templates. These are dynamic and can change over time, so they are not an 'importable JSON' - Every page should get these assets on load, but...
- once it's loaded, and you start navigating through pages, the app should NOT re-fetch them on every single page navigation
- We will implement a socket notification to alert an already-loaded client when templates change in the database
The problem is that, since this is needed on every page, SSR still needs to be able to access this on every page and our SEO policy requires server side rendering to send these pages fully rendered to client.
So, what we are looking for is:
- to have a somewhat 'conditional' getServerSideProps that, if it is a full reload, it fetches that, but, if it is already in the client's memory, it skips that
- we have looked into SWR, which, in theory, would work, but it still makes the API call as an after-thought, helping on the client side, but defeating the objective of not actually making the call, so that the backend is not 'burdened' with an unnecessary call
Honestly, this looks like a very 'common' pattern, yet I have completely failed to achieve a proper solution within the NextJS app environment. Maybe it's an "anti-pattern" and we shouldn't be doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论