-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
158 lines (145 loc) · 4.42 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
const path = require(`path`);
const { slash } = require(`gatsby-core-utils`);
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
// Will create pages for WordPress pages (route : /{slug})
// Will create pages for WordPress posts (route : /post/{slug})
exports.createPages = async ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
// TODO
// TODO NOTE: WHEN DOING THIS FOR PRODUCTION CONSIDER SPLITTING THAT ONE QUERY
// TODO OUT INTO THREE QUERIES, PROCESSING EACH INDIVIDUALLY
// TODO
// The “graphql” function allows us to run arbitrary
// queries against the local Gatsby GraphQL schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.
const result = await graphql(`
{
allWordpressPage {
edges {
node {
id
slug
status
template
title
content
}
}
}
allWordpressWpPortfolio {
edges {
node {
id
title
slug
excerpt
content
featured_media {
source_url
}
acf {
portfolio_url
}
}
}
}
allWordpressPost {
edges {
node {
title
content
excerpt
wordpress_id
date(formatString: "Do MMM YYYY HH:mm")
slug
}
}
}
}
`);
// Check for any errors
if (result.errors) {
throw new Error(result.errors);
}
// Since WP must have a slug for each page, use the home page for root index
createRedirect({
fromPath: "/",
toPath: "/home",
redirectInBrowser: true,
isPermanent: true,
});
// Access query results via object destructuring
const {
allWordpressPage,
allWordpressWpPortfolio,
allWordpressPost,
} = result.data;
// Create Page pages.
const pageTemplate = path.resolve(`./src/templates/page.js`);
const portfolioUnderContentTemplate = path.resolve(
`./src/templates/portfolioUnderContent.js`
);
// We want to create a detailed page for each page node.
// The path field contains the relative original WordPress link
// and we use it for the slug to preserve url structure.
// The Page ID is prefixed with 'PAGE_'
allWordpressPage.edges.forEach(edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
let currentTemplate =
edge.node.template === "portfolio_under_content.php"
? portfolioUnderContentTemplate
: pageTemplate;
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: `/${edge.node.slug}/`,
component: slash(currentTemplate),
context: edge.node,
});
});
const portfolioTemplate = path.resolve(`./src/templates/portfolio.js`);
// We want to create a detailed page for each post node.
// The path field stems from the original WordPress link
// and we use it for the slug to preserve url structure.
// The Post ID is prefixed with 'POST_'
allWordpressWpPortfolio.edges.forEach(edge => {
createPage({
path: `/portfolio/${edge.node.slug}/`,
component: slash(portfolioTemplate),
context: edge.node,
});
});
const blogPostListTemplate = path.resolve(`./src/templates/blogPostList.js`);
const posts = allWordpressPost.edges;
const postsPerPage = 2;
const numberOfPages = Math.ceil(posts.length / postsPerPage);
posts.forEach((post, index) => {
// create the blog list page
createPage({
path: index === 0 ? `/blog/` : `/blog/${index + 1}/`,
component: slash(blogPostListTemplate),
context: {
posts: posts.slice(
index * postsPerPage,
index * postsPerPage + postsPerPage
),
numberOfPages,
currentPage: index + 1,
},
});
// create post page
createPage({
path: `/post/${post.node.slug}/`,
component: slash(pageTemplate),
context: post.node,
});
});
};