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

fix(gatsby): copy slices overrides to 404.html copy #38337

Merged
merged 2 commits into from
Jul 3, 2023
Merged
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
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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEqual check here was added (on top of using action.payload.slices) so that cases like src/pages/404.js + onCreatePage hook that adds slices (or context) can work as otherwise original page would be used and we would ignore potential future edits to the page.

originalStatusPageByStatus[status] = pageToStore
originalStatusPageByPath[action.payload.path] = pageToStore

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