Skip to content

Commit 18944ce

Browse files
authored
fix(gatsby): Recognise null pages as not found (#27003)
* Show null page as not found * Don't register null paths * Add test for redirect
1 parent cb5c3d1 commit 18944ce

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

e2e-tests/development-runtime/cypress/integration/navigation/redirect.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ const runTests = () => {
4040
)
4141
})
4242
})
43+
44+
it(`should redirect to a dynamically-created replacement page`, () => {
45+
cy.visit(`/redirect-me/`).waitForRouteChange()
46+
47+
cy.location(`pathname`).should(`equal`, `/pt/redirect-me/`)
48+
cy.then(() => {
49+
expect(spy).not.to.be.calledWith(
50+
`The route "/redirect" matches both a page and a redirect; this is probably not intentional.`
51+
)
52+
53+
cy.findByText("This should be at /pt/redirect-me/", {
54+
exact: false,
55+
}).should(`exist`)
56+
})
57+
})
4358
}
4459

4560
describe(`redirect`, () => {

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,34 @@ exports.createPages = async function createPages({
104104
}
105105

106106
exports.onCreatePage = async ({ page, actions }) => {
107-
const { createPage } = actions
107+
const { createPage, createRedirect, deletePage } = actions
108108

109109
if (page.path.match(/^\/client-only-paths/)) {
110110
page.matchPath = `/client-only-paths/*`
111111
createPage(page)
112112
}
113+
114+
if (page.path === `/redirect-me/`) {
115+
const toPath = `/pt${page.path}`
116+
117+
deletePage(page)
118+
119+
createRedirect({
120+
fromPath: page.path,
121+
toPath,
122+
isPermanent: false,
123+
redirectInBrowser: true,
124+
Language: `pt`,
125+
statusCode: 301,
126+
})
127+
128+
createPage({
129+
...page,
130+
path: toPath,
131+
context: {
132+
...page.context,
133+
lang: `pt`,
134+
},
135+
})
136+
}
113137
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react"
2+
3+
import Layout from "../components/layout"
4+
import SEO from "../components/seo"
5+
6+
const Redirect = () => (
7+
<Layout>
8+
<SEO title="Redirect" />
9+
<p>This should be at /pt/redirect-me/</p>
10+
</Layout>
11+
)
12+
13+
export default Redirect

packages/gatsby/cache-dir/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class BaseLoader {
394394
isPageNotFound(rawPath) {
395395
const pagePath = findPath(rawPath)
396396
const page = this.pageDb.get(pagePath)
397-
return page && page.notFound === true
397+
return !page || page.notFound
398398
}
399399

400400
loadAppData(retries = 0) {

packages/gatsby/src/utils/websocket-manager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ export class WebsocketManager {
202202

203203
socket.on(`registerPath`, (path: string): void => {
204204
socket.join(getRoomNameFromPath(path))
205-
activePath = path
206-
this.activePaths.add(path)
205+
if (path) {
206+
activePath = path
207+
this.activePaths.add(path)
208+
}
207209
})
208210

209211
socket.on(`disconnect`, (): void => {

0 commit comments

Comments
 (0)