Extend client-side redirects #4
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey! Had some ideas for client-side redirects, since my site runs on GitHub Pages and can't use the server-side stuff.
nextjs-redirect currently doesn't work with Next's static HTML export because it runs
getInitialProps
just once on export and then doesn't make it available to the static client. During this static export, it makes it insidegetInitialProps
'sif (res)
block just fine but then fails onres.writeHead
, saying it isn't a function. So I changed the condition toif (res?.writeHead)
instead, so it'll only make it into that block if it's on the server-side but skip it during the static export.Because
getInitialProps
doesn't make it to the client after a static export, I moved theRouter.push
lines inside acomponentDidMount
. I also added a couple lines to usewindow.location.href
for external URLs instead ofRouter.push
, since the Next.js docs say that's a better option.I added a meta refresh surrounded by a
<noscript>
to the<head>
of the page for a fallback in case the client has disabled JavaScript.I also added a canonical link when
statusCode
is301
(or not set) so search engines know where to send the link juice when there's no server to send an actual 301 code.And finally, I added a redirect link within the actual
<body>
just in case a user doesn't get redirected by the other three methods.Also, the reason I put the children of
next/head
inside an explicitchildren
attribute is because TypeScript and/or Sucrase was giving me errors when I tried to add children the normal way. So I decided I didn't want to deal with it. ¯_(ツ)_/¯Thoughts? Is it too much added?