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

Pass 'force' parameter for redirects #38640

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
86 changes: 66 additions & 20 deletions e2e-tests/adapters/cypress/e2e/redirects.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { applyTrailingSlashOption } from "../../utils"

Cypress.on("uncaught:exception", (err) => {
Cypress.on("uncaught:exception", err => {
if (err.message.includes("Minified React error")) {
return false
}
Expand All @@ -14,44 +14,90 @@ describe("Redirects", () => {
it("should redirect from non-existing page to existing", () => {
cy.visit(applyTrailingSlashOption(`/redirect`, TRAILING_SLASH), {
failOnStatusCode: false,
}).waitForRouteChange()
.assertRoute(applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH))
})
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH)
)

cy.get(`h1`).should(`have.text`, `Hit`)
})
it("should respect that pages take precedence over redirects", () => {
cy.visit(applyTrailingSlashOption(`/routes/redirect/existing`, TRAILING_SLASH), {
failOnStatusCode: false,
}).waitForRouteChange()
.assertRoute(applyTrailingSlashOption(`/routes/redirect/existing`, TRAILING_SLASH))
cy.visit(
applyTrailingSlashOption(`/routes/redirect/existing`, TRAILING_SLASH),
{
failOnStatusCode: false,
}
)
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/redirect/existing`, TRAILING_SLASH)
)

cy.get(`h1`).should(`have.text`, `Existing`)
})
it("should respect force redirect ", () => {
cy.visit(
applyTrailingSlashOption(
`/routes/redirect/existing-force`,
TRAILING_SLASH
),
{
failOnStatusCode: false,
}
)
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH)
)

cy.get(`h1`).should(`have.text`, `Hit`)
})
it("should support hash parameter on direct visit", () => {
cy.visit(applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) + `#anchor`, {
failOnStatusCode: false,
}).waitForRouteChange()
cy.visit(
applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) + `#anchor`,
{
failOnStatusCode: false,
}
).waitForRouteChange()

cy.location(`pathname`).should(`equal`, applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH))
cy.location(`pathname`).should(
`equal`,
applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH)
)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, ``)
})
it("should support search parameter on direct visit", () => {
cy.visit(applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) + `?query_param=hello`, {
failOnStatusCode: false,
}).waitForRouteChange()
cy.visit(
applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) +
`?query_param=hello`,
{
failOnStatusCode: false,
}
).waitForRouteChange()

cy.location(`pathname`).should(`equal`, applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH))
cy.location(`pathname`).should(
`equal`,
applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH)
)
cy.location(`hash`).should(`equal`, ``)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})
it("should support search & hash parameter on direct visit", () => {
cy.visit(applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) + `?query_param=hello#anchor`, {
failOnStatusCode: false,
}).waitForRouteChange()
cy.visit(
applyTrailingSlashOption(`/redirect`, TRAILING_SLASH) +
`?query_param=hello#anchor`,
{
failOnStatusCode: false,
}
).waitForRouteChange()

cy.location(`pathname`).should(`equal`, applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH))
cy.location(`pathname`).should(
`equal`,
applyTrailingSlashOption(`/routes/redirect/hit`, TRAILING_SLASH)
)
cy.location(`hash`).should(`equal`, `#anchor`)
cy.location(`search`).should(`equal`, `?query_param=hello`)
})
})
})
22 changes: 18 additions & 4 deletions e2e-tests/adapters/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ import * as path from "path"
import type { GatsbyNode, GatsbyConfig } from "gatsby"
import { applyTrailingSlashOption } from "./utils"

const TRAILING_SLASH = (process.env.TRAILING_SLASH || `never`) as GatsbyConfig["trailingSlash"]
const TRAILING_SLASH = (process.env.TRAILING_SLASH ||
`never`) as GatsbyConfig["trailingSlash"]

export const createPages: GatsbyNode["createPages"] = ({ actions: { createRedirect, createSlice } }) => {
export const createPages: GatsbyNode["createPages"] = ({
actions: { createRedirect, createSlice },
}) => {
createRedirect({
fromPath: applyTrailingSlashOption("/redirect", TRAILING_SLASH),
toPath: applyTrailingSlashOption("/routes/redirect/hit", TRAILING_SLASH),
})
createRedirect({
fromPath: applyTrailingSlashOption("/routes/redirect/existing", TRAILING_SLASH),
fromPath: applyTrailingSlashOption(
"/routes/redirect/existing",
TRAILING_SLASH
),
toPath: applyTrailingSlashOption("/routes/redirect/hit", TRAILING_SLASH),
})
createRedirect({
fromPath: applyTrailingSlashOption(
"/routes/redirect/existing-force",
TRAILING_SLASH
),
toPath: applyTrailingSlashOption("/routes/redirect/hit", TRAILING_SLASH),
force: true,
})

createSlice({
id: `footer`,
component: path.resolve(`./src/components/footer.jsx`),
context: {},
})
}
}
14 changes: 14 additions & 0 deletions e2e-tests/adapters/src/pages/routes/redirect/existing-force.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from "react"
import Layout from "../../../components/layout"

const ExistingForcePage = () => {
return (
<Layout>
<h1>Existing Force</h1>
</Layout>
)
}

export default ExistingForcePage

export const Head = () => <title>Existing Force</title>
48 changes: 48 additions & 0 deletions packages/gatsby-adapter-netlify/src/__tests__/route-handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fs from "fs-extra"
import { tmpdir } from "os"
import { join } from "path"
import type { IRedirectRoute, RoutesManifest } from "gatsby"
import {
injectEntries,
ADAPTER_MARKER_START,
ADAPTER_MARKER_END,
NETLIFY_PLUGIN_MARKER_START,
NETLIFY_PLUGIN_MARKER_END,
GATSBY_PLUGIN_MARKER_START,
processRoutesManifest,
} from "../route-handler"

function generateLotOfContent(placeholderCharacter: string): string {
Expand Down Expand Up @@ -142,4 +144,50 @@ describe(`route-handler`, () => {
})
})
})

describe(`createRedirects`, () => {
it(`honors the force parameter`, async () => {
const manifest: RoutesManifest = [
{
path: `/old-url`,
type: `redirect`,
toPath: `/new-url`,
status: 301,
headers: [{ key: `string`, value: `string` }],
force: true,
},
{
path: `/old-url2`,
type: `redirect`,
toPath: `/new-url2`,
status: 308,
headers: [{ key: `string`, value: `string` }],
force: false,
},
]

const { redirects } = processRoutesManifest(manifest)

// `!` is appended to status to mark forced redirect
expect(redirects).toMatch(/^\/old-url\s+\/new-url\s+301!$/m)
// `!` is not appended to status to mark not forced redirect
expect(redirects).toMatch(/^\/old-url2\s+\/new-url2\s+308$/m)
})

it(`honors the conditions parameter`, async () => {
const redirect: IRedirectRoute = {
path: `/old-url`,
type: `redirect`,
toPath: `/new-url`,
status: 200,
headers: [{ key: `string`, value: `string` }],
conditions: { language: [`ca`, `us`] },
}

const { redirects } = processRoutesManifest([redirect])
expect(redirects).toMatch(
/^\/old-url\s+\/new-url\s+200\s+Language:ca,us$/m
)
})
})
})
24 changes: 16 additions & 8 deletions packages/gatsby-adapter-netlify/src/route-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ export async function injectEntries(
await fs.move(tmpFile, fileName)
}

export async function handleRoutesManifest(
routesManifest: RoutesManifest
): Promise<{
export function processRoutesManifest(routesManifest: RoutesManifest): {
redirects: string
headers: string
lambdasThatUseCaching: Map<string, string>
}> {
} {
const lambdasThatUseCaching = new Map<string, string>()

let _redirects = ``
Expand All @@ -159,14 +159,13 @@ export async function handleRoutesManifest(
const {
status: routeStatus,
toPath,
force,
// TODO: add headers handling
headers,
...rest
} = route
let status = String(routeStatus)

if (force) {
if (rest.force) {
status = `${status}!`
}

Expand Down Expand Up @@ -222,9 +221,18 @@ export async function handleRoutesManifest(
)}`
}
}
return { redirects: _redirects, headers: _headers, lambdasThatUseCaching }
}

await injectEntries(`public/_redirects`, _redirects)
await injectEntries(`public/_headers`, _headers)
export async function handleRoutesManifest(
routesManifest: RoutesManifest
): Promise<{
lambdasThatUseCaching: Map<string, string>
}> {
const { redirects, headers, lambdasThatUseCaching } =
processRoutesManifest(routesManifest)
await injectEntries(`public/_redirects`, redirects)
await injectEntries(`public/_headers`, headers)

return {
lambdasThatUseCaching,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
src/__tests__/.cache-fetch
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,38 @@ Array [
"toPath": "/new-url",
"type": "redirect",
},
Object {
"conditions": Object {
"language": Array [
"ca",
"us",
],
},
"force": true,
"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": "/old-url2",
"status": 301,
"toPath": "/new-url2",
"type": "redirect",
},
Object {
"functionId": "ssr-engine",
"path": "/ssr/",
Expand Down
8 changes: 8 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,14 @@ const redirects: IGatsbyState["redirects"] = [{
ignoreCase: true,
redirectInBrowser: false,
toPath: '/new-url'
}, {
fromPath: '/old-url2',
isPermanent: true,
ignoreCase: true,
redirectInBrowser: false,
toPath: '/new-url2',
force: true,
conditions: { language: [`ca`, `us`] }
}, {
fromPath: 'https://old-url',
isPermanent: true,
Expand Down
Loading