Skip to content

Conversation

TyMick
Copy link
Contributor

@TyMick TyMick commented Jun 13, 2020

Hey! Had some ideas for client-side redirects, since my site runs on GitHub Pages and can't use the server-side stuff.

  1. 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 inside getInitialProps's if (res) block just fine but then fails on res.writeHead, saying it isn't a function. So I changed the condition to if (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.

  2. Because getInitialProps doesn't make it to the client after a static export, I moved the Router.push lines inside a componentDidMount. I also added a couple lines to use window.location.href for external URLs instead of Router.push, since the Next.js docs say that's a better option.

  3. 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.

  4. I also added a canonical link when statusCode is 301 (or not set) so search engines know where to send the link juice when there's no server to send an actual 301 code.

  5. 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 explicit children 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?

@pablopunk
Copy link
Owner

Hey! Thanks for the PR, everything looks good! I actually had this issue in the past while trying to export the page but I don't really use that feature anymore.

Everything sounds great, I'll merge it and look into the typescript issue, thanks!

@pablopunk pablopunk merged commit 0ccca9b into pablopunk:master Jun 14, 2020
@pablopunk
Copy link
Owner

@tywmick turns out the typescript error goes away installing react types yarn add --dev @types/react 🤦

@TyMick
Copy link
Contributor Author

TyMick commented Jun 15, 2020

@pablopunk Ah, I should have thought of that. 😂 Cool!

@TyMick TyMick deleted the extend-client-side branch June 15, 2020 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants