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}` }