question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support server side redirects

See original GitHub issue

🚀 Feature Proposal

We are using SSR with “authenticated” middleware to protect authenticated routes.

This middleware will check if user is authenticated and if not redirect him to another page, e.g. /login

The logic inside getServerSideProps might look something like this:

export const getServerSideProps: GetServerSideProps<Props> = async (
  context
) => {
  if (someSpecialCase) {
      context.res.writeHead(302, { Location: "/login" });
      context.res.end();
      return undefined;
  }
}

This means that the I would expect /login page to be returned from the getPage function.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Meemawcommented, Nov 21, 2020

Yeah, its very high level and abstract, but I think this is the experience most people would expect and would probably suit most of the use cases. I like the fact that there is no implementation details leaked, you don’t even have to know its NextJS underneath.

In theory, library could have multiple “levels” of abstraction and if more fine grained control would be needed some lower level API could be exposed (if there was some use cases for it).

Just thinking out loud: there could be some lower level API exposed that could return more stuff, e.g. page itself + “props”, “redirect”… and it implementation would be used internally by the “higher” level.

// ignore the name. This page would perhaps not be resolved to the redirect automatically but the user would have to do it in code manually
const { page, props, redirect } = await getPageLowerApi({
   route: '/private/page',
});

if(redirect) {
   const { page: redirectedPage } = await getPageLowerApi({ route: redirect })
   render(redirectedPage)
}
1reaction
Meemawcommented, Nov 21, 2020

Totally agree. That is much easier to implement and a way forward 👍

In that case I would just mention somewhere once it is implemented that only those SSR redirects are supported and should be preferred anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SVR1: Implementing automatic redirects on the server side ...
Server -side technologies provide methods to implement redirects in a way that does not confuse users. A server-side script or configuration file can...
Read more >
Best practices for client side vs. server side redirects
Redirects are to be initiated from the server side with a 301/302 response. All webapp languages/frameworks defaults to 302. You can usually ...
Read more >
Redirects & SEO - The Complete Guide - Audisto
A server side redirect happens, when the HTTP request to a web server is answered with a server status code, which indicates the...
Read more >
Client-Side and Server-Side Redirects in Next.js
js supports both client-side and server-side rendering (SSR) and unfortunately the method for redirecting is very different in both contexts.
Read more >
Redirections in HTTP - MDN Web Docs - Mozilla
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found