This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathdefault-handler.ts
107 lines (85 loc) · 2.92 KB
/
default-handler.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
// @ts-ignore
import PrerenderManifest from "./prerender-manifest.json";
// @ts-ignore
import Manifest from "./manifest.json";
import { PrerenderManifest as PrerenderManifestType } from "next/dist/build/index";
import lambdaAtEdgeCompat from "next-aws-cloudfront";
import {
CloudFrontRequest,
CloudFrontS3Origin,
CloudFrontOrigin,
CloudFrontResultResponse
} from "aws-lambda";
import {
OriginRequestEvent,
OriginRequestDefaultHandlerManifest
} from "./types";
const addS3HostHeader = (
req: CloudFrontRequest,
s3DomainName: string
): void => {
req.headers["host"] = [{ key: "host", value: s3DomainName }];
};
const router = (
manifest: OriginRequestDefaultHandlerManifest
): ((path: string) => string) => {
const {
pages: { ssr, html }
} = manifest;
const allDynamicRoutes = { ...ssr.dynamic, ...html.dynamic };
return (path: string): string => {
if (ssr.nonDynamic[path]) {
return ssr.nonDynamic[path];
}
for (const route in allDynamicRoutes) {
const { file, regex } = allDynamicRoutes[route];
const re = new RegExp(regex, "i");
const pathMatchesRoute = re.test(path);
if (pathMatchesRoute) {
return file;
}
}
// path didn't match any route, return error page
return "pages/_error.js";
};
};
const normaliseUri = (uri: string): string => (uri === "/" ? "/index" : uri);
export const handler = async (
event: OriginRequestEvent
): Promise<CloudFrontResultResponse | CloudFrontRequest> => {
const request = event.Records[0].cf.request;
const uri = normaliseUri(request.uri);
const manifest: OriginRequestDefaultHandlerManifest = Manifest;
const prerenderManifest: PrerenderManifestType = PrerenderManifest;
const { pages, publicFiles } = manifest;
const isStaticPage = pages.html.nonDynamic[uri];
const isPublicFile = publicFiles[uri];
const isPrerenderedPage = prerenderManifest.routes[request.uri]; // prerendered pages are also static pages like "pages.html" above, but are defined in the prerender-manifest
const origin = request.origin as CloudFrontOrigin;
const s3Origin = origin.s3 as CloudFrontS3Origin;
const isHTMLPage = isStaticPage || isPrerenderedPage;
if (uri === "/service-worker.js") {
s3Origin.path = "/_next/static";
return request;
}
if (isHTMLPage || isPublicFile) {
s3Origin.path = isHTMLPage ? "/static-pages" : "/public";
if (isHTMLPage) {
addS3HostHeader(request, s3Origin.domainName);
request.uri = uri + ".html";
}
return request;
}
const pagePath = router(manifest)(uri);
if (pagePath.endsWith(".html")) {
s3Origin.path = "/static-pages";
request.uri = pagePath.replace("pages", "");
addS3HostHeader(request, s3Origin.domainName);
return request;
}
const { req, res, responsePromise } = lambdaAtEdgeCompat(event.Records[0].cf);
// eslint-disable-next-line
const page = require(`./${pagePath}`);
page.render(req, res);
return responsePromise;
};