-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (35 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// index.js
const startTime = process.hrtime()
const { Nuxt } = require('nuxt-start')
const sls = require('serverless-http')
const binaryMimeTypes = require('./binaryMimeTypes')
const config = require('./nuxt.config.js')
const nuxt = new Nuxt(config)
let isReady = false
const readyPromise = nuxt.ready().then(() => {
isReady = true
const hrTime = process.hrtime(startTime)
const hrTimeMs = ((hrTime[0] * 1e9) + hrTime[1]) / 1e6
// eslint-disable-next-line no-console
console.log(`λ Cold start took: ${hrTimeMs}ms`)
}).catch((error) => {
// eslint-disable-next-line no-console
console.error('λ Error while initializing nuxt:', error)
process.exit(1)
})
const handler = sls(nuxt.render, {
binary: binaryMimeTypes
})
module.exports.nuxt = async function (event, context) {
/** Immediate response for WarmUp plugin */
if (event.source === 'serverless-plugin-warmup') {
console.log('WarmUp - Lambda is warm!')
return 'Lambda is warm!'
}
const hostHeader = event.headers.Host || event.headers.host
console.log('======== hostHeader', hostHeader)
if (!isReady) {
await readyPromise
}
return handler(event, context)
}