Skip to content

Commit

Permalink
Serverless Next.js (#5927)
Browse files Browse the repository at this point in the history
**This does not change existing behavior.**

building to serverless is completely opt-in.

- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)

This implements the concept of build targets. Currently there will be 2 build targets:

- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)

The serverless target will output a single file per `page` in the `pages` directory:

- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`

So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:

```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```

So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:

```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```

Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.

The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.

One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846

So to summarize:

- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function

TODO:

- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`

Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
  • Loading branch information
timneutkens authored Dec 28, 2018
1 parent 5674425 commit 0f23faf
Show file tree
Hide file tree
Showing 30 changed files with 417 additions and 117 deletions.
25 changes: 17 additions & 8 deletions packages/next-server/server/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import findUp from 'find-up'
import {CONFIG_FILE} from 'next-server/constants'

const targets = ['server', 'serverless']

const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
Expand All @@ -11,13 +13,21 @@ const defaultConfig = {
useFileSystemPublicRoutes: true,
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['jsx', 'js']
pageExtensions: ['jsx', 'js'],
target: 'server'
}

function normalizeConfig (phase, config) {
if (typeof config === 'function') {
return config(phase, {defaultConfig})
}

return config
}

export default function loadConfig (phase, dir, customConfig) {
if (customConfig) {
customConfig.configOrigin = 'server'
return {...defaultConfig, ...customConfig}
return {...defaultConfig, configOrigin: 'server', ...customConfig}
}
const path = findUp.sync(CONFIG_FILE, {
cwd: dir
Expand All @@ -26,12 +36,11 @@ export default function loadConfig (phase, dir, customConfig) {
// If config file was found
if (path && path.length) {
const userConfigModule = require(path)
const userConfigInitial = userConfigModule.default || userConfigModule
if (typeof userConfigInitial === 'function') {
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial(phase, {defaultConfig})}
const userConfig = normalizeConfig(phase, userConfigModule.default || userConfigModule)
if (userConfig.target && !targets.includes(userConfig.target)) {
throw new Error(`Specified target is invalid. Provided: "${userConfig.target}" should be one of ${targets.join(', ')}`)
}

return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial}
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfig}
}

return defaultConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/next-server/server/get-page-files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {normalizePagePath} from './require'
import { normalizePagePath } from './normalize-page-path'

export type BuildManifest = {
devFiles: string[],
Expand Down
4 changes: 1 addition & 3 deletions packages/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {serveStatic} from './serve-static'
import Router, {route, Route} from './router'
import { isInternalUrl, isBlockedPage } from './utils'
import loadConfig from 'next-server/next-config'
import {PHASE_PRODUCTION_SERVER, BUILD_ID_FILE, CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_RUNTIME, BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
import {PHASE_PRODUCTION_SERVER, BUILD_ID_FILE, CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_RUNTIME} from 'next-server/constants'
import * as asset from '../lib/asset'
import * as envConfig from '../lib/runtime-config'
import {loadComponents} from './load-components'
Expand All @@ -32,7 +32,6 @@ export default class Server {
buildId: string
renderOpts: {
staticMarkup: boolean,
distDir: string,
buildId: string,
generateEtags: boolean,
runtimeConfig?: {[key: string]: any},
Expand All @@ -54,7 +53,6 @@ export default class Server {
this.buildId = this.readBuildId()
this.renderOpts = {
staticMarkup,
distDir: this.distDir,
buildId: this.buildId,
generateEtags
}
Expand Down
17 changes: 17 additions & 0 deletions packages/next-server/server/normalize-page-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { posix } from 'path'
export function normalizePagePath (page: string): string {
// If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages
if (page === '/') {
page = '/index'
}
// Resolve on anything that doesn't start with `/`
if (page[0] !== '/') {
page = `/${page}`
}
// Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) {
throw new Error('Requested and resolved page mismatch')
}
return page
}
1 change: 0 additions & 1 deletion packages/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function render(renderElementToString: (element: React.ReactElement<any>) => str

type RenderOpts = {
staticMarkup: boolean,
distDir: string,
buildId: string,
runtimeConfig?: {[key: string]: any},
assetPrefix?: string,
Expand Down
23 changes: 2 additions & 21 deletions packages/next-server/server/require.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
import {join, posix} from 'path'
import {join} from 'path'
import {PAGES_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
import { normalizePagePath } from './normalize-page-path'

export function pageNotFoundError (page: string): Error {
const err: any = new Error(`Cannot find module for page: ${page}`)
err.code = 'ENOENT'
return err
}

export function normalizePagePath (page: string): string {
// If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages
if (page === '/') {
page = '/index'
}

// Resolve on anything that doesn't start with `/`
if (page[0] !== '/') {
page = `/${page}`
}

// Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) {
throw new Error('Requested and resolved page mismatch')
}

return page
}

export function getPagePath (page: string, distDir: string): string {
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST))
Expand Down
14 changes: 6 additions & 8 deletions packages/next/bin/next-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import { printAndExit } from '../server/lib/utils'
const args = arg({
// Types
'--help': Boolean,
'--lambdas': Boolean,

// Aliases
'-h': '--help',
'-l': '--lambdas'
'-h': '--help'
})

if (args['--help']) {
Expand All @@ -30,23 +27,24 @@ if (args['--help']) {
}

const dir = resolve(args._[0] || '.')
const lambdas = args['--lambdas']

// Check if pages dir exists and warn if not
// Check if the provided directory exists
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

// Check if the pages directory exists
if (!existsSync(join(dir, 'pages'))) {
// Check one level down the tree to see if the pages directory might be there
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
}

printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
}

build(dir, null, lambdas)
build(dir)
.catch((err) => {
console.error('> Build error occured')
console.error('> Build error occurred')
printAndExit(err)
})
76 changes: 68 additions & 8 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,84 @@ import {generateBuildId} from './generate-build-id'
import {writeBuildId} from './write-build-id'
import {isWriteable} from './is-writeable'
import {runCompiler, CompilerResult} from './compiler'
import globModule from 'glob'
import {promisify} from 'util'
import {stringify} from 'querystring'
import {ServerlessLoaderQuery} from './webpack/loaders/next-serverless-loader'

export default async function build (dir: string, conf = null, lambdas: boolean = false): Promise<void> {
const glob = promisify(globModule)

function collectPages (directory: string, pageExtensions: string[]): Promise<string[]> {
return glob(`**/*.+(${pageExtensions.join('|')})`, {cwd: directory})
}

export default async function build (dir: string, conf = null): Promise<void> {
if (!await isWriteable(dir)) {
throw new Error('> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable')
}

const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const lambdasOption = config.lambdas ? config.lambdas : lambdas
const distDir = join(dir, config.distDir)
const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = join(dir, config.distDir)
const pagesDir = join(dir, 'pages')

const pagePaths = await collectPages(pagesDir, config.pageExtensions)
type Result = {[page: string]: string}
const pages: Result = pagePaths.reduce((result: Result, pagePath): Result => {
let page = `/${pagePath.replace(new RegExp(`\\.+(${config.pageExtensions.join('|')})$`), '').replace(/\\/g, '/')}`.replace(/\/index$/, '')
page = page === '' ? '/' : page
result[page] = pagePath
return result
}, {})

let entrypoints
if (config.target === 'serverless') {
const serverlessEntrypoints: any = {}
// Because on Windows absolute paths in the generated code can break because of numbers, eg 1 in the path,
// we have to use a private alias
const pagesDirAlias = 'private-next-pages'
const dotNextDirAlias = 'private-dot-next'
const absoluteAppPath = pages['/_app'] ? join(pagesDirAlias, pages['/_app']).replace(/\\/g, '/') : 'next/dist/pages/_app'
const absoluteDocumentPath = pages['/_document'] ? join(pagesDirAlias, pages['/_document']).replace(/\\/g, '/') : 'next/dist/pages/_document'
const absoluteErrorPath = pages['/_error'] ? join(pagesDirAlias, pages['/_error']).replace(/\\/g, '/') : 'next/dist/pages/_error'

const defaultOptions = {
absoluteAppPath,
absoluteDocumentPath,
absoluteErrorPath,
distDir: dotNextDirAlias,
buildId,
assetPrefix: config.assetPrefix,
generateEtags: config.generateEtags
}

Object.keys(pages).forEach(async (page) => {
if (page === '/_app' || page === '/_document') {
return
}

const absolutePagePath = join(pagesDirAlias, pages[page]).replace(/\\/g, '/')
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const serverlessLoaderOptions: ServerlessLoaderQuery = {page, absolutePagePath, ...defaultOptions}
serverlessEntrypoints[join('pages', bundleFile)] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
})

const errorPage = join('pages', '/_error.js')
if (!serverlessEntrypoints[errorPage]) {
const serverlessLoaderOptions: ServerlessLoaderQuery = {page: '/_error', absolutePagePath: 'next/dist/pages/_error', ...defaultOptions}
serverlessEntrypoints[errorPage] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
}

entrypoints = serverlessEntrypoints
}

const configs: any = await Promise.all([
getBaseWebpackConfig(dir, { buildId, isServer: false, config, lambdas: lambdasOption }),
getBaseWebpackConfig(dir, { buildId, isServer: true, config, lambdas: lambdasOption })
getBaseWebpackConfig(dir, { buildId, isServer: false, config, target: config.target }),
getBaseWebpackConfig(dir, { buildId, isServer: true, config, target: config.target, entrypoints })
])

let result: CompilerResult = {warnings: [], errors: []}
if (lambdasOption) {
if (config.target === 'serverless') {
const clientResult = await runCompiler([configs[0]])
const serverResult = await runCompiler([configs[1]])
result = {warnings: [...clientResult.warnings, ...serverResult.warnings], errors: [...clientResult.errors, ...serverResult.errors]}
Expand All @@ -34,11 +94,11 @@ export default async function build (dir: string, conf = null, lambdas: boolean

if (result.warnings.length > 0) {
console.warn('> Emitted warnings from webpack')
console.warn(...result.warnings)
result.warnings.forEach((warning) => console.warn(warning))
}

if (result.errors.length > 0) {
console.error(...result.errors)
result.errors.forEach((error) => console.error(error))
throw new Error('> Build failed because of webpack errors')
}
await writeBuildId(distDir, buildId)
Expand Down
Loading

0 comments on commit 0f23faf

Please sign in to comment.