Created
December 13, 2021 05:52
-
-
Save hieu-e/8c7fc720fa41750652b750c76e823ec6 to your computer and use it in GitHub Desktop.
CloudFront Functions redirect for React/SPA app under subdirectory.
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 uri = request.uri; | |
// Check if SPA App 1 Direcotry | |
if (uri.startsWith('/my-app-1')) { | |
request.uri = '/my-app-1/index.html'; | |
} | |
// Check whether the URI is missing a file name. | |
else if(uri.endsWith('/')) { | |
request.uri += 'index.html'; | |
} | |
// Check whether the URI is missing a file extension. | |
else if (!uri.includes('.')) { | |
request.uri += '/index.html'; | |
} | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment