-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
202 lines (182 loc) · 5.02 KB
/
Copy pathgatsby-node.js
File metadata and controls
202 lines (182 loc) · 5.02 KB
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
/* eslint-disable no-console */
const fs = require("fs");
const path = require("path");
const md5 = require("md5");
const util = require("util");
const glob = require("glob");
const pageSize = 6;
/**
* Helper for creating paginated pages based on categories.
*
* @param {*} data
* @param {*} createPage
*/
const createPaginationPages = (categories, createPage) => {
const { categorizedPosts } = categories;
// Loop through the categories
// eslint-disable-next-line array-callback-return
categorizedPosts.map((category) => {
const pageCount = Math.ceil(category.edges.length / pageSize);
const { fieldValue: categoryName } = category;
let template;
let templateFull;
let niceHeader;
// Switch based on a category slug
switch (categoryName) {
/* case "project-workshops":
template = "./src/components/Pages/Categories/ProjectWorkshops.jsx";
break; */
case "podcasts":
niceHeader = "podcasts";
template = "./src/components/Pages/Categories/MediaCategory.jsx";
break;
case "printed-media":
niceHeader = "printed";
template = "./src/components/Pages/Categories/MediaCategory.jsx";
break;
case "videos":
niceHeader = "videos";
template = "./src/components/Pages/Categories/MediaCategory.jsx";
break;
case "news":
template = "./src/components/Pages/Categories/News.jsx";
templateFull = "./src/components/Pages/Categories/NewsFull.jsx";
break;
default:
template = false;
templateFull = false;
}
/**
* Create category pagination
*/
if (template) {
Array.from({ length: pageCount }).forEach((_, i) => {
createPage({
path: i === 0 ? `/${categoryName}` : `/${categoryName}/${i + 1}`,
component: path.resolve(template),
context: {
skip: i * pageSize,
limit: pageSize,
pageCount,
currentPage: i + 1,
base: categoryName,
niceHeader,
},
});
});
}
/**
* Create permalinks.
*/
if (templateFull) {
category.edges.map((r) => {
const urlHandle = `/${categoryName}/${r.node.slug}`;
return createPage({
path: urlHandle,
component: path.resolve(templateFull),
context: {
slug: r.node.slug,
id: r.node.id,
},
});
});
}
});
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const result = await graphql(`
{
pages: allWpContentNode(filter: { nodeType: { eq: "Page" } }) {
nodes {
id
uri
slug
__typename
}
}
categories: allWpPost {
categorizedPosts: group(field: categories___nodes___slug) {
fieldValue
edges {
node {
id
title
date(formatString: "MMMM DD, YYYY")
content
slug
}
}
}
}
}
`);
if (result.errors) {
reporter.error("There was an error fetching posts", result.errors);
}
const { pages, categories } = result.data;
/**
* Categories
*/
createPaginationPages(categories, actions.createPage);
/**
* Page nodes
*/
if (pages.nodes.length) {
// eslint-disable-next-line array-callback-return
pages.nodes.map((node) => {
let template;
// eslint-disable-next-line no-underscore-dangle
const templatePath = `./src/components/Pages/Page/Page.jsx`;
try {
if (fs.existsSync(templatePath)) {
template = require.resolve(templatePath);
}
} catch (err) {
console.error(err);
}
if (template && node.slug) {
actions.createPage({
// It's best practice to use the uri field from WPGraphQL nodes when
// building
path: node.slug,
component: template,
context: {
slug: node.slug,
id: node.id,
},
});
}
});
}
};
/**
* Cache busting
*/
const hash = md5(`${new Date().getTime()}`);
const addPageDataVersion = async (file) => {
const stats = await util.promisify(fs.stat)(file);
if (stats.isFile()) {
console.log(`Adding version to page-data.json in ${file}..`);
const content = await util.promisify(fs.readFile)(file, "utf8");
const result = content.replace(
/page-data.json(\?v=[a-f0-9]{32})?/g,
`page-data.json?v=${hash}`
);
await util.promisify(fs.writeFile)(file, result, "utf8");
}
};
exports.onPostBootstrap = async () => {
const loader = path.join(
__dirname,
"node_modules/gatsby/cache-dir/loader.js"
);
await addPageDataVersion(loader);
};
exports.onPostBuild = async () => {
const publicPath = path.join(__dirname, "public");
const htmlAndJSFiles = glob.sync(`${publicPath}/**/*.{html,js}`);
for (const file of htmlAndJSFiles) {
// eslint-disable-next-line no-await-in-loop
await addPageDataVersion(file);
}
};