Amanda
Amanda

Reputation: 23

Nextjs only go to page on button click not by typing it directly into the browser

In Next.js with SSR: I need to implement something so that when a user directly types in a certain link into the browser like: 'www.website.com/helloworld' the user will automatically be redirected to the home page. I only want the user to be able to get to that page after they click a certain button (specifically after filling in a form and clicking submit). Can someone point me into the right direction on how to solve this?

Upvotes: 1

Views: 100

Answers (1)

QurcoJr
QurcoJr

Reputation: 84

You also can redirect user on SSR, from your protected page get appContext from page's getInitialProps and use its appContext.ctx.res.writeHead method.

appContext.ctx.res.writeHead(307, { Location: '/' }).end()

Upvotes: 1

Related Questions