Skip to content

Commit

Permalink
fix(gatsby): handle case of removing trailing slash in inc builds (#2…
Browse files Browse the repository at this point in the history
…9953)

* add test case

* add one more edge case to tests

* add more assertions

* fix(gatsby): [incremental builds] handle case of page path changing during or between builds that wouldn't result in change of artifact filenames

this is to cover for cases like `gatsby-plugin-remove-trailing-slashes` that change page path during the build or
case when page path might be created from some cms content and trailing slash being added or removed there

* make normalizePagePath terser

* initial setup for calcDirtyHtmlFiles unit tests

* flesh out tests
  • Loading branch information
pieh authored Mar 4, 2021
1 parent bba21cd commit 7462030
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 11 deletions.
11 changes: 10 additions & 1 deletion integration-tests/artifacts/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,12 @@ describe(`First run (baseline)`, () => {
})
})

const expectedPages = [`stale-pages/stable`, `stale-pages/only-in-first`]
const expectedPages = [
`stale-pages/stable`,
`stale-pages/only-in-first`,
`page-that-will-have-trailing-slash-removed`,
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont`,
]
const unexpectedPages = [`stale-pages/only-not-in-first`]

describe(`html files`, () => {
Expand Down Expand Up @@ -460,6 +465,7 @@ describe(`Second run (different pages created, data changed)`, () => {
`/page-query-dynamic-2/`,
`/static-query-result-tracking/should-invalidate/`,
`/page-query-template-change/`,
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont/`,
]

const expectedPagesToRemainFromPreviousBuild = [
Expand All @@ -468,6 +474,7 @@ describe(`Second run (different pages created, data changed)`, () => {
`/page-query-changing-but-not-invalidating-html/`,
`/static-query-result-tracking/stable/`,
`/static-query-result-tracking/rerun-query-but-dont-recreate-html/`,
`/page-that-will-have-trailing-slash-removed`,
]

const expectedPages = [
Expand Down Expand Up @@ -542,6 +549,8 @@ describe(`Third run (js change, all pages are recreated)`, () => {
const expectedPages = [
`/stale-pages/only-not-in-first`,
`/page-query-dynamic-3/`,
`/page-that-will-have-trailing-slash-removed`,
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont`,
]

const unexpectedPages = [
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/artifacts/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.onPreInit = ({ emitter }) => {
}

let previouslyCreatedNodes = new Map()
let didRemoveTrailingSlashForTestedPage = false

exports.sourceNodes = ({
actions,
Expand Down Expand Up @@ -145,6 +146,12 @@ exports.createPages = async ({ actions, graphql }) => {
createPageHelper(`only-not-in-first`)
}

createPageHelper(
`sometimes-i-have-trailing-slash-sometimes-i-dont${
runNumber % 2 === 0 ? `/` : ``
}`
)

const { data } = await graphql(`
{
allDepPageQuery {
Expand Down Expand Up @@ -181,6 +188,13 @@ exports.onPreBuild = () => {
let counter = 1
exports.onPostBuild = async ({ graphql }) => {
console.log(`[test] onPostBuild`)

if (!didRemoveTrailingSlashForTestedPage) {
throw new Error(
`Test setup failed - didn't remove trailing slash for /pages-that-will-have-trailing-slash-removed/ page`
)
}

const { data } = await graphql(`
{
allSitePage(filter: { path: { ne: "/dev-404-page/" } }) {
Expand All @@ -206,3 +220,15 @@ exports.onPostBuild = async ({ graphql }) => {
}
)
}

// simulating "gatsby-plugin-remove-trailing-slashes" scenario
exports.onCreatePage = ({ page, actions }) => {
if (page.path === `/page-that-will-have-trailing-slash-removed/`) {
actions.deletePage(page)
actions.createPage({
...page,
path: `/page-that-will-have-trailing-slash-removed`,
})
didRemoveTrailingSlashForTestedPage = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from "react"

export default function NoTrailingSlashPage({ path, pathname }) {
return <div>I don't have trailing slash</div>
}
Loading

0 comments on commit 7462030

Please sign in to comment.