Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ Example:
}
```

### internalServer

- Type: `Boolean`
- Default: `false`

If you have defined `serverMiddleware` in your `nuxt.config`, this builder will automatically enable an internal server within the lambda so you can access your own endpoints via `http://localhost:3000`. (This does not affect how you call your endpoints from client-side.)

If you need to enable or disable the internal server manually (for example, if you are adding server middleware via a module), just set `internalServer` within the builder options.

### `generateStaticRoutes`

- Type: `Boolean`
Expand Down
3 changes: 3 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface NuxtBuilderConfig {
generateStaticRoutes?: boolean
includeFiles?: string[] | string
serverFiles?: string[]
internalServer?: boolean
}

export async function build (opts: BuildOptions & { config: NuxtBuilderConfig }): Promise<BuilderOutput> {
Expand Down Expand Up @@ -137,6 +138,7 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
const buildDir = nuxtConfigFile.buildDir ? path.relative(entrypointPath, nuxtConfigFile.buildDir) : '.nuxt'
const srcDir = nuxtConfigFile.srcDir ? path.relative(entrypointPath, nuxtConfigFile.srcDir) : '.'
const lambdaName = nuxtConfigFile.lambdaName ? nuxtConfigFile.lambdaName : 'index'
const usesServerMiddleware = config.internalServer !== undefined ? config.internalServer : !!nuxtConfigFile.serverMiddleware

await exec('nuxt', [
'build',
Expand Down Expand Up @@ -222,6 +224,7 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
const launcherSrc = (await fs.readFile(launcherPath, 'utf8'))
.replace(/__NUXT_SUFFIX__/g, nuxtDep.suffix)
.replace(/__NUXT_CONFIG__/g, './' + nuxtConfigName)
.replace(/\/\* __ENABLE_INTERNAL_SERVER__ \*\/ *true/g, String(usesServerMiddleware))

const launcherFiles = {
'vercel__launcher.js': new FileBlob({ data: launcherSrc }),
Expand Down
27 changes: 17 additions & 10 deletions src/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import http from 'http'
import type { RequestListener } from 'http'
import esmCompiler from 'esm'

const startTime = process.hrtime()
Expand Down Expand Up @@ -33,19 +33,26 @@ const readyPromise = nuxt.ready().then(() => {
})

// Create bridge and start listening
const { Server } = require('http') as typeof http // eslint-disable-line import/order
const { Server } = require('http') as typeof import('http') // eslint-disable-line import/order
const { Bridge } = require('./vercel__bridge.js') as typeof import('@vercel/node-bridge/bridge')

const server = new Server(
async (req, res) => {
if (!isReady) {
await readyPromise
}
nuxt.server.app(req, res)
const requestListener: RequestListener = async (req, res) => {
if (!isReady) {
await readyPromise
}
)
const bridge = new Bridge(server)
nuxt.server.app(req, res)
}

// This is used by Vercel
const server = new Server(requestListener)
const bridge = new Bridge(server)
bridge.listen()

// eslint-disable-next-line
if (/* __ENABLE_INTERNAL_SERVER__ */true) {
// Allow internal calls from Nuxt to endpoints registered as serverMiddleware
const internalServer = new Server(requestListener)
internalServer.listen(3000, '127.0.0.1')
}

export const launcher: typeof bridge.launcher = bridge.launcher