From 8302bc8e781bd147c1a0ddbbaa5b50740b01ef95 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Thu, 19 Oct 2023 11:34:03 -0400 Subject: [PATCH] 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 (cherry picked from commit 5dbcf9ea956880af7541aaf33ed2f764ec8632d5) Co-authored-by: Jenae Janzen <101715009+jenae-janzen@users.noreply.github.com> --- .../__tests__/__snapshots__/manager.ts.snap | 50 +++++++++++++++++++ .../utils/adapter/__tests__/fixtures/state.ts | 12 +++++ .../src/utils/adapter/__tests__/manager.ts | 14 ++++++ packages/gatsby/src/utils/adapter/manager.ts | 5 +- 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap b/packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap index d1a544ca1a1a8..2243c0842050e 100644 --- a/packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap +++ b/packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap @@ -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/", diff --git a/packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts b/packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts index 471317128314f..b272b43e5d4ad 100644 --- a/packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts +++ b/packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts @@ -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"] = [{ diff --git a/packages/gatsby/src/utils/adapter/__tests__/manager.ts b/packages/gatsby/src/utils/adapter/__tests__/manager.ts index 8fc17c0e90b60..a1b518057e0d5 100644 --- a/packages/gatsby/src/utils/adapter/__tests__/manager.ts +++ b/packages/gatsby/src/utils/adapter/__tests__/manager.ts @@ -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`, () => { diff --git a/packages/gatsby/src/utils/adapter/manager.ts b/packages/gatsby/src/utils/adapter/manager.ts index 5dd77a631c564..3ea04fdf2e757 100644 --- a/packages/gatsby/src/utils/adapter/manager.ts +++ b/packages/gatsby/src/utils/adapter/manager.ts @@ -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}` }