-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.js
More file actions
81 lines (75 loc) · 2.11 KB
/
Copy pathnext.config.js
File metadata and controls
81 lines (75 loc) · 2.11 KB
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
const fetch = require('node-fetch');
const btoa = require('btoa');
module.exports = {
basePath: '/help',
assetPrefix: '/help',
images: {
domains: [
'dev-global-forest-watch-blog.pantheonsite.io',
'test-global-forest-watch-blog.pantheonsite.io',
'content.globalforestwatch.org',
],
},
webpack: (config) => {
config.module.rules.push({
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
options: { svgoConfig: { plugins: { removeViewBox: false } } },
},
],
});
return config;
},
trailingSlash: true,
async redirects() {
const userPassword = btoa(
`${process.env.NEXT_PUBLIC_AUTH_USER}:${process.env.NEXT_PUBLIC_AUTH_TOKEN}`
);
const fetchConfig = {
method: 'get',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${userPassword}`,
},
};
let allRedirects = [];
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/redirection/v1/redirect?per_page=5&filterBy[status]=enabled&filterBy[url]=/help/`,
fetchConfig
);
const json = await response.json();
const totalPages = Math.ceil(parseInt(json.total, 10) / 50);
const redirectPages = await Promise.all(
Array.from(Array(totalPages).keys()).map((page) =>
fetch(
`${
process.env.NEXT_PUBLIC_WORDPRESS_URL
}/redirection/v1/redirect?per_page=50&page=${
page + 1
}&filterBy[status]=enabled&filterBy[url]=/help/`,
fetchConfig
)
)
);
const allResponses = await Promise.all(
redirectPages.map((res) => res.json())
);
allRedirects = allResponses.reduce(
(arr, res) => [...arr, ...res.items],
[]
);
} catch (e) {
throw new Error(
`Wordpress initial request failed, check your credentials \n${e.name}: ${e.message}`
);
}
return allRedirects.map((r) => ({
source: `${r.url}/`,
destination: `${r.action_data.url}/`,
permanent: true,
}));
},
};