|
1 | | -const path = require(`path`); |
2 | | -const { postsPerPage } = require(`./src/utils/siteConfig`); |
3 | | -const { paginate } = require(`gatsby-awesome-pagination`); |
4 | | - |
5 | | -/** |
6 | | - * Here is the place where Gatsby creates the URLs for all the |
7 | | - * posts, tags, pages and authors that we fetched from the Ghost site. |
8 | | - */ |
9 | | -exports.createPages = async ({ graphql, actions }) => { |
10 | | - const { createPage } = actions; |
11 | | - |
12 | | - const result = await graphql(` |
13 | | - { |
14 | | - allGhostPost(sort: { order: ASC, fields: published_at }) { |
15 | | - edges { |
16 | | - node { |
17 | | - slug |
18 | | - } |
19 | | - } |
20 | | - } |
21 | | - allGhostTag(sort: { order: ASC, fields: name }) { |
22 | | - edges { |
23 | | - node { |
24 | | - slug |
25 | | - url |
26 | | - postCount |
27 | | - } |
28 | | - } |
29 | | - } |
30 | | - allGhostAuthor(sort: { order: ASC, fields: name }) { |
31 | | - edges { |
32 | | - node { |
33 | | - slug |
34 | | - url |
35 | | - postCount |
36 | | - } |
37 | | - } |
38 | | - } |
39 | | - allGhostPage(sort: { order: ASC, fields: published_at }) { |
40 | | - edges { |
41 | | - node { |
42 | | - slug |
43 | | - url |
44 | | - } |
45 | | - } |
46 | | - } |
47 | | - } |
48 | | - `); |
49 | | - |
50 | | - // Check for any errors |
51 | | - if (result.errors) { |
52 | | - throw new Error(result.errors); |
53 | | - } |
54 | | - |
55 | | - // Extract query results |
56 | | - const tags = result.data.allGhostTag.edges; |
57 | | - const authors = result.data.allGhostAuthor.edges; |
58 | | - const pages = result.data.allGhostPage.edges; |
59 | | - const posts = result.data.allGhostPost.edges; |
60 | | - |
61 | | - // Load templates |
62 | | - const indexTemplate = path.resolve(`./src/templates/index.js`); |
63 | | - const tagsTemplate = path.resolve(`./src/templates/tag.js`); |
64 | | - const authorTemplate = path.resolve(`./src/templates/author.js`); |
65 | | - const pageTemplate = path.resolve(`./src/templates/page.js`); |
66 | | - const postTemplate = path.resolve(`./src/templates/post.js`); |
67 | | - const blogsTemplate = path.resolve(`./src/templates/blogs.js`); |
68 | | - |
69 | | - paginate({ |
70 | | - createPage, |
71 | | - items: posts, |
72 | | - itemsPerPage: postsPerPage, |
73 | | - component: blogsTemplate, |
74 | | - pathPrefix: "blogs", |
75 | | - context: { |
76 | | - pathPrefix: "blogs", |
77 | | - // slug: node.slug, |
78 | | - }, |
79 | | - }); |
80 | | - |
81 | | - // Create tag pages |
82 | | - tags.forEach(({ node }) => { |
83 | | - const totalPosts = node.postCount !== null ? node.postCount : 0; |
84 | | - |
85 | | - // This part here defines, that our tag pages will use |
86 | | - // a `/tag/:slug/` permalink. |
87 | | - const url = `tag/${node.slug}`; |
88 | | - |
89 | | - const items = Array.from({ length: totalPosts }); |
90 | | - |
91 | | - // Create pagination |
92 | | - paginate({ |
93 | | - createPage, |
94 | | - items: items, |
95 | | - itemsPerPage: postsPerPage, |
96 | | - component: tagsTemplate, |
97 | | - pathPrefix: url, |
98 | | - context: { |
99 | | - pathPrefix: url, |
100 | | - slug: node.slug, |
101 | | - }, |
102 | | - }); |
103 | | - }); |
104 | | - |
105 | | - // Create author pages |
106 | | - authors.forEach(({ node }) => { |
107 | | - const totalPosts = node.postCount !== null ? node.postCount : 0; |
108 | | - |
109 | | - // This part here defines, that our author pages will use |
110 | | - // a `/author/:slug/` permalink. |
111 | | - const url = `author/${node.slug}`; |
112 | | - |
113 | | - const items = Array.from({ length: totalPosts }); |
114 | | - |
115 | | - // Create pagination |
116 | | - paginate({ |
117 | | - createPage, |
118 | | - items: items, |
119 | | - itemsPerPage: postsPerPage, |
120 | | - component: authorTemplate, |
121 | | - pathPrefix: url, |
122 | | - context: { |
123 | | - pathPrefix: url, |
124 | | - slug: node.slug, |
125 | | - }, |
126 | | - }); |
127 | | - }); |
128 | | - |
129 | | - // Create pages |
130 | | - pages.forEach(({ node }) => { |
131 | | - // This part here defines, that our pages will use |
132 | | - // a `/:slug/` permalink. |
133 | | - node.url = `/${node.slug}/`; |
134 | | - |
135 | | - createPage({ |
136 | | - path: node.url, |
137 | | - component: pageTemplate, |
138 | | - context: { |
139 | | - // Data passed to context is available |
140 | | - // in page queries as GraphQL variables. |
141 | | - slug: node.slug, |
142 | | - }, |
143 | | - }); |
144 | | - }); |
145 | | - |
146 | | - // Create post pages |
147 | | - posts.forEach(({ node }) => { |
148 | | - // This part here defines, that our posts will use |
149 | | - // a `/:slug/` permalink. |
150 | | - node.url = `/${node.slug}/`; |
151 | | - |
152 | | - createPage({ |
153 | | - path: node.url, |
154 | | - component: postTemplate, |
155 | | - context: { |
156 | | - // Data passed to context is available |
157 | | - // in page queries as GraphQL variables. |
158 | | - slug: node.slug, |
159 | | - }, |
160 | | - }); |
161 | | - }); |
162 | | - |
163 | | - // Create pagination |
164 | | - paginate({ |
165 | | - createPage, |
166 | | - items: posts, |
167 | | - itemsPerPage: postsPerPage, |
168 | | - component: indexTemplate, |
169 | | - pathPrefix: ({ pageNumber }) => { |
170 | | - if (pageNumber === 0) { |
171 | | - return `/`; |
172 | | - } else { |
173 | | - return `/page`; |
174 | | - } |
175 | | - }, |
176 | | - }); |
177 | | -}; |
178 | | - |
179 | | -exports.onCreateWebpackConfig = ({ stage, actions }) => { |
180 | | - actions.setWebpackConfig({ |
181 | | - resolve: { |
182 | | - fallback: { url: require.resolve("url/") }, |
183 | | - }, |
184 | | - }); |
185 | | -}; |
| 1 | +const path = require(`path`); |
| 2 | +const { postsPerPage } = require(`./src/utils/siteConfig`); |
| 3 | +const { paginate } = require(`gatsby-awesome-pagination`); |
| 4 | + |
| 5 | +/** |
| 6 | + * Here is the place where Gatsby creates the URLs for all the |
| 7 | + * posts, tags, pages and authors that we fetched from the Ghost site. |
| 8 | + */ |
| 9 | +exports.createPages = async ({ graphql, actions }) => { |
| 10 | + const { createPage } = actions; |
| 11 | + |
| 12 | + const result = await graphql(` |
| 13 | + { |
| 14 | + allGhostPost(sort: { order: ASC, fields: published_at }) { |
| 15 | + edges { |
| 16 | + node { |
| 17 | + slug |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + allGhostTag(sort: { order: ASC, fields: name }) { |
| 22 | + edges { |
| 23 | + node { |
| 24 | + slug |
| 25 | + url |
| 26 | + postCount |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + allGhostAuthor(sort: { order: ASC, fields: name }) { |
| 31 | + edges { |
| 32 | + node { |
| 33 | + slug |
| 34 | + url |
| 35 | + postCount |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + allGhostPage(sort: { order: ASC, fields: published_at }) { |
| 40 | + edges { |
| 41 | + node { |
| 42 | + slug |
| 43 | + url |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + `); |
| 49 | + |
| 50 | + // Check for any errors |
| 51 | + if (result.errors) { |
| 52 | + throw new Error(result.errors); |
| 53 | + } |
| 54 | + |
| 55 | + // Extract query results |
| 56 | + const tags = result.data.allGhostTag.edges; |
| 57 | + const authors = result.data.allGhostAuthor.edges; |
| 58 | + const pages = result.data.allGhostPage.edges; |
| 59 | + const posts = result.data.allGhostPost.edges; |
| 60 | + |
| 61 | + // Load templates |
| 62 | + const indexTemplate = path.resolve(`./src/templates/index.js`); |
| 63 | + const tagsTemplate = path.resolve(`./src/templates/tag.js`); |
| 64 | + const authorTemplate = path.resolve(`./src/templates/author.js`); |
| 65 | + const pageTemplate = path.resolve(`./src/templates/page.js`); |
| 66 | + const postTemplate = path.resolve(`./src/templates/post.js`); |
| 67 | + const blogsTemplate = path.resolve(`./src/templates/blogs.js`); |
| 68 | + |
| 69 | + paginate({ |
| 70 | + createPage, |
| 71 | + items: posts, |
| 72 | + itemsPerPage: postsPerPage, |
| 73 | + component: blogsTemplate, |
| 74 | + pathPrefix: "posts", |
| 75 | + context: { |
| 76 | + pathPrefix: "posts", |
| 77 | + // slug: node.slug, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + // Create tag pages |
| 82 | + tags.forEach(({ node }) => { |
| 83 | + const totalPosts = node.postCount !== null ? node.postCount : 0; |
| 84 | + |
| 85 | + // This part here defines, that our tag pages will use |
| 86 | + // a `/tag/:slug/` permalink. |
| 87 | + const url = `tag/${node.slug}`; |
| 88 | + |
| 89 | + const items = Array.from({ length: totalPosts }); |
| 90 | + |
| 91 | + // Create pagination |
| 92 | + paginate({ |
| 93 | + createPage, |
| 94 | + items: items, |
| 95 | + itemsPerPage: postsPerPage, |
| 96 | + component: tagsTemplate, |
| 97 | + pathPrefix: url, |
| 98 | + context: { |
| 99 | + pathPrefix: url, |
| 100 | + slug: node.slug, |
| 101 | + }, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + // Create author pages |
| 106 | + authors.forEach(({ node }) => { |
| 107 | + const totalPosts = node.postCount !== null ? node.postCount : 0; |
| 108 | + |
| 109 | + // This part here defines, that our author pages will use |
| 110 | + // a `/author/:slug/` permalink. |
| 111 | + const url = `author/${node.slug}`; |
| 112 | + |
| 113 | + const items = Array.from({ length: totalPosts }); |
| 114 | + |
| 115 | + // Create pagination |
| 116 | + paginate({ |
| 117 | + createPage, |
| 118 | + items: items, |
| 119 | + itemsPerPage: postsPerPage, |
| 120 | + component: authorTemplate, |
| 121 | + pathPrefix: url, |
| 122 | + context: { |
| 123 | + pathPrefix: url, |
| 124 | + slug: node.slug, |
| 125 | + }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + // Create pages |
| 130 | + pages.forEach(({ node }) => { |
| 131 | + // This part here defines, that our pages will use |
| 132 | + // a `/:slug/` permalink. |
| 133 | + node.url = `/${node.slug}/`; |
| 134 | + |
| 135 | + createPage({ |
| 136 | + path: node.url, |
| 137 | + component: pageTemplate, |
| 138 | + context: { |
| 139 | + // Data passed to context is available |
| 140 | + // in page queries as GraphQL variables. |
| 141 | + slug: node.slug, |
| 142 | + }, |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + // Create post pages |
| 147 | + posts.forEach(({ node }) => { |
| 148 | + // This part here defines, that our posts will use |
| 149 | + // a `/:slug/` permalink. |
| 150 | + node.url = `/${node.slug}/`; |
| 151 | + |
| 152 | + createPage({ |
| 153 | + path: node.url, |
| 154 | + component: postTemplate, |
| 155 | + context: { |
| 156 | + // Data passed to context is available |
| 157 | + // in page queries as GraphQL variables. |
| 158 | + slug: node.slug, |
| 159 | + }, |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + // Create pagination |
| 164 | + paginate({ |
| 165 | + createPage, |
| 166 | + items: posts, |
| 167 | + itemsPerPage: postsPerPage, |
| 168 | + component: indexTemplate, |
| 169 | + pathPrefix: ({ pageNumber }) => { |
| 170 | + if (pageNumber === 0) { |
| 171 | + return `/`; |
| 172 | + } else { |
| 173 | + return `/page`; |
| 174 | + } |
| 175 | + }, |
| 176 | + }); |
| 177 | +}; |
| 178 | + |
| 179 | +exports.onCreateWebpackConfig = ({ stage, actions }) => { |
| 180 | + actions.setWebpackConfig({ |
| 181 | + resolve: { |
| 182 | + fallback: { url: require.resolve("url/") }, |
| 183 | + }, |
| 184 | + }); |
| 185 | +}; |
0 commit comments