-
Notifications
You must be signed in to change notification settings - Fork 52
/
gatsby-node.js
70 lines (62 loc) · 2.09 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const { UrlConverter } = require('./src/build-utils')
const FALLBACK_LOCALE = 'en'
//TODO(Rejon): Add in support for the case similar pages exist outside of the locale folders.
// We don't want to override pages at the top level if they exist.
//Create redirect fallbacks to default locales.
//ie If the user types /contribute and it exists in our default locale, take us to that page.
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions //actions is collection of many actions - https://www.gatsbyjs.org/docs/actions
const { data: pages } = await graphql(`
query redirects {
pages: allMdx(
filter: {
fileAbsolutePath: {
regex: "//([\\\\w]{2})/(?!header.mdx|index.mdx|sidenav.mdx|example.mdx|footer.mdx|404.mdx|.js|.json)/"
}
}
) {
edges {
node {
fileAbsolutePath
}
}
}
}
`)
pages.pages.edges.map(({ node }) => {
const noLocalePath = UrlConverter(node)
.replace(/^\/([\w]{2})\//, '/')
.replace('index', '')
createRedirect({
fromPath: noLocalePath,
toPath: `/${FALLBACK_LOCALE}${noLocalePath}`,
isPermanent: true,
})
})
}
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
// inject breadcrumbs into page context
const { context: oldPageContext } = page
//NOTE(Rejon): Pass a regex string variable for blog home pages so we can make sure we're getting the correct locale.
if (
page.path.includes('/blog/') &&
!page.componentPath.includes('/blogPosts/')
) {
oldPageContext.regex = `//blogPosts/${page.path.split('/')[1]}/` //ie. /blogPosts/en
}
deletePage(page)
createPage({
...page,
context: {
...oldPageContext,
locale: page.path.split('/')[1],
pagePath: page.path, //NOTE(Rejon): I provide this so we can have a navigational anchor during static builds for pathDirs and sidenav/breadcrumb data.
},
})
}