-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
238 lines (221 loc) · 5.63 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const path = require('path');
const slash = require('slash');
const fs = require('fs');
const metadata = require('./gatsby-config.js').siteMetadata;
const util = require('util');
exports.createPages = async ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
const indexTemplate = path.resolve('src/pages/index.jsx');
const jobTemplate = path.resolve('src/templates/JobTemplate.jsx');
const partnerTemplate = path.resolve('src/templates/PartnerTemplate.jsx');
const pageTemplate = path.resolve('src/templates/PageTemplate.jsx');
const boardTemplate = path.resolve('src/templates/BoardTemplate.jsx');
const newsTemplate = path.resolve('src/templates/NewsTemplate.jsx');
const disputeTemplate = path.resolve(`src/templates/DisputeTemplate.jsx`);
const committeeTemplate = path.resolve(`src/templates/CommitteeTemplate.jsx`);
const staticFolder = `src/static-pages`;
const query = await graphql(`
query PagesQuery {
allContentfulJobListing {
edges {
node {
id
node_locale
slug
}
}
}
allContentfulPartner {
edges {
node {
id
node_locale
slug
}
}
}
allContentfulPage {
edges {
node {
id
node_locale
title
slug
parentPage {
slug
}
}
}
}
allContentfulBoard {
edges {
node {
id
node_locale
number
}
}
}
allContentfulNewsArticles {
edges {
node {
id
node_locale
slug
}
}
}
allContentfulDispute {
edges {
node {
id
node_locale
slug
}
}
}
allContentfulCommittee {
edges {
node {
id
node_locale
slug
}
}
}
}
`);
function createTemplatePage(lg, url, templatePath, id) {
// Create page without language for default language
if (lg === metadata.defaultLocale) {
createPage({
path: url,
component: slash(templatePath),
context: {
id: id,
},
});
}
createPage({
path: `${lg}${url}`,
component: slash(templatePath),
context: {
id: id,
},
});
}
function createStaticPages(folder, location) {
fs.readdirSync(folder).forEach(file => {
if (fs.statSync(`${folder}/${file}`).isDirectory())
return createStaticPages(`${folder}/${file}`, file);
let baseUrl = `${location}/${file}`.split('.')[0]; // Get the path before file extension
if (baseUrl === '/index') baseUrl = '';
else if (baseUrl === '/404') return;
// Create a page for each language
metadata.languages.forEach(lang => {
createTemplatePage(
lang,
`/${baseUrl}`,
path.resolve(`${folder}/${file}`),
null
);
});
});
}
if (query.errors) {
throw new Error(JSON.stringify(query.errors));
} else {
// Create all static pages
createStaticPages(staticFolder, '');
// Create jobpages
await Promise.all(
query.data.allContentfulJobListing.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/vacatures/${node.slug}`,
jobTemplate,
node.id
);
})
);
// Create partnerpages
await Promise.all(
query.data.allContentfulPartner.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/partners/${node.slug}`,
partnerTemplate,
node.id
);
})
);
// Create boardpages
await Promise.all(
query.data.allContentfulBoard.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/besturen/${node.number}`,
boardTemplate,
node.id
);
})
);
await Promise.all(
query.data.allContentfulNewsArticles.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/news/${node.slug}`,
newsTemplate,
node.id
);
})
);
console.log('before general');
// Create general pages
await Promise.all(
query.data.allContentfulPage.edges.map(async ({ node }) => {
let url;
if (node.parentPage) {
url = node.parentPage.slug + '/' + node.slug;
} else {
url = node.slug;
}
const localPath = path.resolve('src', 'static-pages', url + '.jsx');
// Check asynchronously if we have the page locally.
try {
await fs.promises.access(localPath, fs.R_OK);
} catch (err) {
if (err) {
createTemplatePage(
node.node_locale,
`/${url}`,
pageTemplate,
node.id
);
}
}
})
);
console.log('general finished');
await Promise.all(
query.data.allContentfulDispute.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/disputen/${node.slug}`,
disputeTemplate,
node.id
);
})
);
await Promise.all(
query.data.allContentfulCommittee.edges.map(async ({ node }) => {
createTemplatePage(
node.node_locale,
`/commissies/${node.slug}`,
committeeTemplate,
node.id
);
})
);
}
};