【问题标题】:Next JS, Back behaviour not called getInitialProps()Next JS,Back 行为未调用 getInitialProps()
【发布时间】:2020-06-22 08:29:52
【问题描述】:

我正在学习 Next js,我将 Express 用于自定义服务器,我想为 gettersetter HttpOnly Cookie 调用 getInitialProps() 方法。
当 go 在浏览器上触发返回行为时 getInitialProps() 未被调用。
有人可以帮助我或 call SSR Function getInitialProps() 的任何解决方案?比如禁用浅渲染等等。
之前谢谢。

【问题讨论】:

    标签: node.js reactjs express next.js


    【解决方案1】:

    在 Next JS 版本 9 中,您可以使用 getServerSideProps - 其工作方式如下所述

    仅在服务器端运行 getServerSideProps 仅在服务器端运行 并且永远不会在浏览器上运行。如果页面使用 getServerSideProps , 那么:

    当你直接请求这个页面时,getServerSideProps 在 请求时间,该页面将与返回的预渲染 道具。当您在客户端页面转换上请求此页面时 通过 next/link(文档)或 next/router(文档), Next.js 向服务器发送 API 请求,服务器运行 获取服务器端道具。它将返回包含结果的 JSON 运行 getServerSideProps,JSON 将用于渲染 页。所有这些工作都将由 Next.js 自动处理,所以你 只要你有 getServerSideProps 就不需要做任何额外的事情 定义。您可以使用此工具来验证 Next.js 消除了什么 客户端包。

    https://nextjs.org/docs/basic-features/data-fetching#only-runs-on-server-side

    【讨论】:

    • 感谢您的评论,对不起,我错过了与您的理解。当触发返回行为时,该方法称为 bro ?
    • 何时返回行为再次请求服务器?我已经阅读了文档。那条路线是链接
    • 顺便说一句,感谢您的回答
    • 非常感谢,该方法 getServerSideProps() 也适用于后端行为。解决了
    猜你喜欢
    • 2020-11-30
    • 2013-01-04
    • 2020-07-13
    • 2021-10-19
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多