Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Success-Guy/6dfd153a89e34a8fc6facbb0b32c80f3 to your computer and use it in GitHub Desktop.
Save Success-Guy/6dfd153a89e34a8fc6facbb0b32c80f3 to your computer and use it in GitHub Desktop.
Cloudfront function to handle NextJS requests of multi-level with get param to the right files
function handler(event) {
var request = event.request;
var rawurl = request.uri;
var url = rawurl.split('?')[0].split('.txt')[0];
var param = request.querystring
console.log('original ==> ' + rawurl)
// console.log('parameter ==> ' + param)
var uri ='';
var location = url.split('/').slice(1);
if (request.uri.endsWith('/')) location.pop();
console.log("location ==> "+location);
console.log("location length==> "+location.length);
if(location.length === 1){
uri = '/'+ location[0]
console.log("uri ==>"+uri);
}else if(location.length === 2){
uri = '/'+ location[0] +'/'+ location[1]
console.log("uri ==>"+uri);
}
(request.uri.endsWith('/'))? uri += '/index.html' : uri += '.html';
request.uri = uri;
if(rawurl.includes('_next') || rawurl.includes('svg/') || rawurl.includes('images/') || rawurl.includes('.svg')) {
request.uri = rawurl;
}
// console.log(request)
return request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment