-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
115 lines (98 loc) · 2.55 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
const path = require('path');
// this is copied from gatsby-plugin-remove-trailing-slashes because it causes the build to fail if it is used direcly
const removeTrailingSlash = _path =>
_path === '/' ? _path : _path.replace(/\/$/, '');
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
});
};
// workaround because gatsby seems to have issues with using ES module imports
// and mixing of CJS and ES does not work
const languages = {
de: 'Deutsch',
en: 'English',
};
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
const blogPostTemplate = path.resolve(`src/templates/blogTemplate.js`);
return graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
frontmatter {
path
locale
originalPath
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
return result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const context = {
languages,
locale: node.frontmatter.locale,
routed: true,
originalPath: node.frontmatter.originalPath,
};
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context,
});
});
});
};
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
// if (page.path.includes('404')) {
// return Promise.resolve();
// }
if (!page.componentPath.includes('src/pages')) {
return Promise.resolve();
}
const oldPage = Object.assign({}, page);
page.path = removeTrailingSlash(page.path);
return new Promise(resolve => {
const redirect = path.resolve('./src/i18n/Redirect.js');
const redirectPage = {
...page,
component: redirect,
path: '/',
context: {
languages,
locale: 'en',
routed: false,
redirectPage: page.path,
},
};
createPage(redirectPage);
if (page.path !== oldPage.path) {
deletePage(oldPage);
}
Object.keys(languages).forEach(locale => {
const context = {
languages,
locale,
routed: true,
originalPath: page.path,
};
const localePage = {
...page,
originalPath: page.path,
path: `/${locale}${page.path}`,
context,
};
createPage(localePage);
});
resolve();
});
};