Skip to content

Commit 6c7a0e3

Browse files
authored
fix(gatsby): copy slices overrides to 404.html copy (#38337)
1 parent 0c8f948 commit 6c7a0e3

File tree

9 files changed

+73
-12
lines changed

9 files changed

+73
-12
lines changed

e2e-tests/development-runtime/cypress/integration/slices/slice-via-create-page.js

+10
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ describe("Slice passed via createPage", () => {
2222
.should(`contain`, allRecipeAuthors.find(author => recipe.authorId === author.id).name)
2323
})
2424
})
25+
26+
it(`404 pages with slices mapping have correct content`, () => {
27+
cy.visit(`/doesnt-exist`, {
28+
failOnStatusCode: false,
29+
}).waitForRouteChange()
30+
31+
cy.get(`button`).click()
32+
33+
cy.getTestElement(`mapped-slice`).should("have.text", "My mapped Slice")
34+
})
2535
})

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

+12
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ exports.createPages = async function createPages({
216216
},
217217
})
218218

219+
createSlice({
220+
id: `mappedslice-fakeid`,
221+
component: require.resolve(`./src/components/mapped-slice.js`),
222+
})
223+
219224
slicesData.allRecipeAuthors.forEach(({ id, name }) => {
220225
createSlice({
221226
id: `author-${id}`,
@@ -342,6 +347,13 @@ exports.onCreatePage = async ({ page, actions }) => {
342347
})
343348
}
344349
}
350+
351+
if (page.path === `/404/`) {
352+
createPage({
353+
...page,
354+
slices: { mappedslice: "mappedslice-fakeid" },
355+
})
356+
}
345357
}
346358

347359
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from "react"
2+
3+
const MappedSlice = () => {
4+
return <div data-testid="mapped-slice">My mapped Slice</div>
5+
}
6+
7+
export default MappedSlice

e2e-tests/development-runtime/src/pages/404.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { graphql, Link } from "gatsby"
2+
import { graphql, Link, Slice } from "gatsby"
33

44
import Layout from "../components/layout"
55
import Seo from "../components/seo"
@@ -64,6 +64,7 @@ const NotFoundPage = ({ data }) => (
6464
Go to page B
6565
</Link>
6666
</fieldset>
67+
<Slice alias="mappedslice" />
6768
</Layout>
6869
)
6970
export const Head = () => <Seo title="404: Not found" />

e2e-tests/production-runtime/cypress/integration/slices/slice-via-create-page.js

+8
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ describe("Slice passed via createPage", () => {
2222
.should(`contain`, allRecipeAuthors.find(author => recipe.authorId === author.id).name)
2323
})
2424
})
25+
26+
it(`404 pages with slices mapping have correct content`, () => {
27+
cy.visit(`/doesnt-exist`, {
28+
failOnStatusCode: false,
29+
}).waitForRouteChange()
30+
31+
cy.getTestElement(`mapped-slice`).should("have.text", "My mapped Slice")
32+
})
2533
})

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

+12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ export const createPages: GatsbyNode["createPages"] = ({
137137
},
138138
})
139139

140+
createSlice({
141+
id: `mappedslice-fakeid`,
142+
component: path.resolve(`./src/components/mapped-slice.js`),
143+
})
144+
140145
slicesData.allRecipeAuthors.forEach(({ id, name }) => {
141146
createSlice({
142147
id: `author-${id}`,
@@ -322,5 +327,12 @@ export const onCreatePage: GatsbyNode["onCreatePage"] = ({ page, actions }) => {
322327
},
323328
})
324329
break
330+
case `/404/`: {
331+
actions.createPage({
332+
...page,
333+
slices: { mappedslice: "mappedslice-fakeid" },
334+
})
335+
break
336+
}
325337
}
326338
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from "react"
2+
3+
const MappedSlice = () => {
4+
return <div data-testid="mapped-slice">My mapped Slice</div>
5+
}
6+
7+
export default MappedSlice

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react"
2-
import { Link } from "gatsby"
2+
import { Link, Slice } from "gatsby"
33

44
import Layout from "../components/layout"
55
import Seo from "../components/seo"
@@ -12,6 +12,7 @@ const NotFoundPage = () => (
1212
<Link to="/" data-testid="index">
1313
Go to Index
1414
</Link>
15+
<Slice alias="mappedslice" />
1516
</Layout>
1617
)
1718

packages/gatsby/src/internal-plugins/prod-404-500/gatsby-node.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const isEqual = require(`lodash/isEqual`)
2+
13
const { emitter, store } = require(`../../redux`)
24
const { actions } = require(`../../redux/actions`)
35

@@ -15,21 +17,22 @@ emitter.on(`CREATE_PAGE`, action => {
1517

1618
const originalPage = originalStatusPageByStatus[status]
1719

18-
if (!originalPage) {
19-
const storedPage = {
20-
path: action.payload.path,
21-
component: action.payload.component,
22-
context: action.payload.context,
23-
status,
24-
}
20+
const pageToStore = {
21+
path: action.payload.path,
22+
component: action.payload.component,
23+
context: action.payload.context,
24+
slices: action.payload.slices,
25+
status,
26+
}
2527

26-
originalStatusPageByStatus[status] = storedPage
27-
originalStatusPageByPath[action.payload.path] = storedPage
28+
if (!originalPage || !isEqual(originalPage, pageToStore)) {
29+
originalStatusPageByStatus[status] = pageToStore
30+
originalStatusPageByPath[action.payload.path] = pageToStore
2831

2932
store.dispatch(
3033
actions.createPage(
3134
{
32-
...storedPage,
35+
...pageToStore,
3336
path: `/${status}.html`,
3437
},
3538
action.plugin,

0 commit comments

Comments
 (0)