-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
63 lines (53 loc) · 2.2 KB
/
handler.js
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
const path = require('path');
const { ignorePaths } = require('./ignore-paths')
const { changeLanguageRegion } = require('./helpers/change-language-region');
const { parseCookie } = require('./helpers/parse-cookie');
exports.handler = async (event) => {
const request = event.Records[0].cf.request;
try {
const headers = request.headers;
const uri = request.uri;
const parsedPath = path.parse(request.uri);
// Paths to ignore such as data and images
if (
!uri ||
!parsedPath ||
parsedPath.ext !== '' && uri !== '/index.html' ||
ignorePaths.some(path => uri.includes(path))
) {
return request;
}
let cookie;
if (headers.cookie) {
const parsedCookies = parseCookie(headers.cookie);
cookie = parsedCookies ? parsedCookies['language-region-override'] : undefined;
}
if (
cookie
) {
const cookieCountryCode = cookie.substring(3, 5).toLowerCase();
const cookieLanguageCode = cookie.substring(0, 2).toLowerCase();
return changeLanguageRegion(request, uri, cookieLanguageCode, cookieCountryCode)
} else if (
headers &&
headers['cloudfront-viewer-country'] &&
headers['cloudfront-viewer-country'][0] &&
headers['cloudfront-viewer-country'][0].value &&
headers['accept-language'] &&
headers['accept-language'][0] &&
headers['accept-language'][0].value
) {
const headerCountryCode = headers['cloudfront-viewer-country'][0].value.toLowerCase();
const acceptLanguage = headers['accept-language'][0].value.toLowerCase();
const headerLanguageCode = acceptLanguage.length > 2 ? acceptLanguage.substring(0, 2) : acceptLanguage;
const languageRegion = changeLanguageRegion(request, uri, headerLanguageCode, headerCountryCode)
return languageRegion;
} else {
const languageRegion = changeLanguageRegion(request, uri);
return languageRegion;
}
} catch (err) {
console.error(err);
return request;
}
};