Disable Client Side Routing in Gatsby

Viewed 2367

Is it possible to disable client side routing in Gatsby?

I'm using Gatsby to generate a static site which only has one page and will be served from AWS/S3. I'm running into an issue caused by Gatsby removing the object suffix from the URL (https://s3.amazonaws.com/top-bucket/sub-bucket/index.html becomes https://s3.amazonaws.com/top-bucket/sub-bucket/) after the page and the Gatsby runtime loads. This issue does not happen if I disable JavaScript, so I'm pretty certain it's caused by Gatsby's use of React/Reach Router.

Is there any way to disable this behavior? I know I can probably setup a redirect on S3 to handle the request to the bucket, but I'd prefer to do this at the application level, if possible.

2 Answers

This is a hack and may not work in anyone else's application or break with future releases of Gatsby, but I was able to prevent this redirect by setting window.page.path = window.location.pathname; in gatsby-browser.js. This short circuits a conditional check in production-app.js, which attempts to "make the canonical path match the actual path" and results in the (IMO) unexpected behavior referenced above.

this issue is pretty old but hope it helps someone, I used this plugin: https://github.com/wardpeet/gatsby-plugin-static-site

npm install @wardpeet/gatsby-plugin-static-site --save

And just added it in gatsby-config.js

plugins: [{
  `@wardpeet/gatsby-plugin-static-site`,
}]

Client side routing was then disabled!

Related