Skip to content

Commit

Permalink
fix(gatsby): don't force leading slash for external paths in routes m…
Browse files Browse the repository at this point in the history
…anifest (#38639) (#38646)

* don't force leading slash for external redirects

* add test

* perf: regexp -> double String.startWith checks

---------

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
(cherry picked from commit 5dbcf9e)

Co-authored-by: Jenae Janzen <101715009+jenae-janzen@users.noreply.github.com>
  • Loading branch information
gatsbybot and jenae-janzen committed Oct 19, 2023
1 parent 39d2fd8 commit 8302bc8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,56 @@ Array [
"path": "/page-data/ssr/page-data.json",
"type": "function",
},
Object {
"headers": Array [
Object {
"key": "x-xss-protection",
"value": "1; mode=block",
},
Object {
"key": "x-content-type-options",
"value": "nosniff",
},
Object {
"key": "referrer-policy",
"value": "same-origin",
},
Object {
"key": "x-frame-options",
"value": "DENY",
},
],
"ignoreCase": true,
"path": "http://old-url",
"status": 301,
"toPath": "http://new-url",
"type": "redirect",
},
Object {
"headers": Array [
Object {
"key": "x-xss-protection",
"value": "1; mode=block",
},
Object {
"key": "x-content-type-options",
"value": "nosniff",
},
Object {
"key": "referrer-policy",
"value": "same-origin",
},
Object {
"key": "x-frame-options",
"value": "DENY",
},
],
"ignoreCase": true,
"path": "https://old-url",
"status": 301,
"toPath": "https://new-url",
"type": "redirect",
},
Object {
"functionId": "static-index-js",
"path": "/api/static/",
Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ const redirects: IGatsbyState["redirects"] = [{
ignoreCase: true,
redirectInBrowser: false,
toPath: '/new-url'
}, {
fromPath: 'https://old-url',
isPermanent: true,
ignoreCase: true,
redirectInBrowser: false,
toPath: 'https://new-url'
}, {
fromPath: 'http://old-url',
isPermanent: true,
ignoreCase: true,
redirectInBrowser: false,
toPath: 'http://new-url'
}]

const functions: IGatsbyState["functions"] = [{
Expand Down
14 changes: 14 additions & 0 deletions packages/gatsby/src/utils/adapter/__tests__/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ describe(`getRoutesManifest`, () => {
])
)
})

it(`should not prepend '\\' to external redirects`, () => {
mockStoreState(stateDefault)
process.chdir(fixturesDir)
setWebpackAssets(new Set([`app-123.js`]))

const routesManifest = getRoutesManifest()
expect(routesManifest).toEqual(
expect.arrayContaining([
expect.objectContaining({ path: `https://old-url` }),
expect.objectContaining({ path: `http://old-url` }),
])
)
})
})

describe(`getFunctionsManifest`, () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby/src/utils/adapter/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ function getRoutesManifest(): RoutesManifest {

// TODO: This could be a "addSortedRoute" function that would add route to the list in sorted order. TBD if necessary performance-wise
function addRoute(route: Route): void {
if (!route.path.startsWith(`/`)) {
if (
!route.path.startsWith(`/`) &&
!(route.path.startsWith(`https://`) || route.path.startsWith(`http://`))
) {
route.path = `/${route.path}`
}

Expand Down

0 comments on commit 8302bc8

Please sign in to comment.