Skip to content

Commit c8c2f32

Browse files
committed
feat(plugin): support custom template args per page
1 parent 30b8d8f commit c8c2f32

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ These are the functions exposed by the plugin.
440440

441441
### createAdvancedPage
442442

443-
> `({ route: string, params?: object, pagination?: object, ...context: any[] }): void`
443+
> `({ route: string, params?: object, templateArgs?: object, pagination?: object, ...context: any[] }): void`
444444

445445
Creates page(s) based on given input parameters. _Note: This function can only be called within [Page helpers](#page-helpers)._
446446

src/node/__tests__/__fixtures__/create-pages.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,30 @@ export default <
275275
})
276276
},
277277
},
278+
{
279+
title: 'correctly creates pages (dynamic-template-args)',
280+
pages: [
281+
{
282+
id: 'page-id',
283+
template: '/path/to/page.js',
284+
helper,
285+
routes: [{ name: 'page', path: '/pages/:page' }],
286+
},
287+
],
288+
helper: ({ createAdvancedPage }) => {
289+
for (const slug of ['hello', 'world']) {
290+
createAdvancedPage({
291+
route: 'page',
292+
params: {
293+
page: slug,
294+
},
295+
templateArgs: {
296+
contentPath: `content/${slug}.mdx`,
297+
},
298+
})
299+
}
300+
},
301+
},
278302
{
279303
title: 'correctly creates pages (dynamic-page)',
280304
pages: [

src/node/__tests__/__snapshots__/create-pages.ts.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ exports[`createPages > 'correctly creates pages (dynamic-page)' > createPage 1`]
162162

163163
exports[`createPages > 'correctly creates pages (dynamic-page)' > routes.json 1`] = `"[{"name":"page","path":"/pages/:page","scopes":{}}]"`;
164164

165+
exports[`createPages > 'correctly creates pages (dynamic-template-args)' > createPage 1`] = `
166+
[
167+
[
168+
{
169+
"component": "/path/to/page.js?contentPath=content%2Fhello.mdx",
170+
"context": {
171+
"id": "page-id",
172+
"page": "hello",
173+
},
174+
"path": "/pages/hello",
175+
},
176+
],
177+
[
178+
{
179+
"component": "/path/to/page.js?contentPath=content%2Fworld.mdx",
180+
"context": {
181+
"id": "page-id",
182+
"page": "world",
183+
},
184+
"path": "/pages/world",
185+
},
186+
],
187+
]
188+
`;
189+
190+
exports[`createPages > 'correctly creates pages (dynamic-template-args)' > routes.json 1`] = `"[{"name":"page","path":"/pages/:page","scopes":{}}]"`;
191+
165192
exports[`createPages > 'correctly creates pages (home-about)' > createPage 1`] = `
166193
[
167194
[

src/node/api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ export interface CreateAdvancedPageProps {
149149
*/
150150
params?: RouteParams
151151

152+
/**
153+
* Additional arguments to be passed to the page template.
154+
*/
155+
templateArgs?: {
156+
[k: string]: string | number
157+
}
158+
152159
/**
153160
* Optional pagination settings for paginated pages.
154161
*/

src/node/lib/pages-creator.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ export default class PagesCreator {
106106
/**
107107
* Creates a Gatsby page with optional pagination.
108108
*/
109-
private createPage({ route, params = {}, pagination, ...context }: CreateAdvancedPageProps) {
109+
private createPage({
110+
route,
111+
params = {},
112+
templateArgs,
113+
pagination,
114+
...context
115+
}: CreateAdvancedPageProps) {
110116
const { currentPage } = this
111117
if (typeof route !== 'string' || !route) {
112118
throw new PageHelperError('Route name', ' must be a non-empty string')
@@ -117,9 +123,15 @@ export default class PagesCreator {
117123
throw new PageHelperError(`Unrecognized route "${route}"`)
118124
}
119125

126+
const template = currentPage.template
127+
const templateQuery =
128+
templateArgs &&
129+
new URLSearchParams(Object.entries(templateArgs).map(([k, v]) => [k, String(v)])).toString()
130+
const templateUrl = templateQuery ? `${template}?${templateQuery}` : template
131+
120132
const gatsbyPage: Page = {
121133
path: routeNode.pathGenerator(params),
122-
component: currentPage.template,
134+
component: templateUrl,
123135
context: {
124136
id: currentPage.id,
125137
...params,

test/setup-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Use virtual file system for testing (see test/__mocks__/fs)
1+
// Use virtual file system for testing (see /__mocks__/fs)
22
vi.mock('fs')
33
vi.mock('fs/promises')
44

0 commit comments

Comments
 (0)