Skip to content

Commit 7462030

Browse files
authored
fix(gatsby): handle case of removing trailing slash in inc builds (#29953)
* 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
1 parent bba21cd commit 7462030

File tree

8 files changed

+463
-11
lines changed

8 files changed

+463
-11
lines changed

integration-tests/artifacts/__tests__/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ describe(`First run (baseline)`, () => {
369369
})
370370
})
371371

372-
const expectedPages = [`stale-pages/stable`, `stale-pages/only-in-first`]
372+
const expectedPages = [
373+
`stale-pages/stable`,
374+
`stale-pages/only-in-first`,
375+
`page-that-will-have-trailing-slash-removed`,
376+
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont`,
377+
]
373378
const unexpectedPages = [`stale-pages/only-not-in-first`]
374379

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

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

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

547556
const unexpectedPages = [

integration-tests/artifacts/gatsby-node.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ exports.onPreInit = ({ emitter }) => {
3131
}
3232

3333
let previouslyCreatedNodes = new Map()
34+
let didRemoveTrailingSlashForTestedPage = false
3435

3536
exports.sourceNodes = ({
3637
actions,
@@ -145,6 +146,12 @@ exports.createPages = async ({ actions, graphql }) => {
145146
createPageHelper(`only-not-in-first`)
146147
}
147148

149+
createPageHelper(
150+
`sometimes-i-have-trailing-slash-sometimes-i-dont${
151+
runNumber % 2 === 0 ? `/` : ``
152+
}`
153+
)
154+
148155
const { data } = await graphql(`
149156
{
150157
allDepPageQuery {
@@ -181,6 +188,13 @@ exports.onPreBuild = () => {
181188
let counter = 1
182189
exports.onPostBuild = async ({ graphql }) => {
183190
console.log(`[test] onPostBuild`)
191+
192+
if (!didRemoveTrailingSlashForTestedPage) {
193+
throw new Error(
194+
`Test setup failed - didn't remove trailing slash for /pages-that-will-have-trailing-slash-removed/ page`
195+
)
196+
}
197+
184198
const { data } = await graphql(`
185199
{
186200
allSitePage(filter: { path: { ne: "/dev-404-page/" } }) {
@@ -206,3 +220,15 @@ exports.onPostBuild = async ({ graphql }) => {
206220
}
207221
)
208222
}
223+
224+
// simulating "gatsby-plugin-remove-trailing-slashes" scenario
225+
exports.onCreatePage = ({ page, actions }) => {
226+
if (page.path === `/page-that-will-have-trailing-slash-removed/`) {
227+
actions.deletePage(page)
228+
actions.createPage({
229+
...page,
230+
path: `/page-that-will-have-trailing-slash-removed`,
231+
})
232+
didRemoveTrailingSlashForTestedPage = true
233+
}
234+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as React from "react"
2+
3+
export default function NoTrailingSlashPage({ path, pathname }) {
4+
return <div>I don't have trailing slash</div>
5+
}

0 commit comments

Comments
 (0)