Skip to content

Commit ca367d9

Browse files
committed
fix(node): allow passing extra pages context to createAdvancedPage()
1 parent 104acf8 commit ca367d9

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ Check out [example](https://github.com/mohatt/gatsby-plugin-advanced-pages/tree/
411411
These are the functions exposed by the plugin.
412412

413413
#### createAdvancedPage
414-
> `createAdvancedPage({ route: string, params?: object, pagination?: object, filter?: object }): void`
414+
> `createAdvancedPage({ route: string, params?: object, pagination?: object, ...context }): void`
415415
416416
Creates page(s) based on given input paramaters. *Note: This function can only be called within [Page helpers](#page-helpers).*
417417

src/gatsby/__tests__/__snapshots__/create-pages.js.snap

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,51 @@ module.exports = [
200200
];"
201201
`;
202202

203+
exports[`createPages correctly creates pages on (page-context) 1`] = `
204+
Array [
205+
Array [
206+
Object {
207+
"component": "/virtual/project/src/templates/page.js",
208+
"context": Object {
209+
"id": "about-id",
210+
"name": "adam",
211+
"profile": Object {
212+
"age": 25,
213+
"gender": "male",
214+
},
215+
},
216+
"path": "/about/adam",
217+
},
218+
],
219+
Array [
220+
Object {
221+
"component": "/virtual/project/src/templates/page.js",
222+
"context": Object {
223+
"id": "about-id",
224+
"name": "sara",
225+
"profile": Object {
226+
"age": 28,
227+
"gender": "female",
228+
},
229+
},
230+
"path": "/about/sara",
231+
},
232+
],
233+
]
234+
`;
235+
236+
exports[`createPages correctly creates pages on (page-context) 2`] = `
237+
"\\"use strict\\";
238+
239+
module.exports = [
240+
{
241+
\\"name\\": \\"about\\",
242+
\\"path\\": \\"/about/:name\\",
243+
\\"scopes\\": {}
244+
}
245+
];"
246+
`;
247+
203248
exports[`createPages throws error on (duplicated-routes) 1`] = `[Error: 'pages/about2.md' is trying to define a route named 'about' that is already defined in 'pages/about.md']`;
204249

205250
exports[`createPages throws error on (invalid-helper) 1`] = `[Error: Invalid helper 'whatever' defined in page 'pages/about.md': File does not exist at '/virtual/project/gatsby/pages/whatever.js']`;

src/gatsby/lib/pages-creator.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export default class PagesCreator {
123123
}
124124
}
125125

126-
createPage ({ route, params = {}, pagination, filter }) {
126+
createPage ({ route, params = {}, pagination, ...context }) {
127127
const { page } = this
128128
if (typeof route !== 'string' || !route) {
129129
throw new TypeError(
@@ -144,14 +144,11 @@ export default class PagesCreator {
144144
component: page.templatePath,
145145
context: {
146146
id: page.id,
147-
...params
147+
...params,
148+
...context
148149
}
149150
}
150151

151-
if (filter) {
152-
gatsbyPage.context.filter = filter
153-
}
154-
155152
if (pagination) {
156153
if (typeof pagination.count === 'undefined') {
157154
throw new TypeError(

test/__fixtures__/create-pages.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,49 @@ export default [
195195
}
196196
}
197197
}
198+
},
199+
{
200+
id: 'page-context',
201+
routes: [
202+
{ name: 'about', path: '/about/:name', page: { path: 'pages/about.md' } },
203+
],
204+
pages: [
205+
{
206+
id: 'about-id',
207+
path: 'pages/about.md',
208+
template: 'page',
209+
helper: 'about',
210+
routes: [
211+
{ name: 'about', path: '/about' }
212+
]
213+
}
214+
],
215+
files: {
216+
templates: [ 'page' ],
217+
helpers: {
218+
about: function ({ createAdvancedPage }) {
219+
createAdvancedPage({
220+
route: 'about',
221+
params: {
222+
name: 'adam'
223+
},
224+
profile: {
225+
gender: 'male',
226+
age: 25
227+
}
228+
})
229+
createAdvancedPage({
230+
route: 'about',
231+
params: {
232+
name: 'sara'
233+
},
234+
profile: {
235+
gender: 'female',
236+
age: 28
237+
}
238+
})
239+
}
240+
}
241+
}
198242
}
199243
]

0 commit comments

Comments
 (0)