-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmiddleware.ts
205 lines (166 loc) · 8.14 KB
/
middleware.ts
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
import { STATIC_REWRITE_PATHS } from 'config/staticRewritePaths';
import { NextMiddleware, NextRequest, NextResponse } from 'next/server';
import {
FriendlyPageTypesValue,
FriendlyPagesDestinations,
FriendlyPagesTypes,
FriendlyPagesTypesKey,
} from 'types/friendlyUrl';
import { getDomainIdFromHostname } from 'utils/domain/getDomainIdFromHostname';
const ERROR_PAGE_ROUTE = '/404';
const MIDDLEWARE_STATUS_CODE_KEY = 'middleware-status-code';
const MIDDLEWARE_STATUS_MESSAGE_KEY = 'middleware-status-message';
export const middleware: NextMiddleware = async (request) => {
try {
if (request.url.includes('_next/data')) {
return new NextResponse(null, { status: 404 });
}
const host = getHostFromRequest(request);
const domainUrlFromStaticUrls = getDomainUrlFromStaticUrls(host);
const staticUrlsAvailableForDomain = getStaticUrlsAvailableForDomain(domainUrlFromStaticUrls);
const rewriteTargetUrl = getRewriteTargetPathname(request, staticUrlsAvailableForDomain);
if (rewriteTargetUrl) {
const rewriteUrlObject = new URL(rewriteTargetUrl, request.url);
addQueryParametersToRewriteUrlObject(rewriteUrlObject, request.nextUrl.search);
return NextResponse.rewrite(rewriteUrlObject);
}
const { search } = new URL(request.url);
const queryParams = new URLSearchParams(search);
const slugTypeQueryParam = queryParams.get('slugType');
if (slugTypeQueryParam) {
return rewriteDynamicPages(slugTypeQueryParam as FriendlyPageTypesValue, request.url, search);
}
const pageTypeResponse = await fetch(`${process.env.INTERNAL_ENDPOINT}resolve-friendly-url`, {
method: 'POST',
body: JSON.stringify({
slug: request.nextUrl.pathname,
domainId: getDomainIdFromHostname(request.headers.get('Host') as string),
}),
});
if (!pageTypeResponse.ok) {
const is400Error = isInRange(pageTypeResponse.status, 400, 499);
const is500Error = isInRange(pageTypeResponse.status, 500, 599);
let statusMessage = 'Unknown middleware error for ' + request.url;
if (is400Error) {
statusMessage = 'Friendly URL page not found for ' + request.url;
} else if (is500Error) {
statusMessage = 'Middleware runtime error for ' + request.url;
}
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), {
headers: [
[MIDDLEWARE_STATUS_CODE_KEY, pageTypeResponse.status.toString()],
[MIDDLEWARE_STATUS_MESSAGE_KEY, statusMessage],
],
});
}
const pageTypeParsedResponse: { route: FriendlyPageTypesValue; redirectTo: string; redirectCode: number } =
await pageTypeResponse.json();
if (pageTypeParsedResponse.redirectTo && pageTypeParsedResponse.redirectTo !== request.url) {
return NextResponse.redirect(
new URL(
`${pageTypeParsedResponse.redirectTo}${queryParams.toString() !== '' ? `?${queryParams}` : ''}`,
request.url,
).href,
pageTypeParsedResponse.redirectCode,
);
}
return rewriteDynamicPages(pageTypeParsedResponse.route, request.url, search);
} catch (e) {
if (
(process.env.ERROR_DEBUGGING_LEVEL === 'console' ||
process.env.ERROR_DEBUGGING_LEVEL === 'toast-and-console') &&
e instanceof Error
) {
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), {
headers: [
[MIDDLEWARE_STATUS_CODE_KEY, '500'],
[MIDDLEWARE_STATUS_MESSAGE_KEY, e.message],
],
});
}
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), {
headers: [
[MIDDLEWARE_STATUS_CODE_KEY, '500'],
[MIDDLEWARE_STATUS_MESSAGE_KEY, 'Middleware runtime error for ' + request.url],
],
});
}
};
export const config = {
matcher: [
'/((?!api|_next|favicon.ico|fonts|svg|images|locales|icons|grapesjs-template|grapesjs-homepage-article-template|grapesjs-article-template|robots).*)',
],
};
const isInRange = (number: number, start: number, end: number) => number >= start && start <= end;
const rewriteDynamicPages = (pageType: FriendlyPageTypesValue, rewriteUrl: string, queryParams: string) => {
const pageTypeKey = (Object.keys(FriendlyPagesTypes) as FriendlyPagesTypesKey[]).find(
(key) => FriendlyPagesTypes[key] === pageType,
);
const host = new URL(rewriteUrl).origin;
if (pageTypeKey) {
return NextResponse.rewrite(new URL(`${FriendlyPagesDestinations[pageTypeKey]}${queryParams}`, host));
}
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, host), {
headers: [
[MIDDLEWARE_STATUS_CODE_KEY, '404'],
[MIDDLEWARE_STATUS_MESSAGE_KEY, 'Friendly URL page not found for ' + rewriteUrl],
],
});
};
const getHostFromRequest = (request: NextRequest): string => {
const requestHeaders = new Headers(request.headers);
const host = requestHeaders.get('host');
if (host === null) {
throw new Error(`Host was not found in the request header.`);
}
return host;
};
const getDomainUrlFromStaticUrls = (host: string): string => {
const domainUrlFromStaticUrls = Object.keys(STATIC_REWRITE_PATHS).find((domainUrl) => domainUrl.match(host));
if (domainUrlFromStaticUrls === undefined) {
throw new Error(`Host ${host} does not have a corresponding URL in the available static URLS.`);
}
return domainUrlFromStaticUrls;
};
const getStaticUrlsAvailableForDomain = (domainUrlFromStaticUrls: string): Record<string, string> => {
const staticUrlsAvailableForDomain = STATIC_REWRITE_PATHS[domainUrlFromStaticUrls];
return staticUrlsAvailableForDomain;
};
const getRewriteTargetPathname = (
request: NextRequest,
staticUrlsAvailableForDomain: Record<string, string>,
): string => {
let rewriteTargetPathnameArray: string[] = [];
for (const [staticRewritePathname, staticLocalizedPathname] of Object.entries(staticUrlsAvailableForDomain)) {
const requestedPathnameSegments = request.nextUrl.pathname.split('/');
const staticRewritePathnameSegments = staticRewritePathname.split('/');
const staticLocalizedPathnameSegments = staticLocalizedPathname.split('/');
let areAllSegmentsIdenticalOrDynamic = true;
const rewriteTargetPathnameArrayBuffer = [];
const hasDynamicSegment = staticRewritePathnameSegments.some((segment) => isPathnameSegmentDynamic(segment));
for (let index = 0; index < requestedPathnameSegments.length; index++) {
const isCurrentPathnameSegmentDynamic = isPathnameSegmentDynamic(staticRewritePathnameSegments[index]);
areAllSegmentsIdenticalOrDynamic =
areAllSegmentsIdenticalOrDynamic &&
(staticLocalizedPathnameSegments[index] === requestedPathnameSegments[index] ||
isCurrentPathnameSegmentDynamic);
if (isCurrentPathnameSegmentDynamic) {
rewriteTargetPathnameArrayBuffer.push(requestedPathnameSegments[index]);
} else {
rewriteTargetPathnameArrayBuffer.push(staticRewritePathnameSegments[index]);
}
}
if (hasDynamicSegment && requestedPathnameSegments.length !== staticRewritePathnameSegments.length) {
areAllSegmentsIdenticalOrDynamic = false;
}
if (areAllSegmentsIdenticalOrDynamic) {
rewriteTargetPathnameArray = [...rewriteTargetPathnameArrayBuffer];
}
}
const rewriteTargetPathname = rewriteTargetPathnameArray.join('/');
return rewriteTargetPathname;
};
const addQueryParametersToRewriteUrlObject = (rewriteUrlObject: URL, originalUrlQueryParams: string) => {
rewriteUrlObject.search = originalUrlQueryParams;
};
const isPathnameSegmentDynamic = (segment?: string) => segment?.charAt(0) === ':';