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

fix(gatsby): Set trailingSlash default to "always" #36798

Merged
merged 15 commits into from
Oct 20, 2022
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
describe(`Client only paths`, () => {
const routes = [
{
path: `/client-only-paths`,
path: `/client-only-paths/`,
marker: `index`,
label: `Index route`,
},
{
path: `/client-only-paths/page/profile`,
path: `/client-only-paths/page/profile/`,
marker: `profile`,
label: `Dynamic route`,
},
{
path: `/client-only-paths/not-found`,
path: `/client-only-paths/not-found/`,
marker: `NotFound`,
label: `Default route (not found)`,
},
{
path: `/client-only-paths/nested`,
path: `/client-only-paths/nested/`,
marker: `nested-page/index`,
label: `Index route inside nested router`,
},
{
path: `/client-only-paths/nested/foo`,
path: `/client-only-paths/nested/foo/`,
marker: `nested-page/foo`,
label: `Dynamic route inside nested router`,
},
{
path: `/client-only-paths/static`,
path: `/client-only-paths/static/`,
marker: `static-sibling`,
label: `Static route that is a sibling to client only path`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function getExpectedCanonicalPath(config) {
return `/query-data-caches/${config.slug}/${
config.page === `client-only`
? `:client-only`
: `page-${config.page}${config.trailingSlash === false ? `` : `/`}`
: `page-${config.page}/`
}`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe(`navigation`, () => {
cy.getTestElement(`subdir-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-1`)
.should(`eq`, `/subdirectory/page-1/`)
})

it(`can navigate to a sibling page`, () => {
Expand All @@ -55,7 +55,7 @@ describe(`navigation`, () => {
.getTestElement(`page-2-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-2`)
.should(`eq`, `/subdirectory/page-2/`)
})

it(`can navigate to a parent page`, () => {
Expand All @@ -64,7 +64,7 @@ describe(`navigation`, () => {
.getTestElement(`page-parent-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory`)
.should(`eq`, `/subdirectory/`)
})

it(`can navigate to a sibling page programatically`, () => {
Expand All @@ -73,7 +73,7 @@ describe(`navigation`, () => {
.getTestElement(`page-2-button-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-2`)
.should(`eq`, `/subdirectory/page-2/`)
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const runTests = () => {
it(`should redirect page to index page when there is no such page`, () => {
cy.visit(`/redirect-without-page`, {
cy.visit(`/redirect-without-page/`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/`)
})

it(`should redirect page to index page even there is a such page`, () => {
cy.visit(`/redirect`, {
cy.visit(`/redirect/`, {
failOnStatusCode: false,
}).waitForRouteChange()

Expand All @@ -29,17 +29,17 @@ const runTests = () => {
}).waitForRouteChange()

cy.getTestElement(`redirect-two-anchor`).click().waitForRouteChange()
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, ``)
})

it(`should support hash parameter on direct visit`, () => {
cy.visit(`/redirect-two#anchor`, {
cy.visit(`/redirect-two/#anchor`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, ``)
})
Expand All @@ -50,17 +50,17 @@ const runTests = () => {
}).waitForRouteChange()

cy.getTestElement(`redirect-two-search`).click().waitForRouteChange()
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, ``)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})

it(`should support search parameter on direct visit`, () => {
cy.visit(`/redirect-two?query_param=hello`, {
cy.visit(`/redirect-two/?query_param=hello`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, ``)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})
Expand All @@ -71,17 +71,17 @@ const runTests = () => {
}).waitForRouteChange()

cy.getTestElement(`redirect-two-search-anchor`).click().waitForRouteChange()
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})

it(`should support search & hash parameter on direct visit`, () => {
cy.visit(`/redirect-two?query_param=hello#anchor`, {
cy.visit(`/redirect-two/?query_param=hello#anchor`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})
Expand Down
8 changes: 4 additions & 4 deletions e2e-tests/development-runtime/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,22 @@ exports.createPages = async function createPages({
})

createRedirect({
fromPath: `/redirect-without-page`,
fromPath: `/redirect-without-page/`,
toPath: `/`,
isPermanent: true,
redirectInBrowser: true,
})

createRedirect({
fromPath: `/redirect`,
fromPath: `/redirect/`,
toPath: `/`,
isPermanent: true,
redirectInBrowser: true,
})

createRedirect({
fromPath: `/redirect-two`,
toPath: `/redirect-search-hash`,
fromPath: `/redirect-two/`,
toPath: `/redirect-search-hash/`,
isPermanent: true,
redirectInBrowser: true,
})
Expand Down
6 changes: 3 additions & 3 deletions e2e-tests/development-runtime/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ const IndexPage = ({ data }) => (
<Link to="/new-page" data-testid="hot-reloading-new-file">
Created by hot-reloading/new-file.js
</Link>
<Link to="/redirect-two#anchor" data-testid="redirect-two-anchor">
<Link to="/redirect-two/#anchor" data-testid="redirect-two-anchor">
Go to redirect with hash
</Link>
<Link
to="/redirect-two?query_param=hello"
to="/redirect-two/?query_param=hello"
data-testid="redirect-two-search"
>
Go to redirect with query param
</Link>
<Link
to="/redirect-two?query_param=hello#anchor"
to="/redirect-two/?query_param=hello#anchor"
data-testid="redirect-two-search-anchor"
>
Go to redirect with query param and hash
Expand Down
8 changes: 4 additions & 4 deletions e2e-tests/path-prefix/cypress/integration/navigate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe(`navigate`, () => {
cy.getTestElement(`subdir-link`)
.click()
.location(`pathname`)
.should(`eq`, `${pathPrefix}/subdirectory/page-1`)
.should(`eq`, withTrailingSlash(`${pathPrefix}/subdirectory/page-1`))
})

it(`can navigate to a sibling page`, () => {
Expand All @@ -38,7 +38,7 @@ describe(`navigate`, () => {
.getTestElement(`page-2-link`)
.click()
.location(`pathname`)
.should(`eq`, `${pathPrefix}/subdirectory/page-2`)
.should(`eq`, withTrailingSlash(`${pathPrefix}/subdirectory/page-2`))
})

it(`can navigate to a parent page`, () => {
Expand All @@ -47,7 +47,7 @@ describe(`navigate`, () => {
.getTestElement(`page-parent-link`)
.click()
.location(`pathname`)
.should(`eq`, `${pathPrefix}/subdirectory`)
.should(`eq`, withTrailingSlash(`${pathPrefix}/subdirectory`))
})

it(`can navigate to a sibling page programatically`, () => {
Expand All @@ -56,7 +56,7 @@ describe(`navigate`, () => {
.getTestElement(`page-2-button-link`)
.click()
.location(`pathname`)
.should(`eq`, `${pathPrefix}/subdirectory/page-2`)
.should(`eq`, withTrailingSlash(`${pathPrefix}/subdirectory/page-2`))
})

it(`can navigate to SSR page`, () => {
Expand Down
14 changes: 7 additions & 7 deletions e2e-tests/production-runtime/cypress/integration/1-production.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe(`Production build tests`, () => {
.getTestElement(`subdir-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-1`)
.should(`eq`, `/subdirectory/page-1/`)
})

it(`can navigate to a sibling page`, () => {
Expand All @@ -59,7 +59,7 @@ describe(`Production build tests`, () => {
.getTestElement(`page-2-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-2`)
.should(`eq`, `/subdirectory/page-2/`)
})

it(`can navigate to a parent page`, () => {
Expand All @@ -68,7 +68,7 @@ describe(`Production build tests`, () => {
.getTestElement(`page-parent-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory`)
.should(`eq`, `/subdirectory/`)
})

it(`can navigate to a sibling page programatically`, () => {
Expand All @@ -77,7 +77,7 @@ describe(`Production build tests`, () => {
.getTestElement(`page-2-button-link`)
.click()
.location(`pathname`)
.should(`eq`, `/subdirectory/page-2`)
.should(`eq`, `/subdirectory/page-2/`)
})
})

Expand Down Expand Up @@ -224,15 +224,15 @@ describe(`Production build tests`, () => {
})

describe(`Keeps search query`, () => {
describe(`No trailing slash canonical path (/slashes/no-trailing)`, () => {
describe(`Converts no trailing slash canonical path to trailing (/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(`pathname`).should(`equal`, `/slashes/no-trailing/`)
cy.location(`search`).should(`equal`, `?param=value`)
})

Expand All @@ -243,7 +243,7 @@ describe(`Production build tests`, () => {
.invoke(`text`)
.should(`equal`, `?param=value`)

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