-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.tsx
57 lines (49 loc) · 1.86 KB
/
utils.tsx
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
import { getCurrentDateTime } from "./helpers";
import { IncomingMessage, ServerResponse } from "http";
import { NextApiRequestCookies } from "next/dist/server/api-utils";
export const parseCookie = (str) =>
str
.split(";")
.map((v) => v.split("="))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {}) ?? {};
export const extractFromCookie = (
res: ServerResponse,
req: IncomingMessage & {
cookies: NextApiRequestCookies;
}
) => {
let cookies;
// Akamai
if (Object.keys(req.cookies).length) {
cookies = req.cookies;
// Vercel
} else {
cookies = res.getHeader("set-cookie") ?? req.headers["set-cookie"] ?? [];
cookies = cookies.map(parseCookie);
cookies = cookies.reduce((acc, obj) => ({ ...acc, ...obj }), {});
}
const datetime = getCurrentDateTime();
return { optimizely: cookies, datetime };
};
// !no-store - Akamai by default has a rule to no store everything with no extensions like pages paths. So to enable the caching behaviour we have to negate it and then pass the proper cache headers.
export const cacheResponse = (res) => {
// res.setHeader("Cache-Control", "max-age=60");
res.setHeader(
"Edge-Control",
`!no-store, maxage=${process.env.NEXT_PUBLIC_CACHE_MAX_AGE}`
); // Remember about time period suffixes
res.setHeader(
"Pragma",
"akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-check-cacheable, akamai-x-get-cache-key, akamai-x-get-extracted-values"
);
// res.setHeader("Vercel-CDN-Cache-Control", "no-store");
// res.setHeader("CDN-Cache-Control", "no-store");
// res.setHeader("Cache-Control", "no-store");
};
// Vercel
// Set-Cookie: optimizely_visitor_id=bb439045-f848-4bed-be3d-ee3afe016885;new_page_layout=true
// Akamai
// Cookie: optimizely_visitor_id=2bc2-befe-319f-466d; new_page_layout=false