Skip to content

Commit 0654800

Browse files
authored
fix(gatsby): Hashes and anchors in redirects also work in production (#32850)
1 parent 1369881 commit 0654800

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

e2e-tests/production-runtime/cypress/integration/redirects.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe(`Redirects`, () => {
99

1010
cy.get(`h1`).invoke(`text`).should(`contain`, `Hi from the long page`)
1111
})
12+
1213
it(`are case sensitive when ignoreCase is set to false`, () => {
1314
cy.visit(`/PAGINA-larga`, { failOnStatusCode: false }).waitForRouteChange()
1415

@@ -57,4 +58,67 @@ describe(`Redirects`, () => {
5758
})
5859
})
5960
})
61+
62+
it(`should support hash parameter with Link component`, () => {
63+
cy.visit(`/`, {
64+
failOnStatusCode: false,
65+
}).waitForRouteChange()
66+
67+
cy.getTestElement(`redirect-two-anchor`).click().waitForRouteChange()
68+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
69+
cy.location(`hash`).should(`equal`, `#anchor`)
70+
cy.location(`search`).should(`equal`, ``)
71+
})
72+
73+
it(`should support hash parameter on direct visit`, () => {
74+
cy.visit(`/redirect-two#anchor`, {
75+
failOnStatusCode: false,
76+
}).waitForRouteChange()
77+
78+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
79+
cy.location(`hash`).should(`equal`, `#anchor`)
80+
cy.location(`search`).should(`equal`, ``)
81+
})
82+
83+
it(`should support search parameter with Link component`, () => {
84+
cy.visit(`/`, {
85+
failOnStatusCode: false,
86+
}).waitForRouteChange()
87+
88+
cy.getTestElement(`redirect-two-search`).click().waitForRouteChange()
89+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
90+
cy.location(`hash`).should(`equal`, ``)
91+
cy.location(`search`).should(`equal`, `?query_param=hello`)
92+
})
93+
94+
it(`should support search parameter on direct visit`, () => {
95+
cy.visit(`/redirect-two?query_param=hello`, {
96+
failOnStatusCode: false,
97+
}).waitForRouteChange()
98+
99+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
100+
cy.location(`hash`).should(`equal`, ``)
101+
cy.location(`search`).should(`equal`, `?query_param=hello`)
102+
})
103+
104+
it(`should support search & hash parameter with Link component`, () => {
105+
cy.visit(`/`, {
106+
failOnStatusCode: false,
107+
}).waitForRouteChange()
108+
109+
cy.getTestElement(`redirect-two-search-anchor`).click().waitForRouteChange()
110+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash`)
111+
cy.location(`hash`).should(`equal`, `#anchor`)
112+
cy.location(`search`).should(`equal`, `?query_param=hello`)
113+
})
114+
115+
it(`should support search & hash parameter on direct visit`, () => {
116+
cy.visit(`/redirect-two?query_param=hello#anchor`, {
117+
failOnStatusCode: false,
118+
}).waitForRouteChange()
119+
120+
cy.location(`pathname`).should(`equal`, `/redirect-search-hash/`)
121+
cy.location(`hash`).should(`equal`, `#anchor`)
122+
cy.location(`search`).should(`equal`, `?query_param=hello`)
123+
})
60124
})

e2e-tests/production-runtime/gatsby-node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ exports.createPages = ({ actions: { createPage, createRedirect } }) => {
145145
redirectInBrowser: true,
146146
ignoreCase: true,
147147
})
148+
149+
createRedirect({
150+
fromPath: `/redirect-two`,
151+
toPath: `/redirect-search-hash`,
152+
isPermanent: true,
153+
redirectInBrowser: true,
154+
})
148155
}
149156

150157
exports.onCreatePage = ({ page, actions }) => {

e2e-tests/production-runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"build": "cross-env CYPRESS_SUPPORT=y gatsby build",
3434
"build:offline": "cross-env TEST_PLUGIN_OFFLINE=y CYPRESS_SUPPORT=y gatsby build",
3535
"develop": "gatsby develop",
36-
"format": "prettier --write '**/*.js'",
36+
"format": "prettier --write '**/*.js' --ignore-path .gitignore",
3737
"serve": "gatsby serve",
3838
"start": "npm run develop",
3939
"test": "npm run build && npm run start-server-and-test && npm run test-env-vars",

e2e-tests/production-runtime/src/pages/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ const IndexPage = ({ pageContext }) => (
9292
Go to client route splat (splat: blah/blah/blah)
9393
</Link>
9494
</li>
95+
<li>
96+
<Link to="/redirect-two#anchor" data-testid="redirect-two-anchor">
97+
Go to redirect with hash
98+
</Link>
99+
</li>
100+
<li>
101+
<Link
102+
to="/redirect-two?query_param=hello"
103+
data-testid="redirect-two-search"
104+
>
105+
Go to redirect with query param
106+
</Link>
107+
</li>
108+
<li>
109+
<Link
110+
to="/redirect-two?query_param=hello#anchor"
111+
data-testid="redirect-two-search-anchor"
112+
>
113+
Go to redirect with query param and hash
114+
</Link>
115+
</li>
95116
</ul>
96117
</Layout>
97118
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from "react"
2+
3+
import Layout from "../components/layout"
4+
5+
const RedirectSearchHash = () => (
6+
<Layout>
7+
<p>This should be a page that also has search & hash</p>
8+
</Layout>
9+
)
10+
11+
export default RedirectSearchHash

packages/gatsby/cache-dir/navigation.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ const navigate = (to, options = {}) => {
9090
// If the loaded page has a different compilation hash to the
9191
// window, then a rebuild has occurred on the server. Reload.
9292
if (process.env.NODE_ENV === `production` && pageResources) {
93+
// window.___webpackCompilationHash gets set in production-app.js after navigationInit() is called
94+
// So on a direct visit of a page with a browser redirect this check is truthy and thus the codepath is hit
95+
// While the resource actually exists, but only too late
96+
// TODO: This should probably be fixed by setting ___webpackCompilationHash before navigationInit() is called
9397
if (
9498
pageResources.page.webpackCompilationHash !==
9599
window.___webpackCompilationHash
@@ -105,7 +109,7 @@ const navigate = (to, options = {}) => {
105109
})
106110
}
107111

108-
window.location = pathname
112+
window.location = pathname + search + hash
109113
}
110114
}
111115
reachNavigate(to, options)

0 commit comments

Comments
 (0)