-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
286 lines (270 loc) · 7.26 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const localeConfig = require("./src/util/locale-config.json");
/**
* Our Contentful space uses the full "en-US" locale name for English, so this
* helper function let's us easily grab the long name when we need it.
*/
const getFullLocaleName = (locale) => (locale === "en" ? "en-US" : locale);
/**
* For urls with no locale specified, this helper generates params for the `createPage`
* method to generate a page that programmatically adds on the browser's default locale.
* See `locale-redirect.tsx` for details on how the redirect works.
*/
const createLocaleRedirectOptions = (path) => {
let options = {
path: path,
component: require.resolve(`./src/components/locale-redirect.tsx`),
context: {
slug: path,
},
};
return options;
};
/* Generate Learning Center pages */
const generateLearningPages = async function ({ actions, graphql }, locale) {
const query =
`query {
contentfulLearningCenterSearchPage(node_locale: { eq:"` +
getFullLocaleName(locale) +
`" } ){
title
categoryButtons {
title
description
slug
}
articles {
metadata {
title
description
keywords {
keywords
}
shareImage {
file {
url
}
}
}
slug
englishOnly
title
coverPhoto {
fluid {
base64
aspectRatio
src
srcSet
sizes
}
}
summary
whatCanIDo {
json
}
subtitle {
json
}
author
dateUpdated
categories {
title
description
slug
}
articleSections {
__typename
... on ContentfulLearningArticleCtaBlock {
title
ctaText
ctaLink
}
... on ContentfulLearningArticleSection {
title
content {
json
}
}
}
productBlocks {
productName
title
descriptionText {
json
}
button {
title
link
}
location
language
}
}
}
}
`;
const { data } = await graphql(query);
const articlePreviews = data.contentfulLearningCenterSearchPage.articles.map(
(article) => {
const {
title,
slug,
englishOnly,
metadata,
categories,
dateUpdated,
...rest
} = article;
const subset = {
title,
slug,
englishOnly,
metadata,
categories,
dateUpdated,
};
return subset;
}
);
/* Create each Learning Center category page with appropriate data */
data.contentfulLearningCenterSearchPage.categoryButtons.forEach(
(category) => {
actions.createPage({
path: locale + "/learn/category/" + category.slug,
component: require.resolve(
`./src/components/learning-center/category-page-template.tsx`
),
context: {
content: category,
articlePreviews: articlePreviews.filter((article) =>
article.categories.some(
(articleCategory) => articleCategory.title === category.title
)
),
},
});
/* Create a redirect for urls with no locale specified */
actions.createPage(
createLocaleRedirectOptions("/learn/category/" + category.slug)
);
}
);
/* Create each Learning Center article page with appropriate data */
const learningCenterTitle = data.contentfulLearningCenterSearchPage.title;
data.contentfulLearningCenterSearchPage.articles.forEach((article) => {
actions.createPage({
path: locale + "/learn/" + article.slug,
component: require.resolve(
`./src/components/learning-center/article-template.tsx`
),
context: {
learningCenterTitle: learningCenterTitle,
content: article,
},
});
/* Create a redirect for urls with no locale specified */
actions.createPage(createLocaleRedirectOptions("/learn/" + article.slug));
});
};
const deprecatedLearningCenterArticles = [
{
slug: "how-to-break-a-lease",
redirectCategory: "laws",
},
{
slug: "public-eviction-records",
redirectCategory: "eviction",
},
{
slug: "housing-discrimination-examples",
redirectCategory: "discrimination",
},
{
slug: "fair-housing-act",
redirectCategory: "laws",
},
{
slug: "eviction-notice-what-to-do",
redirectCategory: "eviction",
},
{
slug: "ny-eviction-moratorium-faq",
redirectCategory: "eviction",
},
{
slug: "nyc-housing-during-coronavirus",
redirectCategory: "laws",
},
{
slug: "homeless-during-coronavirus-nyc",
redirectCategory: "discrimination",
},
{
slug: "rent-freeze-faq",
redirectCategory: "eviction",
},
];
exports.createPages = async function ({ actions, graphql }) {
generateLearningPages({ actions, graphql }, "en"); // English pages
generateLearningPages({ actions, graphql }, "es"); // Spanish pages
/* Redirects for old site pages */
const { createRedirect } = actions; //actions is collection of many actions - https://www.gatsbyjs.org/docs/actions
createRedirect({
fromPath: "/donate",
toPath: "https://donorbox.org/donate-to-justfix-nyc",
isPermanent: true,
});
createRedirect({
fromPath: "/ehp",
toPath: "https://app.justfix.org/ehp",
isPermanent: true,
});
createRedirect({ fromPath: "/get-repairs", toPath: "/", isPermanent: true });
createRedirect({
fromPath: "/about/products-and-services",
toPath: "/#products",
isPermanent: true,
});
// Create redirects for all pages that used to be in the old `/about` directory:
["partners", "press", "team"].map((path) =>
localeConfig.ACCEPTED_LOCALES.map((locale) =>
createRedirect({
fromPath: `/${locale}/about/${path}`,
toPath: `/${locale}/${path}`,
isPermanent: true,
})
)
);
// Create redirects for old Learning Center articles that have been removed:
deprecatedLearningCenterArticles.map(({ slug, redirectCategory }) =>
localeConfig.ACCEPTED_LOCALES.map((locale) =>
createRedirect({
fromPath: `/${locale}/learn/${slug}`,
toPath: `/${locale}/learn/category/${redirectCategory}`,
isPermanent: true,
})
)
);
};
/* Add a redirect page for any remaining route that doesn't specify the locale in the url */
exports.onCreatePage = async ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators;
if (
!(page.context.slug && page.context.langKey === localeConfig.DEFAULT_LOCALE)
)
return;
/* Do we need to return a Promise here? All of the examples I saw of returns from `onCreatePage`
returned a promise, but not entirely sure why... */
return new Promise((resolve, reject) => {
const rootSlug = page.context.slug
.replace(page.context.langKey, "")
.replace("//", "/");
const newPage = createLocaleRedirectOptions(rootSlug);
createPage(newPage);
resolve();
});
};