Created
July 29, 2019 13:20
-
-
Save jesusgollonet/873ef8f6cb83eaae1a10ce2d3fd4d5be to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
exports.handler = (event, context, callback) => { | |
const request = event.Records[0].cf.request; | |
const headers = request.headers; | |
if (request.uri === '/' || request.uri.indexOf('.') === -1){ | |
console.log(JSON.stringify(request)); | |
if (isDesktopOrTablet(headers)){ | |
console.log('desktop or tablet') | |
request.uri = '/index.html'; | |
} else { | |
console.log('mobile') | |
request.uri = '/index-mobile.html'; | |
} | |
} | |
callback(null, request); | |
}; | |
function isDesktopOrTablet(headers){ | |
return (headers['cloudfront-is-desktop-viewer'] | |
&& headers['cloudfront-is-desktop-viewer'][0].value === 'true' ) || | |
(headers['cloudfront-is-tablet-viewer'] | |
&& headers['cloudfront-is-tablet-viewer'][0].value === 'true' ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment