Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
if(request.uri == '/') {
if (typeof headers['accept-language'] !== 'undefined') {
const supportedLanguages = headers['accept-language'][0].value;
console.log('Supported languages:', supportedLanguages);
if(supportedLanguages.startsWith('en')){
callback(null, redirect('/en/index.html'));
} else if(supportedLanguages.startsWith('ja')){
callback(null, redirect('/ja/index.html'));
} else if(supportedLanguages.startsWith('fr')){
callback(null, redirect('/fr/'));
} else if(supportedLanguages.startsWith('it')){
callback(null, redirect('/it/'));
} else if(supportedLanguages.startsWith('pl')){
callback(null, redirect('/pl-PL/'));
} else {
callback(null, redirect('/ja/index.html'));
}
} else {
callback(null, redirect('/ja/index.html'));
}
} else {
callback(null, request);
}
};
function redirect (to) {
return {
status: '301',
statusDescription: 'redirect to browser language',
headers: {
location: [{ key: 'Location', value: to }]
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.