Skip to content

Commit 6ad990c

Browse files
fix(gatsby-plugin-page-creator): support index routes when using the File System Route API (#31339)
Co-authored-by: LekoArts <[email protected]>
1 parent 844c19e commit 6ad990c

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

packages/gatsby-plugin-page-creator/src/__tests__/derive-path.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,74 @@ describe(`derive-path`, () => {
265265
).toEqual(`foo/dolores/[...name]`)
266266
})
267267

268+
it(`supports index paths`, () => {
269+
expect(
270+
derivePath(
271+
`{Page.path}`,
272+
{
273+
path: `/`,
274+
},
275+
reporter
276+
).derivedPath
277+
).toEqual(`index`)
278+
expect(
279+
derivePath(
280+
`{Page.path}.js`,
281+
{
282+
path: `/`,
283+
},
284+
reporter
285+
).derivedPath
286+
).toEqual(`index.js`)
287+
expect(
288+
derivePath(
289+
`foo/{Page.path}`,
290+
{
291+
path: `/`,
292+
},
293+
reporter
294+
).derivedPath
295+
).toEqual(`foo`)
296+
expect(
297+
derivePath(
298+
`foo/{Page.path}/bar`,
299+
{
300+
path: `/`,
301+
},
302+
reporter
303+
).derivedPath
304+
).toEqual(`foo/bar`)
305+
expect(
306+
derivePath(
307+
`foo/{Page.pathOne}/{Page.pathTwo}`,
308+
{
309+
pathOne: `/`,
310+
pathTwo: `bar`,
311+
},
312+
reporter
313+
).derivedPath
314+
).toEqual(`foo/bar`)
315+
expect(
316+
derivePath(
317+
`foo/{Page.pathOne}/{Page.pathTwo}`,
318+
{
319+
pathOne: `/`,
320+
pathTwo: `/bar`,
321+
},
322+
reporter
323+
).derivedPath
324+
).toEqual(`foo/bar`)
325+
expect(
326+
derivePath(
327+
`foo/{Page.path}/[...name]`,
328+
{
329+
path: `/`,
330+
},
331+
reporter
332+
).derivedPath
333+
).toEqual(`foo/[...name]`)
334+
})
335+
268336
it(`handles special chars`, () => {
269337
expect(
270338
derivePath(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import {
66
extractAllCollectionSegments,
77
switchToPeriodDelimiters,
88
stripTrailingSlash,
9+
removeFileExtension,
910
} from "./path-utils"
1011

1112
const doubleForwardSlashes = /\/\/+/g
13+
// Match 0 or 1 of "/"
14+
const indexRoute = /^\/?$/
1215

1316
// Generates the path for the page from the file path
1417
// product/{Product.id} => /product/:id, pulls from nodes.id
@@ -64,6 +67,14 @@ export function derivePath(
6467
// 4. Remove double forward slashes that could occur in the final URL
6568
modifiedPath = modifiedPath.replace(doubleForwardSlashes, `/`)
6669

70+
// 5. Remove trailing slashes that could occur in the final URL
71+
modifiedPath = stripTrailingSlash(modifiedPath)
72+
73+
// 6. If the final URL appears to be an index path, use the "index" file naming convention
74+
if (indexRoute.test(removeFileExtension(modifiedPath))) {
75+
modifiedPath = `index${modifiedPath}`
76+
}
77+
6778
const derivedPath = modifiedPath
6879

6980
return {

0 commit comments

Comments
 (0)