-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
60 lines (55 loc) · 1.56 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const path = require('path');
const firebase = require('firebase');
const routes = require('./templates.js');
const config = require('./firebaseConfig');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const app = firebase.initializeApp(config);
await Promise.all(
routes.map(
async ({
collection,
path: routePath,
param,
fileName,
context,
}) => {
const Template = path.resolve(
`./src/templates/${fileName}.js`,
);
await app
.firestore()
.collection(collection)
.get()
.then(async querySnapshot => {
await Promise.all(
querySnapshot.docs.map(async item => {
const itemData = item.data();
await createPage({
path: `${routePath}/${itemData[param]}`,
component: Template,
context: context.reduce((acc, curr) => {
acc[curr] = itemData[curr];
return acc;
}, {}),
});
}),
);
});
},
),
);
};
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions;
// page.matchPath is a special key that's used for matching pages
// only on the client.
routes.forEach(item => {
const regex = new RegExp(`${item.path}`, 'g');
if (page.path.match(regex)) {
page.matchPath = `${item.path}/*`;
// Update the page.
createPage(page);
}
});
};