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): don't force leading slash for external paths in routes manifest #38639

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 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,8 @@ 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(`/`)) {
const externalPathsRegex = new RegExp(`^https?://`, `i`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any performance concerns using a regex here, i.e. if a site has a lot of redirects?

Copy link
Contributor

@pieh pieh Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing route.path.startsWith('http://') || route.path.startsWith('https://') is faster than using that exact regex (tho doesn't cover case insensitivity) - so I'll adjust to use this.

"RegExp x 1,614,517 ops/sec ±0.62% (96 runs sampled)"
"startWith x 5,467,939 ops/sec ±0.17% (96 runs sampled)"
"Fastest is startWith"

Note here that it seems like initiating new regexp on each addRoute call is siginificant portion of overhead it seems - after moving regexp to be inited once earlier we get

"RegExp x 2,410,360 ops/sec ±0.99% (95 runs sampled)"
"startWith x 5,037,209 ops/sec ±3.31% (93 runs sampled)"
"Fastest is startWith"

so 2 startWith calls is still faster then regexp test (the startWith benchmark case was using 2 calls, not 1, so this will be ~twice as fast)

I'll adjust to use startWith and get PR merged

if (!route.path.startsWith(`/`) && !externalPathsRegex.test(route.path)) {
route.path = `/${route.path}`
}

Expand Down