Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-plugin-remove-trailing-slashes] results in 301 redirects #9025

Closed
endymion1818 opened this issue Oct 11, 2018 · 6 comments
Closed

[gatsby-plugin-remove-trailing-slashes] results in 301 redirects #9025

endymion1818 opened this issue Oct 11, 2018 · 6 comments

Comments

@endymion1818
Copy link
Contributor

Description

I am rebuilding a site that traditionally has no trailing slashes on each page. I've tried using both the plugin gatsby-remove-trailing-slashes and the method hilighted here: https://www.gatsbyjs.org/docs/creating-and-modifying-pages/#removing-trailing-slashes

Both of these result in a redirect, which you can see in the network panel.

Is there a way of removing trailing slashes without the redirect?

Steps to reproduce

Add the following to the gatsby-node file:

exports.onCreatePage = ({ page, actions }) => {
  const { createPage, deletePage } = actions
  return new Promise(resolve => {
    const oldPage = Object.assign({}, page)
    // Remove trailing slash unless page is /
    page.path = _path => (_path === `/` ? _path : _path.replace(/\/$/, ``))
    if (page.path !== oldPage.path) {
      // Replace new page with old page
      deletePage(oldPage)
      createPage(page)
    }
    resolve()
  })
}

Expected result

  • All page urls (except home page) end with the last charachter of the slug, not a trailing slash
  • Because this is the canonical URL, no 301 redirect is needed

Actual result

A 301 redirect is logged in the console:

screen shot 2018-10-11 at 11 23 48

Environment

System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.11.0 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 69.0.3497.100
Firefox: 62.0
Safari: 12.0
npmPackages:
gatsby: ^2.0.14 => 2.0.14
gatsby-plugin-canonical-urls: ^2.0.6 => 2.0.6
gatsby-plugin-google-analytics: ^2.0.5 => 2.0.6
gatsby-plugin-google-tagmanager: ^2.0.5 => 2.0.5
gatsby-plugin-manifest: ^2.0.4 => 2.0.4
gatsby-plugin-react-helmet: ^3.0.0 => 3.0.0
gatsby-plugin-robots-txt: ^1.3.0 => 1.3.0
gatsby-plugin-sitemap: ^2.0.1 => 2.0.1
gatsby-plugin-styled-components: ^3.0.0 => 3.0.0
gatsby-source-apiserver: ^2.0.0 => 2.0.0
gatsby-source-filesystem: ^2.0.1 => 2.0.1
gatsby-source-wordpress: ^3.0.0 => 3.0.1
gatsby-transformer-javascript-frontmatter: ^2.0.0 => 2.0.0
gatsby-transformer-json: ^2.1.2 => 2.1.2
npmGlobalPackages:
gatsby-cli: 2.4.3

@endymion1818 endymion1818 changed the title [gatsby-plugin-remove-trailing-slashes] always results in a 301 redirect [gatsby-plugin-remove-trailing-slashes] results in 301 redirects Oct 11, 2018
@stefanprobst
Copy link
Contributor

This is most likely a server config issue, see the plugin docs.

@endymion1818
Copy link
Contributor Author

Thanks Stefan, think you're right.

@drewatk
Copy link

drewatk commented Nov 15, 2018

Was this also happening for you on the development servers?

@endymion1818
Copy link
Contributor Author

Yes @drewatk, I have since learned that node server also does something similar

@abohannon
Copy link
Contributor

I'm encountering this, too.

Locally, using surge, and in production. The redirect is removing our tracking query params, as well, which is why I'm searching for a solution. Anyone have any ideas?

@endymion1818
Copy link
Contributor Author

I think there might be 2 ways of doing this:

  1. adding NGINX or some proxy in front of your hosting. This isn't great, because why have a static site if you're going to have a proxy in front of it?
  2. using a lambda function to change status codes. This is what we're currently investigating, and theres some confidence from this SO comment: https://stackoverflow.com/questions/31329495/is-there-a-way-to-change-the-http-status-codes-returned-by-amazon-api-gateway

I'll try to remember to post back here once we've implemented it to let you know the results of our implementation.

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

No branches or pull requests

4 participants