illusi03
illusi03

Reputation: 51

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.

Upvotes: 0

Views: 960

Answers (1)

Ramakay
Ramakay

Reputation: 3135

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

Upvotes: 1

Related Questions