Skip to content

Commit 9916b3d

Browse files
authored
fix(gatsby-plugin-page-creator): Find nested files for knownCollections (#37762)
1 parent a253589 commit 9916b3d

File tree

10 files changed

+32
-3
lines changed

10 files changed

+32
-3
lines changed

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/index.tsx

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/nested/{NestedDirectoryOnlyItem}/edit.tsx

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/nested/{NestedDirectoryOnlyItem}/index.tsx

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/nested/{NestedItem.name}.tsx

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/non-collection.tsx

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/{Other.extension}.js

Whitespace-only changes.

packages/gatsby-plugin-page-creator/src/__tests__/fixtures/page-utils/collection-routes/{RootItem.name}.tsx

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { findCollectionPageFiles } from "../path-utils"
2+
import path from "path"
3+
4+
describe(`findCollectionPageFiles`, () => {
5+
it(`will find all and only the collection routes in a fixture`, async () => {
6+
expect(
7+
await findCollectionPageFiles(
8+
path.join(__dirname, `fixtures`, `page-utils`, `collection-routes`)
9+
)
10+
).toMatchInlineSnapshot(`
11+
Array [
12+
"{Other.extension}.js",
13+
"{RootItem.name}.tsx",
14+
"nested/{NestedItem.name}.tsx",
15+
"nested/{NestedDirectoryOnlyItem}/edit.tsx",
16+
"nested/{NestedDirectoryOnlyItem}/index.tsx",
17+
]
18+
`)
19+
})
20+
})

packages/gatsby-plugin-page-creator/src/gatsby-node.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
getPluginInstance,
2929
ICreateAPageFromNodeArgs,
3030
} from "./tracked-nodes-state"
31+
import { findCollectionPageFiles } from "./path-utils"
3132
import { getCollectionRouteParams } from "./get-collection-route-params"
3233
import { reverseLookupParams } from "./extract-query"
3334
import { getMatchPath } from "gatsby-core-utils/match-path"
@@ -421,9 +422,7 @@ export async function onPluginInit(
421422
}
422423

423424
try {
424-
const pagesGlob = `**/**\\{*\\}**`
425-
426-
const files = await glob(pagesGlob, { cwd: pagesPath })
425+
const files = await findCollectionPageFiles(pagesPath)
427426

428427
if (files.length > 0) {
429428
trackFeatureIsUsed(`UnifiedRoutes:collection-page-builder`)

packages/gatsby-plugin-page-creator/src/path-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import glob from "globby"
2+
13
// Regex created with: https://spec.graphql.org/draft/#sec-Names
24
// First char only letter, underscore; rest letter, underscore, digit
35
const extractModelRegex = /\{([a-zA-Z_][\w]*)\./
@@ -121,3 +123,11 @@ export function compose(
121123
return (filePart: string): string =>
122124
functions.reduce((value, fn) => fn(value), filePart)
123125
}
126+
127+
// Use globby to find all page files with collection routes
128+
// We want to find all files in a given path where any segment includes text wrapped in curly braces
129+
// e.g. "{Collection.field}.tsx" and "{Collection.field}/nestedpage.tsx"
130+
export const pagesGlob = `**/**\\{*\\}**(/**)`
131+
export const findCollectionPageFiles = (
132+
pagesPath: string
133+
): Promise<Array<string>> => glob(pagesGlob, { cwd: pagesPath })

0 commit comments

Comments
 (0)