Created
July 16, 2020 08:48
-
-
Save renoinn/353e9f357fd398383aefc16da4a12a75 to your computer and use it in GitHub Desktop.
nuxt generateしてS3+CloudFrontでhostする際に、サブディレクトリのindex.htmlを補完するLambda@Edge
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 config = { | |
| suffix: '/index.html', | |
| appendToDirs: 'index.html', | |
| } | |
| const regexSuffixless = /\/[^/.]+$/ // e.g. "/some/page" but not "/", "/some/" or "/some.jpg" | |
| const regexTrailingSlash = /.+\/$/ // e.g. "/some/" or "/some/page/" but not root "/" | |
| exports.handler = function handler (event, context, callback) { | |
| const {request} = event.Records[0].cf | |
| const {uri} = request | |
| const {suffix, appendToDirs} = config | |
| if (suffix && uri.match(regexSuffixless)) { | |
| request.uri = uri + suffix | |
| callback(null, request) | |
| return | |
| } | |
| if (appendToDirs && uri.match(regexTrailingSlash)) { | |
| request.uri = uri + appendToDirs | |
| callback(null, request) | |
| return | |
| } | |
| callback(null, request) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment