-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprerender.build.js
147 lines (123 loc) · 3.19 KB
/
prerender.build.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
const path = require("path");
// const Prismic = require("prismic-javascript");
const PrerenderSPAPlugin = require("prerender-spa-plugin");
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
console.log("renderer", Renderer);
// TODO: Use same routes object as router
const PAGES = {
home: {
route: "/",
title: "Plenum Journal"
},
about: {
route: "/about",
title: "About Plenum"
},
test: {
route: "/test",
title: "test Plenum"
}
};
let STATIC_ROUTES = [];
for (let key in PAGES) {
STATIC_ROUTES.push(PAGES[key].route);
}
/**
* Returns the path to the specified directory relative to the root directory
*/
function resolve(dir) {
return path.join(__dirname, `../${dir}`);
}
/**
*
*/
const getProjects = async () => {
let api;
try {
api = await Prismic.api("https://plenum-website.cdn.prismic.io/api/v2");
} catch (err) {
throw err;
}
let options = {}; // In Node.js, pass the request as 'req' to read the reference from the cookies
let response;
try {
response = await api.query(
Prismic.Predicates.at("document.type", "essay"),
options
);
} catch (err) {
throw err;
}
return response.results;
};
/**
*
*/
const getPrerenderedRoutes = async () => {
let staticRoutes = STATIC_ROUTES;
let projects;
try {
// projects = await getProjects();
projects = [];
} catch (err) {
console.error("Unable to obtain routes to prerender");
throw err;
}
const projectRoutes = projects.map(project => `/project/${project.uid}`);
return staticRoutes.concat(projectRoutes);
};
/**
*
*/
const getProductionPlugins = async () => {
let routes;
try {
routes = await getPrerenderedRoutes();
} catch (err) {
throw err;
}
let productionPlugins = [];
productionPlugins.push(
new PrerenderSPAPlugin({
staticDir: resolve("dist"),
outputDir: resolve("prerendered"),
routes: routes,
renderer: new Renderer({
// injectProperty: "__PRERENDER_INJECTED",
// We need to inject a value so we're able to
// detect if the page is currently pre-rendered.
inject: {
prerendered: true
},
headless: false,
devtools: true,
// Our view component is rendered after the API
// request has fetched all the necessary data,
// so we create a snapshot of the page after the
// `data-view` attribute exists in the DOM.
// renderAfterElementExists: "[data-view]",
// https://snipcart.com/blog/vue-js-seo-prerender-example
renderAfterDocumentEvent: "app.rendered"
// Options
// TODO: construct titles from project names?
// postProcess(context) {
// let titles = {
// "/": "My home page",
// "/about": "My awesome about page",
// "/contact": "Contact me"
// };
// context.html = context.html.replace(
// /<title>[^<]*<\/title>/i,
// `<title>${titles[context.route]}</title>`
// );
// return context;
// }
})
})
);
console.log(productionPlugins);
return productionPlugins;
};
module.exports = {
plugins: getProductionPlugins()
};