Skip to content

Commit

Permalink
fix(gatsby): copy slices overrides to 404.html copy (#38337)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 3, 2023
1 parent 0c8f948 commit 6c7a0e3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ describe("Slice passed via createPage", () => {
.should(`contain`, allRecipeAuthors.find(author => recipe.authorId === author.id).name)
})
})

it(`404 pages with slices mapping have correct content`, () => {
cy.visit(`/doesnt-exist`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.get(`button`).click()

cy.getTestElement(`mapped-slice`).should("have.text", "My mapped Slice")
})
})
12 changes: 12 additions & 0 deletions e2e-tests/development-runtime/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ exports.createPages = async function createPages({
},
})

createSlice({
id: `mappedslice-fakeid`,
component: require.resolve(`./src/components/mapped-slice.js`),
})

slicesData.allRecipeAuthors.forEach(({ id, name }) => {
createSlice({
id: `author-${id}`,
Expand Down Expand Up @@ -342,6 +347,13 @@ exports.onCreatePage = async ({ page, actions }) => {
})
}
}

if (page.path === `/404/`) {
createPage({
...page,
slices: { mappedslice: "mappedslice-fakeid" },
})
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions e2e-tests/development-runtime/src/components/mapped-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from "react"

const MappedSlice = () => {
return <div data-testid="mapped-slice">My mapped Slice</div>
}

export default MappedSlice
3 changes: 2 additions & 1 deletion e2e-tests/development-runtime/src/pages/404.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { graphql, Link } from "gatsby"
import { graphql, Link, Slice } from "gatsby"

import Layout from "../components/layout"
import Seo from "../components/seo"
Expand Down Expand Up @@ -64,6 +64,7 @@ const NotFoundPage = ({ data }) => (
Go to page B
</Link>
</fieldset>
<Slice alias="mappedslice" />
</Layout>
)
export const Head = () => <Seo title="404: Not found" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ describe("Slice passed via createPage", () => {
.should(`contain`, allRecipeAuthors.find(author => recipe.authorId === author.id).name)
})
})

it(`404 pages with slices mapping have correct content`, () => {
cy.visit(`/doesnt-exist`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.getTestElement(`mapped-slice`).should("have.text", "My mapped Slice")
})
})
12 changes: 12 additions & 0 deletions e2e-tests/production-runtime/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ export const createPages: GatsbyNode["createPages"] = ({
},
})

createSlice({
id: `mappedslice-fakeid`,
component: path.resolve(`./src/components/mapped-slice.js`),
})

slicesData.allRecipeAuthors.forEach(({ id, name }) => {
createSlice({
id: `author-${id}`,
Expand Down Expand Up @@ -322,5 +327,12 @@ export const onCreatePage: GatsbyNode["onCreatePage"] = ({ page, actions }) => {
},
})
break
case `/404/`: {
actions.createPage({
...page,
slices: { mappedslice: "mappedslice-fakeid" },
})
break
}
}
}
7 changes: 7 additions & 0 deletions e2e-tests/production-runtime/src/components/mapped-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from "react"

const MappedSlice = () => {
return <div data-testid="mapped-slice">My mapped Slice</div>
}

export default MappedSlice
3 changes: 2 additions & 1 deletion e2e-tests/production-runtime/src/pages/404.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react"
import { Link } from "gatsby"
import { Link, Slice } from "gatsby"

import Layout from "../components/layout"
import Seo from "../components/seo"
Expand All @@ -12,6 +12,7 @@ const NotFoundPage = () => (
<Link to="/" data-testid="index">
Go to Index
</Link>
<Slice alias="mappedslice" />
</Layout>
)

Expand Down
23 changes: 13 additions & 10 deletions packages/gatsby/src/internal-plugins/prod-404-500/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const isEqual = require(`lodash/isEqual`)

const { emitter, store } = require(`../../redux`)
const { actions } = require(`../../redux/actions`)

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

const originalPage = originalStatusPageByStatus[status]

if (!originalPage) {
const storedPage = {
path: action.payload.path,
component: action.payload.component,
context: action.payload.context,
status,
}
const pageToStore = {
path: action.payload.path,
component: action.payload.component,
context: action.payload.context,
slices: action.payload.slices,
status,
}

originalStatusPageByStatus[status] = storedPage
originalStatusPageByPath[action.payload.path] = storedPage
if (!originalPage || !isEqual(originalPage, pageToStore)) {
originalStatusPageByStatus[status] = pageToStore
originalStatusPageByPath[action.payload.path] = pageToStore

store.dispatch(
actions.createPage(
{
...storedPage,
...pageToStore,
path: `/${status}.html`,
},
action.plugin,
Expand Down

0 comments on commit 6c7a0e3

Please sign in to comment.