简体   繁体   中英

Next JS, Back behaviour not called getInitialProps()

Iam learning Next js and i use Express for custom server, and i want to call getInitialProps() method for getter or setter the HttpOnly Cookies.
When go trigger back behaviour on browser getInitialProps() is not called.
Someone can help me or any solution for call SSR Function getInitialProps() ?? Like disable shallow render maybe or etc.
Thanks before.

With Next JS version 9, you can use getServerSideProps - The way this would work is explained below

Only runs on server-side getServerSideProps only runs on server-side and never runs on the browser. If a page uses getServerSideProps, then:

When you request this page directly, getServerSideProps runs at the request time, and this page will be pre-rendered with the returned props. When you request this page on client-side page transitions through next/link (documentation) or next/router (documentation), Next.js sends an API request to the server, which runs getServerSideProps. It'll return JSON that contains the result of running getServerSideProps, and the JSON will be used to render the page. All this work will be handled automatically by Next.js, so you don't need to do anything extra as long as you have getServerSideProps defined. You can use this tool to verify what Next.js eliminates from the client-side bundle.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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