Gatsby: [gatsby-plugin-remove-trailing-slashes] results in 301 redirects

Created on 11 Oct 2018  路  6Comments  路  Source: gatsbyjs/gatsby

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

Most helpful comment

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

All 6 comments

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

Thanks Stefan, think you're right.

Was this also happening for you on the development servers?

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

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?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KyleAMathews picture KyleAMathews  路  3Comments

theduke picture theduke  路  3Comments

benstr picture benstr  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

andykais picture andykais  路  3Comments