Skip to content

Instantly share code, notes, and snippets.

@Gomah
Created July 25, 2017 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gomah/6ec3e70d0d529af1bd3950ddebe8b6cb to your computer and use it in GitHub Desktop.
Save Gomah/6ec3e70d0d529af1bd3950ddebe8b6cb to your computer and use it in GitHub Desktop.
Return pre-rendered html files for non-supported browsers
const whitelist = ['chrome', 'crios', 'firefox', 'fxios', 'googlebot'];
const isSupportedBrowser = uas => {
if (uas && Array.isArray(uas) && uas.length > 0) {
return uas.some(ua =>
whitelist.some(w => ua.value.toLowerCase().includes(w))
);
}
return false;
};
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const { headers } = request;
if (!isSupportedBrowser(headers['user-agent'])) {
if (
!request.uri ||
request.uri === '/' ||
request.uri.includes('index.html')
) {
request.uri = '/index.static.html';
} else {
request.uri += '.static.html';
}
}
callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment