Last active
January 15, 2024 09:50
-
-
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
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
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