-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathnext.config.js
More file actions
97 lines (86 loc) · 2.17 KB
/
next.config.js
File metadata and controls
97 lines (86 loc) · 2.17 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { request, gql } = require('graphql-request');
const ANALYTICS_BASE_URL = 'https://hn-ping2.hashnode.com';
const HASHNODE_ADVANCED_ANALYTICS_URL = 'https://user-analytics.hashnode.com';
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL;
const GQL_ENDPOINT = process.env.NEXT_PUBLIC_HASHNODE_GQL_ENDPOINT;
const host = process.env.NEXT_PUBLIC_HASHNODE_PUBLICATION_HOST;
const getBasePath = () => {
if (BASE_URL && BASE_URL.indexOf('/') !== -1) {
return BASE_URL.substring(BASE_URL.indexOf('/'));
}
return undefined;
};
const getRedirectionRules = async () => {
const query = gql`
query GetRedirectionRules {
publication(host: "${host}") {
id
redirectionRules {
source
destination
type
}
}
}
`;
const data = await request(GQL_ENDPOINT, query);
if (!data.publication) {
throw 'Please ensure you have set the env var NEXT_PUBLIC_HASHNODE_PUBLICATION_HOST correctly.';
}
const redirectionRules = data.publication.redirectionRules;
// convert to next.js redirects format
const redirects = redirectionRules
.filter((rule) => {
// Hashnode gives an option to set a wildcard redirect,
// but it doesn't work properly with Next.js
// the solution is to filter out all the rules with wildcard and use static redirects for now
return rule.source.indexOf('*') === -1;
})
.map((rule) => {
return {
source: rule.source,
destination: rule.destination,
permanent: rule.type === 'PERMANENT',
};
});
return redirects;
};
/**
* @type {import('next').NextConfig}
*/
const config = {
transpilePackages: ['@starter-kit/utils'],
basePath: getBasePath(),
experimental: {
scrollRestoration: true,
},
images: {
unoptimized: true,
remotePatterns: [
{
protocol: 'https',
hostname: 'cdn.hashnode.com',
},
],
},
async rewrites() {
return [
{
source: '/ping/data-event',
destination: `${ANALYTICS_BASE_URL}/api/data-event`,
},
{
source: '/api/analytics',
destination: `${HASHNODE_ADVANCED_ANALYTICS_URL}/api/analytics`,
},
{
source: '/feed.xml',
destination: '/rss.xml',
},
];
},
async redirects() {
return await getRedirectionRules();
},
};
module.exports = config;