Skip to content

Commit

Permalink
fix(gatsby): preserve query params on pages without trailing slashes (#…
Browse files Browse the repository at this point in the history
…33811) (#33894)

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
GatsbyJS Bot and pieh authored Nov 8, 2021
1 parent 4086ca5 commit e32b355
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
50 changes: 50 additions & 0 deletions e2e-tests/production-runtime/cypress/integration/1-production.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,54 @@ describe(`Production build tests`, () => {
cy.getTestElement(`404`).should(`exist`)
})
})

describe(`Keeps search query`, () => {
describe(`No trailing slash canonical path (/slashes/no-trailing)`, () => {
it(`/slashes/no-trailing?param=value`, () => {
cy.visit(`/slashes/no-trailing?param=value`).waitForRouteChange()

cy.getTestElement(`search-marker`)
.invoke(`text`)
.should(`equal`, `?param=value`)

cy.location(`pathname`).should(`equal`, `/slashes/no-trailing`)
cy.location(`search`).should(`equal`, `?param=value`)
})

it(`/slashes/no-trailing/?param=value`, () => {
cy.visit(`/slashes/no-trailing/?param=value`).waitForRouteChange()

cy.getTestElement(`search-marker`)
.invoke(`text`)
.should(`equal`, `?param=value`)

cy.location(`pathname`).should(`equal`, `/slashes/no-trailing`)
cy.location(`search`).should(`equal`, `?param=value`)
})
})

describe(`With trailing slash canonical path (/slashes/with-trailing/)`, () => {
it(`/slashes/with-trailing?param=value`, () => {
cy.visit(`/slashes/with-trailing?param=value`).waitForRouteChange()

cy.getTestElement(`search-marker`)
.invoke(`text`)
.should(`equal`, `?param=value`)

cy.location(`pathname`).should(`equal`, `/slashes/with-trailing/`)
cy.location(`search`).should(`equal`, `?param=value`)
})

it(`/slashes/with-trailing/?param=value`, () => {
cy.visit(`/slashes/with-trailing/?param=value`).waitForRouteChange()

cy.getTestElement(`search-marker`)
.invoke(`text`)
.should(`equal`, `?param=value`)

cy.location(`pathname`).should(`equal`, `/slashes/with-trailing/`)
cy.location(`search`).should(`equal`, `?param=value`)
})
})
})
})
16 changes: 16 additions & 0 deletions e2e-tests/production-runtime/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ exports.createPages = ({ actions: { createPage, createRedirect } }) => {
component: path.resolve(`./.cache/static-page-from-cache.js`),
})

{
const searchParamComponent = path.resolve(
`src/templates/search-param-render.js`
)

createPage({
path: `/slashes/no-trailing`,
component: searchParamComponent,
})

createPage({
path: `/slashes/with-trailing/`,
component: searchParamComponent,
})
}

createRedirect({
fromPath: "/pagina-larga",
toPath: "/long-page",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from "react"

const SearchParam = ({ location }) => (
<pre data-testid="search-marker">{location.search}</pre>
)

export default SearchParam
12 changes: 9 additions & 3 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,15 @@ apiRunnerAsync(`onClientEntry`).then(() => {
pagePath.match(/^\/offline-plugin-app-shell-fallback\/?$/)
)
) {
navigate(__BASE_PATH__ + pagePath + browserLoc.hash, {
replace: true,
})
navigate(
__BASE_PATH__ +
pagePath +
(!pagePath.includes(`?`) ? browserLoc.search : ``) +
browserLoc.hash,
{
replace: true,
}
)
}

publicLoader.loadPage(browserLoc.pathname + browserLoc.search).then(page => {
Expand Down

0 comments on commit e32b355

Please sign in to comment.