next-server.ts 29.4 KB
Newer Older
1
import compression from 'compression'
J
Joe Haddad 已提交
2
import fs from 'fs'
J
Joe Haddad 已提交
3
import { IncomingMessage, ServerResponse } from 'http'
J
Joe Haddad 已提交
4
import { join, resolve, sep } from 'path'
5
import pathToRegexp from 'path-to-regexp'
6
import { parse as parseQs, ParsedUrlQuery } from 'querystring'
7
import { format as formatUrl, parse as parseUrl, UrlWithParsedQuery } from 'url'
J
Joe Haddad 已提交
8

J
JJ Kasper 已提交
9
import { withCoalescedInvoke } from '../../lib/coalesced-function'
J
Joe Haddad 已提交
10 11
import {
  BUILD_ID_FILE,
12
  CLIENT_PUBLIC_FILES_PATH,
J
Joe Haddad 已提交
13 14
  CLIENT_STATIC_FILES_PATH,
  CLIENT_STATIC_FILES_RUNTIME,
15
  PAGES_MANIFEST,
J
Joe Haddad 已提交
16
  PHASE_PRODUCTION_SERVER,
17
  ROUTES_MANIFEST,
J
Joe Haddad 已提交
18
  SERVER_DIRECTORY,
19
  SERVERLESS_DIRECTORY,
20
  DEFAULT_REDIRECT_STATUS,
T
Tim Neutkens 已提交
21
} from '../lib/constants'
J
Joe Haddad 已提交
22 23 24 25
import {
  getRouteMatcher,
  getRouteRegex,
  getSortedRoutes,
26
  isDynamicRoute,
J
Joe Haddad 已提交
27
} from '../lib/router/utils'
28
import * as envConfig from '../lib/runtime-config'
J
Joe Haddad 已提交
29
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
30
import { apiResolver } from './api-utils'
31
import loadConfig, { isTargetLikeServerless } from './config'
32
import pathMatch from './lib/path-match'
J
Joe Haddad 已提交
33
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
34
import { loadComponents, LoadComponentsReturnType } from './load-components'
J
Joe Haddad 已提交
35
import { renderToHTML } from './render'
J
Joe Haddad 已提交
36
import { getPagePath } from './require'
37
import Router, { Params, route, Route, RouteMatch } from './router'
J
Joe Haddad 已提交
38 39
import { sendHTML } from './send-html'
import { serveStatic } from './serve-static'
J
Joe Haddad 已提交
40
import { getSprCache, initializeSprCache, setSprCache } from './spr-cache'
41
import { isBlockedPage } from './utils'
J
JJ Kasper 已提交
42 43

const getCustomRouteMatcher = pathMatch(true)
44 45 46

type NextConfig = any

47
export type Rewrite = {
J
JJ Kasper 已提交
48 49 50 51
  source: string
  destination: string
}

52
export type Redirect = Rewrite & {
J
JJ Kasper 已提交
53 54 55
  statusCode?: number
}

56 57 58 59 60 61
type Middleware = (
  req: IncomingMessage,
  res: ServerResponse,
  next: (err?: Error) => void
) => void

T
Tim Neutkens 已提交
62
export type ServerConstructor = {
63 64 65
  /**
   * Where the Next project is located - @default '.'
   */
J
Joe Haddad 已提交
66 67
  dir?: string
  staticMarkup?: boolean
68 69 70
  /**
   * Hide error messages containing server information - @default false
   */
J
Joe Haddad 已提交
71
  quiet?: boolean
72 73 74
  /**
   * Object what you would use in next.config.js - @default {}
   */
75
  conf?: NextConfig
J
JJ Kasper 已提交
76
  dev?: boolean
77
}
78

N
nkzawa 已提交
79
export default class Server {
80 81 82 83
  dir: string
  quiet: boolean
  nextConfig: NextConfig
  distDir: string
84
  pagesDir?: string
85
  publicDir: string
86
  hasStaticDir: boolean
J
JJ Kasper 已提交
87
  pagesManifest: string
88 89
  buildId: string
  renderOpts: {
T
Tim Neutkens 已提交
90
    poweredByHeader: boolean
T
Tim Neutkens 已提交
91
    ampBindInitData: boolean
J
Joe Haddad 已提交
92 93 94 95
    staticMarkup: boolean
    buildId: string
    generateEtags: boolean
    runtimeConfig?: { [key: string]: any }
96 97
    assetPrefix?: string
    canonicalBase: string
98
    documentMiddlewareEnabled: boolean
J
Joe Haddad 已提交
99
    hasCssMode: boolean
100
    dev?: boolean
101
  }
102
  private compression?: Middleware
J
JJ Kasper 已提交
103
  private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
104
  router: Router
105
  protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }>
J
JJ Kasper 已提交
106 107 108 109
  protected customRoutes?: {
    rewrites: Rewrite[]
    redirects: Redirect[]
  }
110

J
Joe Haddad 已提交
111 112 113 114 115
  public constructor({
    dir = '.',
    staticMarkup = false,
    quiet = false,
    conf = null,
J
JJ Kasper 已提交
116
    dev = false,
J
Joe Haddad 已提交
117
  }: ServerConstructor = {}) {
N
nkzawa 已提交
118
    this.dir = resolve(dir)
N
Naoyuki Kanezawa 已提交
119
    this.quiet = quiet
T
Tim Neutkens 已提交
120
    const phase = this.currentPhase()
121
    this.nextConfig = loadConfig(phase, this.dir, conf)
122
    this.distDir = join(this.dir, this.nextConfig.distDir)
123
    this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
124
    this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
J
JJ Kasper 已提交
125 126
    this.pagesManifest = join(
      this.distDir,
127 128 129
      this.nextConfig.target === 'server'
        ? SERVER_DIRECTORY
        : SERVERLESS_DIRECTORY,
J
JJ Kasper 已提交
130 131
      PAGES_MANIFEST
    )
T
Tim Neutkens 已提交
132

133 134
    // Only serverRuntimeConfig needs the default
    // publicRuntimeConfig gets it's default in client/index.js
J
Joe Haddad 已提交
135 136 137 138 139
    const {
      serverRuntimeConfig = {},
      publicRuntimeConfig,
      assetPrefix,
      generateEtags,
140
      compress,
J
Joe Haddad 已提交
141
    } = this.nextConfig
142

T
Tim Neutkens 已提交
143
    this.buildId = this.readBuildId()
144

145
    this.renderOpts = {
T
Tim Neutkens 已提交
146
      ampBindInitData: this.nextConfig.experimental.ampBindInitData,
T
Tim Neutkens 已提交
147
      poweredByHeader: this.nextConfig.poweredByHeader,
148
      canonicalBase: this.nextConfig.amp.canonicalBase,
149 150
      documentMiddlewareEnabled: this.nextConfig.experimental
        .documentMiddleware,
J
Joe Haddad 已提交
151
      hasCssMode: this.nextConfig.experimental.css,
152
      staticMarkup,
153
      buildId: this.buildId,
154
      generateEtags,
155
    }
N
Naoyuki Kanezawa 已提交
156

157 158
    // Only the `publicRuntimeConfig` key is exposed to the client side
    // It'll be rendered as part of __NEXT_DATA__ on the client side
159
    if (Object.keys(publicRuntimeConfig).length > 0) {
160
      this.renderOpts.runtimeConfig = publicRuntimeConfig
161 162
    }

163
    if (compress && this.nextConfig.target === 'server') {
164 165 166
      this.compression = compression() as Middleware
    }

167
    // Initialize next/config with the environment configuration
168 169 170 171 172 173
    if (this.nextConfig.target === 'server') {
      envConfig.setConfig({
        serverRuntimeConfig,
        publicRuntimeConfig,
      })
    }
174

J
JJ Kasper 已提交
175
    this.router = new Router(this.generateRoutes())
176
    this.setAssetPrefix(assetPrefix)
J
JJ Kasper 已提交
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    // call init-server middleware, this is also handled
    // individually in serverless bundles when deployed
    if (!dev && this.nextConfig.experimental.plugins) {
      const serverPath = join(
        this.distDir,
        this._isLikeServerless ? 'serverless' : 'server'
      )
      const initServer = require(join(serverPath, 'init-server.js')).default
      this.onErrorMiddleware = require(join(
        serverPath,
        'on-error-server.js'
      )).default
      initServer()
    }

J
JJ Kasper 已提交
193 194 195 196 197 198 199 200 201 202 203 204
    initializeSprCache({
      dev,
      distDir: this.distDir,
      pagesDir: join(
        this.distDir,
        this._isLikeServerless
          ? SERVERLESS_DIRECTORY
          : `${SERVER_DIRECTORY}/static/${this.buildId}`,
        'pages'
      ),
      flushToDisk: this.nextConfig.experimental.sprFlushToDisk,
    })
N
Naoyuki Kanezawa 已提交
205
  }
N
nkzawa 已提交
206

207
  protected currentPhase(): string {
208
    return PHASE_PRODUCTION_SERVER
209 210
  }

211 212 213 214
  private logError(err: Error): void {
    if (this.onErrorMiddleware) {
      this.onErrorMiddleware({ err })
    }
215 216
    if (this.quiet) return
    // tslint:disable-next-line
217
    console.error(err)
218 219
  }

J
Joe Haddad 已提交
220 221 222
  private handleRequest(
    req: IncomingMessage,
    res: ServerResponse,
223
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
224
  ): Promise<void> {
225
    // Parse url if parsedUrl not provided
226
    if (!parsedUrl || typeof parsedUrl !== 'object') {
227 228
      const url: any = req.url
      parsedUrl = parseUrl(url, true)
229
    }
230

231 232 233
    // Parse the querystring ourselves if the user doesn't handle querystring parsing
    if (typeof parsedUrl.query === 'string') {
      parsedUrl.query = parseQs(parsedUrl.query)
N
Naoyuki Kanezawa 已提交
234
    }
235

236
    res.statusCode = 200
237
    return this.run(req, res, parsedUrl).catch(err => {
J
Joe Haddad 已提交
238 239 240 241
      this.logError(err)
      res.statusCode = 500
      res.end('Internal Server Error')
    })
242 243
  }

244
  public getRequestHandler() {
245
    return this.handleRequest.bind(this)
N
nkzawa 已提交
246 247
  }

248
  public setAssetPrefix(prefix?: string) {
249
    this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
250 251
  }

252
  // Backwards compatibility
253
  public async prepare(): Promise<void> {}
N
nkzawa 已提交
254

T
Tim Neutkens 已提交
255
  // Backwards compatibility
256
  protected async close(): Promise<void> {}
T
Tim Neutkens 已提交
257

258
  protected setImmutableAssetCacheControl(res: ServerResponse) {
T
Tim Neutkens 已提交
259
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
N
nkzawa 已提交
260 261
  }

J
JJ Kasper 已提交
262 263 264 265
  protected getCustomRoutes() {
    return require(join(this.distDir, ROUTES_MANIFEST))
  }

266
  protected generateRoutes(): Route[] {
J
JJ Kasper 已提交
267 268
    this.customRoutes = this.getCustomRoutes()

269 270 271
    const publicRoutes = fs.existsSync(this.publicDir)
      ? this.generatePublicRoutes()
      : []
J
JJ Kasper 已提交
272

273
    const staticFilesRoute = this.hasStaticDir
274 275 276 277 278 279 280
      ? [
          {
            // It's very important to keep this route's param optional.
            // (but it should support as many params as needed, separated by '/')
            // Otherwise this will lead to a pretty simple DOS attack.
            // See more: https://github.com/zeit/next.js/issues/2617
            match: route('/static/:path*'),
281
            name: 'static catchall',
282 283 284
            fn: async (req, res, params, parsedUrl) => {
              const p = join(this.dir, 'static', ...(params.path || []))
              await this.serveStatic(req, res, p, parsedUrl)
285 286 287
              return {
                finished: true,
              }
288 289 290 291
            },
          } as Route,
        ]
      : []
292

293
    const topRoutes: Route[] = [
T
Tim Neutkens 已提交
294
      {
295
        match: route('/_next/static/:path*'),
296 297
        type: 'route',
        name: '_next/static catchall',
298
        fn: async (req, res, params, parsedUrl) => {
T
Tim Neutkens 已提交
299 300 301
          // The commons folder holds commonschunk files
          // The chunks folder holds dynamic entries
          // The buildId folder holds pages and potentially other assets. As buildId changes per build it can be long-term cached.
302 303

          // make sure to 404 for /_next/static itself
304 305 306 307 308 309
          if (!params.path) {
            await this.render404(req, res, parsedUrl)
            return {
              finished: true,
            }
          }
310

J
Joe Haddad 已提交
311 312 313 314 315
          if (
            params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
            params.path[0] === 'chunks' ||
            params.path[0] === this.buildId
          ) {
T
Tim Neutkens 已提交
316
            this.setImmutableAssetCacheControl(res)
317
          }
J
Joe Haddad 已提交
318 319 320
          const p = join(
            this.distDir,
            CLIENT_STATIC_FILES_PATH,
321
            ...(params.path || [])
J
Joe Haddad 已提交
322
          )
323
          await this.serveStatic(req, res, p, parsedUrl)
324 325 326
          return {
            finished: true,
          }
327
        },
328
      },
J
JJ Kasper 已提交
329 330
      {
        match: route('/_next/data/:path*'),
331 332
        type: 'route',
        name: '_next/data catchall',
J
JJ Kasper 已提交
333
        fn: async (req, res, params, _parsedUrl) => {
J
JJ Kasper 已提交
334 335 336
          // Make sure to 404 for /_next/data/ itself and
          // we also want to 404 if the buildId isn't correct
          if (!params.path || params.path[0] !== this.buildId) {
337 338 339 340
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
341 342 343 344 345 346
          }
          // remove buildId from URL
          params.path.shift()

          // show 404 if it doesn't end with .json
          if (!params.path[params.path.length - 1].endsWith('.json')) {
347 348 349 350
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
351 352 353 354 355 356 357
          }

          // re-create page's pathname
          const pathname = `/${params.path.join('/')}`
            .replace(/\.json$/, '')
            .replace(/\/index$/, '/')

J
JJ Kasper 已提交
358 359 360 361 362 363 364 365 366
          req.url = pathname
          const parsedUrl = parseUrl(pathname, true)
          await this.render(
            req,
            res,
            pathname,
            { _nextSprData: '1' },
            parsedUrl
          )
367 368 369
          return {
            finished: true,
          }
J
JJ Kasper 已提交
370 371
        },
      },
T
Tim Neutkens 已提交
372
      {
373
        match: route('/_next/:path*'),
374 375
        type: 'route',
        name: '_next catchall',
T
Tim Neutkens 已提交
376
        // This path is needed because `render()` does a check for `/_next` and the calls the routing again
377
        fn: async (req, res, _params, parsedUrl) => {
T
Tim Neutkens 已提交
378
          await this.render404(req, res, parsedUrl)
379 380 381
          return {
            finished: true,
          }
L
Lukáš Huvar 已提交
382 383
        },
      },
384 385
      ...publicRoutes,
      ...staticFilesRoute,
T
Tim Neutkens 已提交
386
    ]
387
    const routes: Route[] = [...topRoutes]
388

J
JJ Kasper 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    if (this.customRoutes) {
      const { redirects, rewrites } = this.customRoutes

      const getCustomRoute = (
        r: { source: string; destination: string; statusCode?: number },
        type: 'redirect' | 'rewrite'
      ) => ({
        ...r,
        type,
        matcher: getCustomRouteMatcher(r.source),
      })

      const customRoutes = [
        ...redirects.map(r => getCustomRoute(r, 'redirect')),
        ...rewrites.map(r => getCustomRoute(r, 'rewrite')),
      ]

      routes.push(
407
        ...customRoutes.map(route => {
J
JJ Kasper 已提交
408
          return {
409 410 411 412 413 414
            match: route.matcher,
            type: route.type,
            statusCode: route.statusCode,
            name: `${route.type} ${route.source} route`,
            fn: async (_req, res, params, _parsedUrl) => {
              let destinationCompiler = pathToRegexp.compile(route.destination)
415 416 417 418 419 420 421 422 423 424 425 426 427 428
              let newUrl

              try {
                newUrl = destinationCompiler(params)
              } catch (err) {
                if (
                  err.message.match(/Expected .*? to not repeat, but got array/)
                ) {
                  throw new Error(
                    `To use a multi-match in the destination you must add \`*\` at the end of the param name to signify it should repeat. https://err.sh/zeit/next.js/invalid-multi-match`
                  )
                }
                throw err
              }
429 430 431 432 433 434 435

              if (route.type === 'redirect') {
                res.setHeader('Location', newUrl)
                res.statusCode = route.statusCode || DEFAULT_REDIRECT_STATUS
                res.end()
                return {
                  finished: true,
J
JJ Kasper 已提交
436 437 438
                }
              }

439 440 441
              return {
                finished: false,
                pathname: newUrl,
J
JJ Kasper 已提交
442 443 444 445 446
              }
            },
          } as Route
        })
      )
447 448 449
      // make sure previous routes can still be rewritten to by
      // custom routes e.g. /docs/_next/static -> /_next/static
      routes.push(...topRoutes)
J
JJ Kasper 已提交
450 451
    }

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    routes.push({
      match: route('/api/:path*'),
      type: 'route',
      name: 'API Route',
      fn: async (req, res, params, parsedUrl) => {
        const { pathname } = parsedUrl
        await this.handleApiRequest(
          req as NextApiRequest,
          res as NextApiResponse,
          pathname!
        )
        return {
          finished: true,
        }
      },
    })
468

469
    if (this.nextConfig.useFileSystemPublicRoutes) {
J
Joe Haddad 已提交
470
      this.dynamicRoutes = this.getDynamicRoutes()
J
Joe Haddad 已提交
471

472 473 474
      // It's very important to keep this route's param optional.
      // (but it should support as many params as needed, separated by '/')
      // Otherwise this will lead to a pretty simple DOS attack.
475
      // See more: https://github.com/zeit/next.js/issues/2617
T
Tim Neutkens 已提交
476
      routes.push({
477
        match: route('/:path*'),
478 479 480
        type: 'route',
        name: 'Catchall render',
        fn: async (req, res, params, parsedUrl) => {
T
Tim Neutkens 已提交
481
          const { pathname, query } = parsedUrl
482
          if (!pathname) {
483 484
            throw new Error('pathname is undefined')
          }
485

486 487 488 489 490 491 492
          // Used in development to check public directory paths
          if (await this._beforeCatchAllRender(req, res, params, parsedUrl)) {
            return {
              finished: true,
            }
          }

493
          await this.render(req, res, pathname, query, parsedUrl)
494 495 496
          return {
            finished: true,
          }
497
        },
T
Tim Neutkens 已提交
498
      })
499
    }
N
nkzawa 已提交
500

T
Tim Neutkens 已提交
501 502 503
    return routes
  }

504 505 506 507 508 509 510 511 512
  protected async _beforeCatchAllRender(
    _req: IncomingMessage,
    _res: ServerResponse,
    _params: Params,
    _parsedUrl: UrlWithParsedQuery
  ) {
    return false
  }

L
Lukáš Huvar 已提交
513 514 515 516 517 518
  /**
   * Resolves `API` request, in development builds on demand
   * @param req http request
   * @param res http response
   * @param pathname path of request
   */
J
Joe Haddad 已提交
519
  private async handleApiRequest(
L
Lukáš Huvar 已提交
520 521
    req: NextApiRequest,
    res: NextApiResponse,
522
    pathname: string
J
Joe Haddad 已提交
523
  ) {
L
Lukáš Huvar 已提交
524
    let params: Params | boolean = false
J
JJ Kasper 已提交
525 526 527 528 529
    let resolverFunction: any

    try {
      resolverFunction = await this.resolveApiRequest(pathname)
    } catch (err) {}
L
Lukáš Huvar 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

    if (
      this.dynamicRoutes &&
      this.dynamicRoutes.length > 0 &&
      !resolverFunction
    ) {
      for (const dynamicRoute of this.dynamicRoutes) {
        params = dynamicRoute.match(pathname)
        if (params) {
          resolverFunction = await this.resolveApiRequest(dynamicRoute.page)
          break
        }
      }
    }

J
JJ Kasper 已提交
545 546 547 548
    if (!resolverFunction) {
      return this.render404(req, res)
    }

549
    if (!this.renderOpts.dev && this._isLikeServerless) {
J
JJ Kasper 已提交
550 551 552 553 554 555
      const mod = require(resolverFunction)
      if (typeof mod.default === 'function') {
        return mod.default(req, res)
      }
    }

556
    await apiResolver(
557 558 559
      req,
      res,
      params,
J
JJ Kasper 已提交
560 561
      resolverFunction ? require(resolverFunction) : undefined,
      this.onErrorMiddleware
562
    )
L
Lukáš Huvar 已提交
563 564 565 566 567 568
  }

  /**
   * Resolves path to resolver function
   * @param pathname path of request
   */
569
  protected async resolveApiRequest(pathname: string): Promise<string | null> {
J
Joe Haddad 已提交
570 571 572
    return getPagePath(
      pathname,
      this.distDir,
573
      this._isLikeServerless,
574
      this.renderOpts.dev
J
Joe Haddad 已提交
575
    )
L
Lukáš Huvar 已提交
576 577
  }

578
  protected generatePublicRoutes(): Route[] {
579 580
    const routes: Route[] = []
    const publicFiles = recursiveReadDirSync(this.publicDir)
581 582
    const serverBuildPath = join(
      this.distDir,
583
      this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
584
    )
585 586
    const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST))

587
    publicFiles.forEach(path => {
588 589 590 591 592
      const unixPath = path.replace(/\\/g, '/')
      // Only include public files that will not replace a page path
      if (!pagesManifest[unixPath]) {
        routes.push({
          match: route(unixPath),
593 594
          type: 'route',
          name: 'public catchall',
595 596 597
          fn: async (req, res, _params, parsedUrl) => {
            const p = join(this.publicDir, unixPath)
            await this.serveStatic(req, res, p, parsedUrl)
598 599 600
            return {
              finished: true,
            }
601 602 603 604 605 606 607 608
          },
        })
      }
    })

    return routes
  }

609
  protected getDynamicRoutes() {
J
JJ Kasper 已提交
610 611
    const manifest = require(this.pagesManifest)
    const dynamicRoutedPages = Object.keys(manifest).filter(isDynamicRoute)
612 613 614 615
    return getSortedRoutes(dynamicRoutedPages).map(page => ({
      page,
      match: getRouteMatcher(getRouteRegex(page)),
    }))
J
Joe Haddad 已提交
616 617
  }

618 619 620 621 622 623
  private handleCompression(req: IncomingMessage, res: ServerResponse) {
    if (this.compression) {
      this.compression(req, res, () => {})
    }
  }

624
  protected async run(
J
Joe Haddad 已提交
625 626
    req: IncomingMessage,
    res: ServerResponse,
627
    parsedUrl: UrlWithParsedQuery
J
Joe Haddad 已提交
628
  ) {
629 630
    this.handleCompression(req, res)

631
    try {
632 633
      const matched = await this.router.execute(req, res, parsedUrl)
      if (matched) {
634 635 636 637 638 639 640 641
        return
      }
    } catch (err) {
      if (err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return this.renderError(null, req, res, '/_error', {})
      }
      throw err
642 643
    }

644
    await this.render404(req, res, parsedUrl)
N
nkzawa 已提交
645 646
  }

647
  protected async sendHTML(
J
Joe Haddad 已提交
648 649
    req: IncomingMessage,
    res: ServerResponse,
650
    html: string
J
Joe Haddad 已提交
651
  ) {
T
Tim Neutkens 已提交
652 653
    const { generateEtags, poweredByHeader } = this.renderOpts
    return sendHTML(req, res, html, { generateEtags, poweredByHeader })
654 655
  }

J
Joe Haddad 已提交
656 657 658 659 660
  public async render(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
661
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
662
  ): Promise<void> {
663
    const url: any = req.url
664 665 666 667 668

    if (
      url.match(/^\/_next\//) ||
      (this.hasStaticDir && url.match(/^\/static\//))
    ) {
669 670 671
      return this.handleRequest(req, res, parsedUrl)
    }

672
    if (isBlockedPage(pathname)) {
673
      return this.render404(req, res, parsedUrl)
674 675
    }

676
    const html = await this.renderToHTML(req, res, pathname, query, {
J
Joe Haddad 已提交
677 678 679 680 681
      dataOnly:
        (this.renderOpts.ampBindInitData && Boolean(query.dataOnly)) ||
        (req.headers &&
          (req.headers.accept || '').indexOf('application/amp.bind+json') !==
            -1),
682
    })
683 684
    // Request was ended by the user
    if (html === null) {
685 686 687
      return
    }

688
    return this.sendHTML(req, res, html)
N
Naoyuki Kanezawa 已提交
689
  }
N
nkzawa 已提交
690

J
Joe Haddad 已提交
691
  private async findPageComponents(
J
Joe Haddad 已提交
692
    pathname: string,
693
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
694
  ) {
695
    const serverless = !this.renderOpts.dev && this._isLikeServerless
J
JJ Kasper 已提交
696 697 698
    // try serving a static AMP version first
    if (query.amp) {
      try {
J
Joe Haddad 已提交
699 700 701 702
        return await loadComponents(
          this.distDir,
          this.buildId,
          (pathname === '/' ? '/index' : pathname) + '.amp',
703
          serverless
J
Joe Haddad 已提交
704
        )
J
JJ Kasper 已提交
705 706 707 708
      } catch (err) {
        if (err.code !== 'ENOENT') throw err
      }
    }
J
Joe Haddad 已提交
709 710 711 712
    return await loadComponents(
      this.distDir,
      this.buildId,
      pathname,
713
      serverless
J
Joe Haddad 已提交
714 715 716
    )
  }

J
JJ Kasper 已提交
717 718 719 720 721 722
  private __sendPayload(
    res: ServerResponse,
    payload: any,
    type: string,
    revalidate?: number | false
  ) {
J
JJ Kasper 已提交
723
    // TODO: ETag? Cache-Control headers? Next-specific headers?
J
JJ Kasper 已提交
724
    res.setHeader('Content-Type', type)
J
JJ Kasper 已提交
725
    res.setHeader('Content-Length', Buffer.byteLength(payload))
J
Joe Haddad 已提交
726 727 728 729 730 731 732 733 734 735 736 737
    if (!this.renderOpts.dev) {
      if (revalidate) {
        res.setHeader(
          'Cache-Control',
          `s-maxage=${revalidate}, stale-while-revalidate`
        )
      } else if (revalidate === false) {
        res.setHeader(
          'Cache-Control',
          `s-maxage=31536000, stale-while-revalidate`
        )
      }
J
JJ Kasper 已提交
738
    }
J
JJ Kasper 已提交
739 740 741
    res.end(payload)
  }

J
Joe Haddad 已提交
742 743 744 745 746 747
  private async renderToHTMLWithComponents(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
    result: LoadComponentsReturnType,
748
    opts: any
J
JJ Kasper 已提交
749
  ): Promise<string | null> {
J
JJ Kasper 已提交
750
    // handle static page
J
Joe Haddad 已提交
751 752 753 754
    if (typeof result.Component === 'string') {
      return result.Component
    }

J
JJ Kasper 已提交
755 756
    // check request state
    const isLikeServerless =
J
Joe Haddad 已提交
757
      typeof result.Component === 'object' &&
J
JJ Kasper 已提交
758
      typeof result.Component.renderReqToHTML === 'function'
J
JJ Kasper 已提交
759 760 761 762 763 764
    const isSpr = !!result.unstable_getStaticProps

    // non-spr requests should render like normal
    if (!isSpr) {
      // handle serverless
      if (isLikeServerless) {
765 766 767 768 769 770 771 772
        const curUrl = parseUrl(req.url!, true)
        req.url = formatUrl({
          ...curUrl,
          query: {
            ...curUrl.query,
            ...query,
          },
        })
J
JJ Kasper 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        return result.Component.renderReqToHTML(req, res)
      }

      return renderToHTML(req, res, pathname, query, {
        ...result,
        ...opts,
      })
    }

    // Toggle whether or not this is an SPR Data request
    const isSprData = isSpr && query._nextSprData
    if (isSprData) {
      delete query._nextSprData
    }
    // Compute the SPR cache key
    const sprCacheKey = parseUrl(req.url || '').pathname!

    // Complete the response with cached data if its present
    const cachedData = await getSprCache(sprCacheKey)
    if (cachedData) {
      const data = isSprData
        ? JSON.stringify(cachedData.pageData)
        : cachedData.html

      this.__sendPayload(
        res,
        data,
J
JJ Kasper 已提交
800 801
        isSprData ? 'application/json' : 'text/html; charset=utf-8',
        cachedData.curRevalidate
J
JJ Kasper 已提交
802 803 804 805 806 807
      )

      // Stop the request chain here if the data we sent was up-to-date
      if (!cachedData.isStale) {
        return null
      }
J
JJ Kasper 已提交
808
    }
J
Joe Haddad 已提交
809

J
JJ Kasper 已提交
810 811 812 813 814
    // If we're here, that means data is missing or it's stale.

    // Serverless requests need its URL transformed back into the original
    // request path (to emulate lambda behavior in production)
    if (isLikeServerless && isSprData) {
J
JJ Kasper 已提交
815 816 817
      let { pathname } = parseUrl(req.url || '', true)
      pathname = !pathname || pathname === '/' ? '/index' : pathname
      req.url = `/_next/data/${this.buildId}${pathname}.json`
J
JJ Kasper 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    }

    const doRender = withCoalescedInvoke(async function(): Promise<{
      html: string | null
      sprData: any
      sprRevalidate: number | false
    }> {
      let sprData: any
      let html: string | null
      let sprRevalidate: number | false

      let renderResult
      // handle serverless
      if (isLikeServerless) {
        renderResult = await result.Component.renderReqToHTML(req, res, true)

        html = renderResult.html
        sprData = renderResult.renderOpts.sprData
        sprRevalidate = renderResult.renderOpts.revalidate
      } else {
        const renderOpts = {
          ...result,
          ...opts,
        }
        renderResult = await renderToHTML(req, res, pathname, query, renderOpts)

        html = renderResult
        sprData = renderOpts.sprData
        sprRevalidate = renderOpts.revalidate
      }

      return { html, sprData, sprRevalidate }
850
    })
J
JJ Kasper 已提交
851 852 853 854 855 856 857 858

    return doRender(sprCacheKey, []).then(
      async ({ isOrigin, value: { html, sprData, sprRevalidate } }) => {
        // Respond to the request if a payload wasn't sent above (from cache)
        if (!isResSent(res)) {
          this.__sendPayload(
            res,
            isSprData ? JSON.stringify(sprData) : html,
J
JJ Kasper 已提交
859 860
            isSprData ? 'application/json' : 'text/html; charset=utf-8',
            sprRevalidate
J
JJ Kasper 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
          )
        }

        // Update the SPR cache if the head request
        if (isOrigin) {
          await setSprCache(
            sprCacheKey,
            { html: html!, pageData: sprData },
            sprRevalidate
          )
        }

        return null
      }
    )
876 877
  }

J
Joe Haddad 已提交
878
  public renderToHTML(
J
Joe Haddad 已提交
879 880 881 882
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
J
Joe Haddad 已提交
883 884 885 886 887 888 889
    {
      amphtml,
      dataOnly,
      hasAmp,
    }: {
      amphtml?: boolean
      hasAmp?: boolean
890 891
      dataOnly?: boolean
    } = {}
J
Joe Haddad 已提交
892
  ): Promise<string | null> {
J
Joe Haddad 已提交
893 894
    return this.findPageComponents(pathname, query)
      .then(
895
        result => {
J
Joe Haddad 已提交
896 897 898 899 900 901
          return this.renderToHTMLWithComponents(
            req,
            res,
            pathname,
            query,
            result,
902
            { ...this.renderOpts, amphtml, hasAmp, dataOnly }
J
Joe Haddad 已提交
903 904
          )
        },
905
        err => {
J
Joe Haddad 已提交
906 907 908 909 910 911 912 913 914 915 916
          if (err.code !== 'ENOENT' || !this.dynamicRoutes) {
            return Promise.reject(err)
          }

          for (const dynamicRoute of this.dynamicRoutes) {
            const params = dynamicRoute.match(pathname)
            if (!params) {
              continue
            }

            return this.findPageComponents(dynamicRoute.page, query).then(
917 918
              result => {
                return this.renderToHTMLWithComponents(
J
Joe Haddad 已提交
919 920 921
                  req,
                  res,
                  dynamicRoute.page,
J
JJ Kasper 已提交
922 923 924 925 926 927 928
                  // only add params for SPR enabled pages
                  {
                    ...(result.unstable_getStaticProps
                      ? { _nextSprData: query._nextSprData }
                      : query),
                    ...params,
                  },
J
Joe Haddad 已提交
929
                  result,
J
JJ Kasper 已提交
930 931 932 933 934 935
                  {
                    ...this.renderOpts,
                    amphtml,
                    hasAmp,
                    dataOnly,
                  }
936
                )
937
              }
J
Joe Haddad 已提交
938 939 940 941
            )
          }

          return Promise.reject(err)
942
        }
J
Joe Haddad 已提交
943
      )
944
      .catch(err => {
J
Joe Haddad 已提交
945 946 947 948 949 950 951 952 953
        if (err && err.code === 'ENOENT') {
          res.statusCode = 404
          return this.renderErrorToHTML(null, req, res, pathname, query)
        } else {
          this.logError(err)
          res.statusCode = 500
          return this.renderErrorToHTML(err, req, res, pathname, query)
        }
      })
N
Naoyuki Kanezawa 已提交
954 955
  }

J
Joe Haddad 已提交
956 957 958 959 960
  public async renderError(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
961
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
962 963 964
  ): Promise<void> {
    res.setHeader(
      'Cache-Control',
965
      'no-cache, no-store, max-age=0, must-revalidate'
J
Joe Haddad 已提交
966
    )
N
Naoyuki Kanezawa 已提交
967
    const html = await this.renderErrorToHTML(err, req, res, pathname, query)
968
    if (html === null) {
969 970
      return
    }
971
    return this.sendHTML(req, res, html)
N
nkzawa 已提交
972 973
  }

J
Joe Haddad 已提交
974 975 976 977 978
  public async renderErrorToHTML(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    _pathname: string,
979
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
980
  ) {
J
Joe Haddad 已提交
981
    const result = await this.findPageComponents('/_error', query)
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    let html
    try {
      html = await this.renderToHTMLWithComponents(
        req,
        res,
        '/_error',
        query,
        result,
        {
          ...this.renderOpts,
          err,
        }
      )
    } catch (err) {
      console.error(err)
      res.statusCode = 500
      html = 'Internal Server Error'
    }
    return html
N
Naoyuki Kanezawa 已提交
1001 1002
  }

J
Joe Haddad 已提交
1003 1004 1005
  public async render404(
    req: IncomingMessage,
    res: ServerResponse,
1006
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1007
  ): Promise<void> {
1008 1009
    const url: any = req.url
    const { pathname, query } = parsedUrl ? parsedUrl : parseUrl(url, true)
1010
    if (!pathname) {
1011 1012
      throw new Error('pathname is undefined')
    }
N
Naoyuki Kanezawa 已提交
1013
    res.statusCode = 404
1014
    return this.renderError(null, req, res, pathname, query)
N
Naoyuki Kanezawa 已提交
1015
  }
N
Naoyuki Kanezawa 已提交
1016

J
Joe Haddad 已提交
1017 1018 1019 1020
  public async serveStatic(
    req: IncomingMessage,
    res: ServerResponse,
    path: string,
1021
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1022
  ): Promise<void> {
A
Arunoda Susiripala 已提交
1023
    if (!this.isServeableUrl(path)) {
1024
      return this.render404(req, res, parsedUrl)
A
Arunoda Susiripala 已提交
1025 1026
    }

1027 1028 1029 1030 1031 1032
    if (!(req.method === 'GET' || req.method === 'HEAD')) {
      res.statusCode = 405
      res.setHeader('Allow', ['GET', 'HEAD'])
      return this.renderError(null, req, res, path)
    }

N
Naoyuki Kanezawa 已提交
1033
    try {
1034
      await serveStatic(req, res, path)
N
Naoyuki Kanezawa 已提交
1035
    } catch (err) {
T
Tim Neutkens 已提交
1036
      if (err.code === 'ENOENT' || err.statusCode === 404) {
1037
        this.render404(req, res, parsedUrl)
1038 1039 1040
      } else if (err.statusCode === 412) {
        res.statusCode = 412
        return this.renderError(err, req, res, path)
N
Naoyuki Kanezawa 已提交
1041 1042 1043 1044 1045 1046
      } else {
        throw err
      }
    }
  }

1047
  private isServeableUrl(path: string): boolean {
A
Arunoda Susiripala 已提交
1048 1049
    const resolved = resolve(path)
    if (
1050
      resolved.indexOf(join(this.distDir) + sep) !== 0 &&
1051 1052
      resolved.indexOf(join(this.dir, 'static') + sep) !== 0 &&
      resolved.indexOf(join(this.dir, 'public') + sep) !== 0
A
Arunoda Susiripala 已提交
1053 1054 1055 1056 1057 1058 1059 1060
    ) {
      // Seems like the user is trying to traverse the filesystem.
      return false
    }

    return true
  }

1061
  protected readBuildId(): string {
1062 1063 1064 1065 1066
    const buildIdFile = join(this.distDir, BUILD_ID_FILE)
    try {
      return fs.readFileSync(buildIdFile, 'utf8').trim()
    } catch (err) {
      if (!fs.existsSync(buildIdFile)) {
J
Joe Haddad 已提交
1067
        throw new Error(
1068
          `Could not find a valid build in the '${this.distDir}' directory! Try building your app with 'next build' before starting the server.`
J
Joe Haddad 已提交
1069
        )
1070 1071 1072
      }

      throw err
1073
    }
1074
  }
1075 1076 1077 1078

  private get _isLikeServerless(): boolean {
    return isTargetLikeServerless(this.nextConfig.target)
  }
1079
}