-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
168 lines (150 loc) · 4.27 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
const path = require('path');
const webpack = require('webpack');
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
return new Promise((resolve, reject) => {
const storyblokEntry = path.resolve('src/templates/storyblok-entry.js');
resolve(
graphql(
`{
allStoryblokEntry {
edges {
node {
id
name
created_at
uuid
slug
full_slug
content
is_startpage
parent_id
group_id
}
}
}
}`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const entries = result.data.allStoryblokEntry.edges
entries.forEach((entry, index) => {
let slug = `${entry.node.full_slug}`
slug = slug.replace(/^\/|\/$/g, '')
let pagePath = entry.node.full_slug == 'home' ? '' : slug + '/'
// Wire up the 404 page by setting the path to just 404 as Gatsby expects it.
if (pagePath.match(/^404/)) {
pagePath = "404"
}
// Wire up the 403 page by setting the path to just 403 as Gatsby expects it.
if (pagePath.match(/^403/)) {
pagePath = "403"
}
// Determine if the page is canonical, or is using a custom canonical URL.
const content = JSON.parse(entry.node.content);
let isCanonical = true;
if (content.canonicalURL && (content.canonicalURL.url || content.canonicalURL.cached_url)) {
isCanonical = false;
}
createPage({
path: '/' + pagePath,
component: storyblokEntry,
context: {
story: entry.node,
isCanonical: isCanonical,
noindex: !!content.noindex,
}
})
})
})
)
// Add Redirects pre-configured in Storyblok.
resolve(
graphql(
`{
allStoryblokEntry(filter: {field_enabled_boolean: {eq: true}, field_component: {eq: "redirect"}}) {
edges {
node {
name
field_to_string
field_from_string
field_enabled_boolean
field_statusCode_string
field_component
}
}
}
}`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const entries = result.data.allStoryblokEntry.edges;
entries.forEach((entry, index) => {
createRedirect({
fromPath: entry.node.field_from_string,
toPath: entry.node.field_to_string,
force: true,
redirectInBrowser: false,
statusCode: Number(entry.node.field_statusCode_string),
})
})
})
)
})
}
// Alter Gatsby's webpack config.
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
getConfig
}) => {
// override config only during
// production JS & CSS build
if (stage === 'build-javascript') {
// get current webpack config
const config = getConfig()
// find CSS minimizer
const minifyCssIndex = config.optimization.minimizer.findIndex(
minimizer => minimizer.constructor.name ===
'CssMinimizerPlugin'
)
// if found, overwrite existing CSS minimizer preset svgo plugin options.
if (minifyCssIndex > -1) {
delete config.optimization.minimizer[minifyCssIndex].options.minimizerOptions.preset[1].svgo.plugins;
}
// replace webpack config with the modified object
actions.replaceWebpackConfig(config)
}
actions.setWebpackConfig({
resolve: {
fallback: {
path: require.resolve("path-browserify"),
fs: false,
}
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
}),
]
})
if (stage === 'build-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /sa11y/,
use: loaders.null(),
},
],
},
});
}
}