Skip to content

Instantly share code, notes, and snippets.

@renoinn
Created July 16, 2020 08:48
Show Gist options
  • Select an option

  • Save renoinn/353e9f357fd398383aefc16da4a12a75 to your computer and use it in GitHub Desktop.

Select an option

Save renoinn/353e9f357fd398383aefc16da4a12a75 to your computer and use it in GitHub Desktop.
nuxt generateしてS3+CloudFrontでhostする際に、サブディレクトリのindex.htmlを補完するLambda@Edge
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