next-server.ts 30.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'
J
Joe Haddad 已提交
5
import { compile as compilePathToRegex } 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 38 39 40 41 42 43
import Router, {
  Params,
  route,
  Route,
  DynamicRoutes,
  PageChecker,
} from './router'
J
Joe Haddad 已提交
44 45
import { sendHTML } from './send-html'
import { serveStatic } from './serve-static'
J
Joe Haddad 已提交
46
import { getSprCache, initializeSprCache, setSprCache } from './spr-cache'
47
import { isBlockedPage } from './utils'
48
import { Redirect, Rewrite } from '../../lib/check-custom-routes'
J
JJ Kasper 已提交
49 50

const getCustomRouteMatcher = pathMatch(true)
51 52 53

type NextConfig = any

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

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

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

J
Joe Haddad 已提交
110 111 112 113 114
  public constructor({
    dir = '.',
    staticMarkup = false,
    quiet = false,
    conf = null,
J
JJ Kasper 已提交
115
    dev = false,
J
Joe Haddad 已提交
116
  }: ServerConstructor = {}) {
N
nkzawa 已提交
117
    this.dir = resolve(dir)
N
Naoyuki Kanezawa 已提交
118
    this.quiet = quiet
T
Tim Neutkens 已提交
119
    const phase = this.currentPhase()
120
    this.nextConfig = loadConfig(phase, this.dir, conf)
121
    this.distDir = join(this.dir, this.nextConfig.distDir)
122
    this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
123
    this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
T
Tim Neutkens 已提交
124

125 126
    // Only serverRuntimeConfig needs the default
    // publicRuntimeConfig gets it's default in client/index.js
J
Joe Haddad 已提交
127 128 129 130 131
    const {
      serverRuntimeConfig = {},
      publicRuntimeConfig,
      assetPrefix,
      generateEtags,
132
      compress,
J
Joe Haddad 已提交
133
    } = this.nextConfig
134

T
Tim Neutkens 已提交
135
    this.buildId = this.readBuildId()
136

137
    this.renderOpts = {
T
Tim Neutkens 已提交
138
      ampBindInitData: this.nextConfig.experimental.ampBindInitData,
T
Tim Neutkens 已提交
139
      poweredByHeader: this.nextConfig.poweredByHeader,
140
      canonicalBase: this.nextConfig.amp.canonicalBase,
141 142
      documentMiddlewareEnabled: this.nextConfig.experimental
        .documentMiddleware,
J
Joe Haddad 已提交
143
      hasCssMode: this.nextConfig.experimental.css,
144
      staticMarkup,
145
      buildId: this.buildId,
146
      generateEtags,
147
    }
N
Naoyuki Kanezawa 已提交
148

149 150
    // Only the `publicRuntimeConfig` key is exposed to the client side
    // It'll be rendered as part of __NEXT_DATA__ on the client side
151
    if (Object.keys(publicRuntimeConfig).length > 0) {
152
      this.renderOpts.runtimeConfig = publicRuntimeConfig
153 154
    }

155
    if (compress && this.nextConfig.target === 'server') {
156 157 158
      this.compression = compression() as Middleware
    }

159
    // Initialize next/config with the environment configuration
160 161 162 163 164 165
    if (this.nextConfig.target === 'server') {
      envConfig.setConfig({
        serverRuntimeConfig,
        publicRuntimeConfig,
      })
    }
166

167 168 169 170 171 172 173 174 175 176
    this.serverBuildDir = join(
      this.distDir,
      this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
    )
    const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)

    if (!dev) {
      this.pagesManifest = require(pagesManifestPath)
    }

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

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

J
JJ Kasper 已提交
192 193 194 195 196 197 198 199 200 201 202 203
    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 已提交
204
  }
N
nkzawa 已提交
205

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

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

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

230 231 232
    // 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 已提交
233
    }
234

T
Tim Neutkens 已提交
235 236 237 238 239 240 241 242 243 244
    if (parsedUrl.pathname!.startsWith(this.nextConfig.experimental.basePath)) {
      // If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
      parsedUrl.pathname =
        parsedUrl.pathname!.replace(
          this.nextConfig.experimental.basePath,
          ''
        ) || '/'
      req.url = req.url!.replace(this.nextConfig.experimental.basePath, '')
    }

245
    res.statusCode = 200
246
    return this.run(req, res, parsedUrl).catch(err => {
J
Joe Haddad 已提交
247 248 249 250
      this.logError(err)
      res.statusCode = 500
      res.end('Internal Server Error')
    })
251 252
  }

253
  public getRequestHandler() {
254
    return this.handleRequest.bind(this)
N
nkzawa 已提交
255 256
  }

257
  public setAssetPrefix(prefix?: string) {
258
    this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
259 260
  }

261
  // Backwards compatibility
262
  public async prepare(): Promise<void> {}
N
nkzawa 已提交
263

T
Tim Neutkens 已提交
264
  // Backwards compatibility
265
  protected async close(): Promise<void> {}
T
Tim Neutkens 已提交
266

267
  protected setImmutableAssetCacheControl(res: ServerResponse) {
T
Tim Neutkens 已提交
268
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
N
nkzawa 已提交
269 270
  }

J
JJ Kasper 已提交
271 272 273 274
  protected getCustomRoutes() {
    return require(join(this.distDir, ROUTES_MANIFEST))
  }

275 276 277 278 279 280 281
  protected generateRoutes(): {
    routes: Route[]
    fsRoutes: Route[]
    catchAllRoute: Route
    pageChecker: PageChecker
    dynamicRoutes: DynamicRoutes | undefined
  } {
J
JJ Kasper 已提交
282 283
    this.customRoutes = this.getCustomRoutes()

284 285 286
    const publicRoutes = fs.existsSync(this.publicDir)
      ? this.generatePublicRoutes()
      : []
J
JJ Kasper 已提交
287

288
    const staticFilesRoute = this.hasStaticDir
289 290 291 292 293 294 295
      ? [
          {
            // 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*'),
296
            name: 'static catchall',
297 298 299
            fn: async (req, res, params, parsedUrl) => {
              const p = join(this.dir, 'static', ...(params.path || []))
              await this.serveStatic(req, res, p, parsedUrl)
300 301 302
              return {
                finished: true,
              }
303 304 305 306
            },
          } as Route,
        ]
      : []
307

308
    const fsRoutes: Route[] = [
T
Tim Neutkens 已提交
309
      {
310
        match: route('/_next/static/:path*'),
311 312
        type: 'route',
        name: '_next/static catchall',
313
        fn: async (req, res, params, parsedUrl) => {
T
Tim Neutkens 已提交
314 315 316
          // 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.
317 318

          // make sure to 404 for /_next/static itself
319 320 321 322 323 324
          if (!params.path) {
            await this.render404(req, res, parsedUrl)
            return {
              finished: true,
            }
          }
325

J
Joe Haddad 已提交
326 327 328 329 330
          if (
            params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
            params.path[0] === 'chunks' ||
            params.path[0] === this.buildId
          ) {
T
Tim Neutkens 已提交
331
            this.setImmutableAssetCacheControl(res)
332
          }
J
Joe Haddad 已提交
333 334 335
          const p = join(
            this.distDir,
            CLIENT_STATIC_FILES_PATH,
336
            ...(params.path || [])
J
Joe Haddad 已提交
337
          )
338
          await this.serveStatic(req, res, p, parsedUrl)
339 340 341
          return {
            finished: true,
          }
342
        },
343
      },
J
JJ Kasper 已提交
344 345
      {
        match: route('/_next/data/:path*'),
346 347
        type: 'route',
        name: '_next/data catchall',
J
JJ Kasper 已提交
348
        fn: async (req, res, params, _parsedUrl) => {
J
JJ Kasper 已提交
349 350 351
          // 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) {
352 353 354 355
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
356 357 358 359 360 361
          }
          // 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')) {
362 363 364 365
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
366 367 368 369 370 371 372
          }

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

J
JJ Kasper 已提交
373 374 375 376 377 378 379 380 381
          req.url = pathname
          const parsedUrl = parseUrl(pathname, true)
          await this.render(
            req,
            res,
            pathname,
            { _nextSprData: '1' },
            parsedUrl
          )
382 383 384
          return {
            finished: true,
          }
J
JJ Kasper 已提交
385 386
        },
      },
T
Tim Neutkens 已提交
387
      {
388
        match: route('/_next/:path*'),
389 390
        type: 'route',
        name: '_next catchall',
T
Tim Neutkens 已提交
391
        // This path is needed because `render()` does a check for `/_next` and the calls the routing again
392
        fn: async (req, res, _params, parsedUrl) => {
T
Tim Neutkens 已提交
393
          await this.render404(req, res, parsedUrl)
394 395 396
          return {
            finished: true,
          }
L
Lukáš Huvar 已提交
397 398
        },
      },
399 400
      ...publicRoutes,
      ...staticFilesRoute,
T
Tim Neutkens 已提交
401
    ]
402
    const routes: Route[] = []
403

J
JJ Kasper 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    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(
422
        ...customRoutes.map(route => {
J
JJ Kasper 已提交
423
          return {
424
            check: true,
425 426 427 428 429
            match: route.matcher,
            type: route.type,
            statusCode: route.statusCode,
            name: `${route.type} ${route.source} route`,
            fn: async (_req, res, params, _parsedUrl) => {
430 431 432 433
              const parsedDestination = parseUrl(route.destination, true)
              let destinationCompiler = compilePathToRegex(
                `${parsedDestination.pathname!}${parsedDestination.hash || ''}`
              )
434 435 436 437 438 439
              let newUrl

              try {
                newUrl = destinationCompiler(params)
              } catch (err) {
                if (
J
Joe Haddad 已提交
440 441 442
                  err.message.match(
                    /Expected .*? to not repeat, but got an array/
                  )
443 444 445 446 447 448 449
                ) {
                  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
              }
450 451

              if (route.type === 'redirect') {
452 453 454 455 456 457 458 459 460
                const parsedNewUrl = parseUrl(newUrl)
                res.setHeader(
                  'Location',
                  formatUrl({
                    ...parsedDestination,
                    pathname: parsedNewUrl.pathname,
                    hash: parsedNewUrl.hash,
                  })
                )
461 462 463 464
                res.statusCode = route.statusCode || DEFAULT_REDIRECT_STATUS
                res.end()
                return {
                  finished: true,
J
JJ Kasper 已提交
465 466 467
                }
              }

468 469 470
              return {
                finished: false,
                pathname: newUrl,
J
JJ Kasper 已提交
471 472 473 474 475 476 477
              }
            },
          } as Route
        })
      )
    }

478 479
    const catchAllRoute: Route = {
      match: route('/:path*'),
480
      type: 'route',
481
      name: 'Catchall render',
482
      fn: async (req, res, params, parsedUrl) => {
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        const { pathname, query } = parsedUrl
        if (!pathname) {
          throw new Error('pathname is undefined')
        }

        // Used in development to check public directory paths
        if (await this._beforeCatchAllRender(req, res, params, parsedUrl)) {
          return {
            finished: true,
          }
        }

        if (params && params.path && params.path[0] === 'api') {
          const handled = await this.handleApiRequest(
            req as NextApiRequest,
            res as NextApiResponse,
            pathname!
          )
          if (handled) {
            return { finished: true }
          }
        }

        await this.render(req, res, pathname, query, parsedUrl)
507 508 509 510
        return {
          finished: true,
        }
      },
511
    }
512

513
    if (this.nextConfig.useFileSystemPublicRoutes) {
J
Joe Haddad 已提交
514
      this.dynamicRoutes = this.getDynamicRoutes()
J
Joe Haddad 已提交
515

516 517 518
      // 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.
519
      // See more: https://github.com/zeit/next.js/issues/2617
520
      routes.push(catchAllRoute)
521
    }
N
nkzawa 已提交
522

523 524 525 526 527 528 529
    return {
      routes,
      fsRoutes,
      catchAllRoute,
      dynamicRoutes: this.dynamicRoutes,
      pageChecker: this.hasPage.bind(this),
    }
T
Tim Neutkens 已提交
530 531
  }

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  private async getPagePath(pathname: string) {
    return getPagePath(
      pathname,
      this.distDir,
      this._isLikeServerless,
      this.renderOpts.dev
    )
  }

  protected async hasPage(pathname: string): Promise<boolean> {
    let found = false
    try {
      found = !!(await this.getPagePath(pathname))
    } catch (_) {}

    return found
  }

550 551 552 553 554 555 556 557 558
  protected async _beforeCatchAllRender(
    _req: IncomingMessage,
    _res: ServerResponse,
    _params: Params,
    _parsedUrl: UrlWithParsedQuery
  ) {
    return false
  }

559 560 561
  // Used to build API page in development
  protected async ensureApiPage(pathname: string) {}

L
Lukáš Huvar 已提交
562 563 564 565 566 567
  /**
   * Resolves `API` request, in development builds on demand
   * @param req http request
   * @param res http response
   * @param pathname path of request
   */
J
Joe Haddad 已提交
568
  private async handleApiRequest(
569 570
    req: IncomingMessage,
    res: ServerResponse,
571
    pathname: string
J
Joe Haddad 已提交
572
  ) {
573
    let page = pathname
L
Lukáš Huvar 已提交
574
    let params: Params | boolean = false
575
    let pageFound = await this.hasPage(page)
J
JJ Kasper 已提交
576

577
    if (!pageFound && this.dynamicRoutes) {
L
Lukáš Huvar 已提交
578 579 580
      for (const dynamicRoute of this.dynamicRoutes) {
        params = dynamicRoute.match(pathname)
        if (params) {
581 582
          page = dynamicRoute.page
          pageFound = true
L
Lukáš Huvar 已提交
583 584 585 586 587
          break
        }
      }
    }

588
    if (!pageFound) {
589
      return false
J
JJ Kasper 已提交
590
    }
591 592 593 594 595 596
    // Make sure the page is built before getting the path
    // or else it won't be in the manifest yet
    await this.ensureApiPage(page)

    const builtPagePath = await this.getPagePath(page)
    const pageModule = require(builtPagePath)
J
JJ Kasper 已提交
597

598
    if (!this.renderOpts.dev && this._isLikeServerless) {
599
      if (typeof pageModule.default === 'function') {
600 601
        await pageModule.default(req, res)
        return true
J
JJ Kasper 已提交
602 603 604
      }
    }

605
    await apiResolver(req, res, params, pageModule, this.onErrorMiddleware)
606
    return true
L
Lukáš Huvar 已提交
607 608
  }

609
  protected generatePublicRoutes(): Route[] {
610 611 612
    const routes: Route[] = []
    const publicFiles = recursiveReadDirSync(this.publicDir)

613
    publicFiles.forEach(path => {
614 615
      const unixPath = path.replace(/\\/g, '/')
      // Only include public files that will not replace a page path
616 617
      // this should not occur now that we check this during build
      if (!this.pagesManifest![unixPath]) {
618 619
        routes.push({
          match: route(unixPath),
620 621
          type: 'route',
          name: 'public catchall',
622 623 624
          fn: async (req, res, _params, parsedUrl) => {
            const p = join(this.publicDir, unixPath)
            await this.serveStatic(req, res, p, parsedUrl)
625 626 627
            return {
              finished: true,
            }
628 629 630 631 632 633 634 635
          },
        })
      }
    })

    return routes
  }

636
  protected getDynamicRoutes() {
637 638 639
    const dynamicRoutedPages = Object.keys(this.pagesManifest!).filter(
      isDynamicRoute
    )
640 641 642 643
    return getSortedRoutes(dynamicRoutedPages).map(page => ({
      page,
      match: getRouteMatcher(getRouteRegex(page)),
    }))
J
Joe Haddad 已提交
644 645
  }

646 647 648 649 650 651
  private handleCompression(req: IncomingMessage, res: ServerResponse) {
    if (this.compression) {
      this.compression(req, res, () => {})
    }
  }

652
  protected async run(
J
Joe Haddad 已提交
653 654
    req: IncomingMessage,
    res: ServerResponse,
655
    parsedUrl: UrlWithParsedQuery
J
Joe Haddad 已提交
656
  ) {
657 658
    this.handleCompression(req, res)

659
    try {
660 661
      const matched = await this.router.execute(req, res, parsedUrl)
      if (matched) {
662 663 664 665 666 667 668 669
        return
      }
    } catch (err) {
      if (err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return this.renderError(null, req, res, '/_error', {})
      }
      throw err
670 671
    }

672
    await this.render404(req, res, parsedUrl)
N
nkzawa 已提交
673 674
  }

675
  protected async sendHTML(
J
Joe Haddad 已提交
676 677
    req: IncomingMessage,
    res: ServerResponse,
678
    html: string
J
Joe Haddad 已提交
679
  ) {
T
Tim Neutkens 已提交
680 681
    const { generateEtags, poweredByHeader } = this.renderOpts
    return sendHTML(req, res, html, { generateEtags, poweredByHeader })
682 683
  }

J
Joe Haddad 已提交
684 685 686 687 688
  public async render(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
689
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
690
  ): Promise<void> {
691
    const url: any = req.url
692 693 694 695 696

    if (
      url.match(/^\/_next\//) ||
      (this.hasStaticDir && url.match(/^\/static\//))
    ) {
697 698 699
      return this.handleRequest(req, res, parsedUrl)
    }

700
    if (isBlockedPage(pathname)) {
701
      return this.render404(req, res, parsedUrl)
702 703
    }

704
    const html = await this.renderToHTML(req, res, pathname, query, {
J
Joe Haddad 已提交
705 706 707 708 709
      dataOnly:
        (this.renderOpts.ampBindInitData && Boolean(query.dataOnly)) ||
        (req.headers &&
          (req.headers.accept || '').indexOf('application/amp.bind+json') !==
            -1),
710
    })
711 712
    // Request was ended by the user
    if (html === null) {
713 714 715
      return
    }

716
    return this.sendHTML(req, res, html)
N
Naoyuki Kanezawa 已提交
717
  }
N
nkzawa 已提交
718

J
Joe Haddad 已提交
719
  private async findPageComponents(
J
Joe Haddad 已提交
720
    pathname: string,
721
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
722
  ) {
723
    const serverless = !this.renderOpts.dev && this._isLikeServerless
J
JJ Kasper 已提交
724 725 726
    // try serving a static AMP version first
    if (query.amp) {
      try {
J
Joe Haddad 已提交
727 728 729 730
        return await loadComponents(
          this.distDir,
          this.buildId,
          (pathname === '/' ? '/index' : pathname) + '.amp',
731
          serverless
J
Joe Haddad 已提交
732
        )
J
JJ Kasper 已提交
733 734 735 736
      } catch (err) {
        if (err.code !== 'ENOENT') throw err
      }
    }
J
Joe Haddad 已提交
737 738 739 740
    return await loadComponents(
      this.distDir,
      this.buildId,
      pathname,
741
      serverless
J
Joe Haddad 已提交
742 743 744
    )
  }

J
JJ Kasper 已提交
745 746 747 748 749 750
  private __sendPayload(
    res: ServerResponse,
    payload: any,
    type: string,
    revalidate?: number | false
  ) {
J
JJ Kasper 已提交
751
    // TODO: ETag? Cache-Control headers? Next-specific headers?
J
JJ Kasper 已提交
752
    res.setHeader('Content-Type', type)
J
JJ Kasper 已提交
753
    res.setHeader('Content-Length', Buffer.byteLength(payload))
J
Joe Haddad 已提交
754 755 756 757 758 759 760 761 762 763 764 765
    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 已提交
766
    }
J
JJ Kasper 已提交
767 768 769
    res.end(payload)
  }

J
Joe Haddad 已提交
770 771 772 773 774 775
  private async renderToHTMLWithComponents(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
    result: LoadComponentsReturnType,
776
    opts: any
J
JJ Kasper 已提交
777
  ): Promise<string | null> {
J
JJ Kasper 已提交
778
    // handle static page
J
Joe Haddad 已提交
779 780 781 782
    if (typeof result.Component === 'string') {
      return result.Component
    }

J
JJ Kasper 已提交
783 784
    // check request state
    const isLikeServerless =
J
Joe Haddad 已提交
785
      typeof result.Component === 'object' &&
J
JJ Kasper 已提交
786
      typeof result.Component.renderReqToHTML === 'function'
J
JJ Kasper 已提交
787 788 789 790 791 792
    const isSpr = !!result.unstable_getStaticProps

    // non-spr requests should render like normal
    if (!isSpr) {
      // handle serverless
      if (isLikeServerless) {
793 794 795 796 797 798 799 800
        const curUrl = parseUrl(req.url!, true)
        req.url = formatUrl({
          ...curUrl,
          query: {
            ...curUrl.query,
            ...query,
          },
        })
J
JJ Kasper 已提交
801 802 803 804 805 806 807 808 809 810 811
        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
812 813
    delete query._nextSprData

J
JJ Kasper 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826
    // 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 已提交
827 828
        isSprData ? 'application/json' : 'text/html; charset=utf-8',
        cachedData.curRevalidate
J
JJ Kasper 已提交
829 830 831 832 833 834
      )

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

J
JJ Kasper 已提交
837 838 839 840 841
    // 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 已提交
842 843 844
      let { pathname } = parseUrl(req.url || '', true)
      pathname = !pathname || pathname === '/' ? '/index' : pathname
      req.url = `/_next/data/${this.buildId}${pathname}.json`
J
JJ Kasper 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    }

    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 }
877
    })
J
JJ Kasper 已提交
878 879 880 881 882 883 884 885

    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 已提交
886 887
            isSprData ? 'application/json' : 'text/html; charset=utf-8',
            sprRevalidate
J
JJ Kasper 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
          )
        }

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

        return null
      }
    )
903 904
  }

J
Joe Haddad 已提交
905
  public renderToHTML(
J
Joe Haddad 已提交
906 907 908 909
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
J
Joe Haddad 已提交
910 911 912 913 914 915 916
    {
      amphtml,
      dataOnly,
      hasAmp,
    }: {
      amphtml?: boolean
      hasAmp?: boolean
917 918
      dataOnly?: boolean
    } = {}
J
Joe Haddad 已提交
919
  ): Promise<string | null> {
J
Joe Haddad 已提交
920 921
    return this.findPageComponents(pathname, query)
      .then(
922
        result => {
J
Joe Haddad 已提交
923 924 925 926
          return this.renderToHTMLWithComponents(
            req,
            res,
            pathname,
927 928 929
            result.unstable_getStaticProps
              ? { _nextSprData: query._nextSprData }
              : query,
J
Joe Haddad 已提交
930
            result,
931
            { ...this.renderOpts, amphtml, hasAmp, dataOnly }
J
Joe Haddad 已提交
932 933
          )
        },
934
        err => {
J
Joe Haddad 已提交
935 936 937 938 939 940 941 942 943 944 945
          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(
946 947
              result => {
                return this.renderToHTMLWithComponents(
J
Joe Haddad 已提交
948 949 950
                  req,
                  res,
                  dynamicRoute.page,
J
JJ Kasper 已提交
951 952 953 954 955 956 957
                  // only add params for SPR enabled pages
                  {
                    ...(result.unstable_getStaticProps
                      ? { _nextSprData: query._nextSprData }
                      : query),
                    ...params,
                  },
J
Joe Haddad 已提交
958
                  result,
J
JJ Kasper 已提交
959 960 961 962 963 964
                  {
                    ...this.renderOpts,
                    amphtml,
                    hasAmp,
                    dataOnly,
                  }
965
                )
966
              }
J
Joe Haddad 已提交
967 968 969 970
            )
          }

          return Promise.reject(err)
971
        }
J
Joe Haddad 已提交
972
      )
973
      .catch(err => {
J
Joe Haddad 已提交
974 975 976 977 978 979 980 981 982
        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 已提交
983 984
  }

J
Joe Haddad 已提交
985 986 987 988 989
  public async renderError(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
990
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
991 992 993
  ): Promise<void> {
    res.setHeader(
      'Cache-Control',
994
      'no-cache, no-store, max-age=0, must-revalidate'
J
Joe Haddad 已提交
995
    )
N
Naoyuki Kanezawa 已提交
996
    const html = await this.renderErrorToHTML(err, req, res, pathname, query)
997
    if (html === null) {
998 999
      return
    }
1000
    return this.sendHTML(req, res, html)
N
nkzawa 已提交
1001 1002
  }

J
Joe Haddad 已提交
1003 1004 1005 1006 1007
  public async renderErrorToHTML(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    _pathname: string,
1008
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1009
  ) {
J
Joe Haddad 已提交
1010
    const result = await this.findPageComponents('/_error', query)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    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 已提交
1030 1031
  }

J
Joe Haddad 已提交
1032 1033 1034
  public async render404(
    req: IncomingMessage,
    res: ServerResponse,
1035
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1036
  ): Promise<void> {
1037 1038
    const url: any = req.url
    const { pathname, query } = parsedUrl ? parsedUrl : parseUrl(url, true)
1039
    if (!pathname) {
1040 1041
      throw new Error('pathname is undefined')
    }
N
Naoyuki Kanezawa 已提交
1042
    res.statusCode = 404
1043
    return this.renderError(null, req, res, pathname, query)
N
Naoyuki Kanezawa 已提交
1044
  }
N
Naoyuki Kanezawa 已提交
1045

J
Joe Haddad 已提交
1046 1047 1048 1049
  public async serveStatic(
    req: IncomingMessage,
    res: ServerResponse,
    path: string,
1050
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1051
  ): Promise<void> {
A
Arunoda Susiripala 已提交
1052
    if (!this.isServeableUrl(path)) {
1053
      return this.render404(req, res, parsedUrl)
A
Arunoda Susiripala 已提交
1054 1055
    }

1056 1057 1058 1059 1060 1061
    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 已提交
1062
    try {
1063
      await serveStatic(req, res, path)
N
Naoyuki Kanezawa 已提交
1064
    } catch (err) {
T
Tim Neutkens 已提交
1065
      if (err.code === 'ENOENT' || err.statusCode === 404) {
1066
        this.render404(req, res, parsedUrl)
1067 1068 1069
      } else if (err.statusCode === 412) {
        res.statusCode = 412
        return this.renderError(err, req, res, path)
N
Naoyuki Kanezawa 已提交
1070 1071 1072 1073 1074 1075
      } else {
        throw err
      }
    }
  }

1076
  private isServeableUrl(path: string): boolean {
A
Arunoda Susiripala 已提交
1077 1078
    const resolved = resolve(path)
    if (
1079
      resolved.indexOf(join(this.distDir) + sep) !== 0 &&
1080 1081
      resolved.indexOf(join(this.dir, 'static') + sep) !== 0 &&
      resolved.indexOf(join(this.dir, 'public') + sep) !== 0
A
Arunoda Susiripala 已提交
1082 1083 1084 1085 1086 1087 1088 1089
    ) {
      // Seems like the user is trying to traverse the filesystem.
      return false
    }

    return true
  }

1090
  protected readBuildId(): string {
1091 1092 1093 1094 1095
    const buildIdFile = join(this.distDir, BUILD_ID_FILE)
    try {
      return fs.readFileSync(buildIdFile, 'utf8').trim()
    } catch (err) {
      if (!fs.existsSync(buildIdFile)) {
J
Joe Haddad 已提交
1096
        throw new Error(
1097
          `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 已提交
1098
        )
1099 1100 1101
      }

      throw err
1102
    }
1103
  }
1104 1105 1106 1107

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