Skip to content

Commit 8302bc8

Browse files
fix(gatsby): don't force leading slash for external paths in routes manifest (#38639) (#38646)
* don't force leading slash for external redirects * add test * perf: regexp -> double String.startWith checks --------- Co-authored-by: Michal Piechowiak <[email protected]> (cherry picked from commit 5dbcf9e) Co-authored-by: Jenae Janzen <[email protected]>
1 parent 39d2fd8 commit 8302bc8

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,56 @@ Array [
9494
"path": "/page-data/ssr/page-data.json",
9595
"type": "function",
9696
},
97+
Object {
98+
"headers": Array [
99+
Object {
100+
"key": "x-xss-protection",
101+
"value": "1; mode=block",
102+
},
103+
Object {
104+
"key": "x-content-type-options",
105+
"value": "nosniff",
106+
},
107+
Object {
108+
"key": "referrer-policy",
109+
"value": "same-origin",
110+
},
111+
Object {
112+
"key": "x-frame-options",
113+
"value": "DENY",
114+
},
115+
],
116+
"ignoreCase": true,
117+
"path": "http://old-url",
118+
"status": 301,
119+
"toPath": "http://new-url",
120+
"type": "redirect",
121+
},
122+
Object {
123+
"headers": Array [
124+
Object {
125+
"key": "x-xss-protection",
126+
"value": "1; mode=block",
127+
},
128+
Object {
129+
"key": "x-content-type-options",
130+
"value": "nosniff",
131+
},
132+
Object {
133+
"key": "referrer-policy",
134+
"value": "same-origin",
135+
},
136+
Object {
137+
"key": "x-frame-options",
138+
"value": "DENY",
139+
},
140+
],
141+
"ignoreCase": true,
142+
"path": "https://old-url",
143+
"status": 301,
144+
"toPath": "https://new-url",
145+
"type": "redirect",
146+
},
97147
Object {
98148
"functionId": "static-index-js",
99149
"path": "/api/static/",

packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ const redirects: IGatsbyState["redirects"] = [{
7070
ignoreCase: true,
7171
redirectInBrowser: false,
7272
toPath: '/new-url'
73+
}, {
74+
fromPath: 'https://old-url',
75+
isPermanent: true,
76+
ignoreCase: true,
77+
redirectInBrowser: false,
78+
toPath: 'https://new-url'
79+
}, {
80+
fromPath: 'http://old-url',
81+
isPermanent: true,
82+
ignoreCase: true,
83+
redirectInBrowser: false,
84+
toPath: 'http://new-url'
7385
}]
7486

7587
const functions: IGatsbyState["functions"] = [{

packages/gatsby/src/utils/adapter/__tests__/manager.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ describe(`getRoutesManifest`, () => {
9292
])
9393
)
9494
})
95+
96+
it(`should not prepend '\\' to external redirects`, () => {
97+
mockStoreState(stateDefault)
98+
process.chdir(fixturesDir)
99+
setWebpackAssets(new Set([`app-123.js`]))
100+
101+
const routesManifest = getRoutesManifest()
102+
expect(routesManifest).toEqual(
103+
expect.arrayContaining([
104+
expect.objectContaining({ path: `https://old-url` }),
105+
expect.objectContaining({ path: `http://old-url` }),
106+
])
107+
)
108+
})
95109
})
96110

97111
describe(`getFunctionsManifest`, () => {

packages/gatsby/src/utils/adapter/manager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ function getRoutesManifest(): RoutesManifest {
276276

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

0 commit comments

Comments
 (0)