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)

* don't force leading slash for external redirects

* add test

* perf: regexp -> double String.startWith checks

---------

Co-authored-by: Michal Piechowiak <[email protected]>
  • Loading branch information
jenae-janzen and pieh authored Oct 18, 2023
1 parent d90d747 commit 5dbcf9e
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 5dbcf9e

Please sign in to comment.