Created
July 25, 2017 23:46
-
-
Save Gomah/6ec3e70d0d529af1bd3950ddebe8b6cb to your computer and use it in GitHub Desktop.
Return pre-rendered html files for non-supported browsers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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