next-server.ts 52.4 KB
Newer Older
G
Guy Bedford 已提交
1
import compression from 'next/dist/compiled/compression'
J
Joe Haddad 已提交
2
import fs from 'fs'
3
import chalk from 'next/dist/compiled/chalk'
J
Joe Haddad 已提交
4
import { IncomingMessage, ServerResponse } from 'http'
G
Guy Bedford 已提交
5
import Proxy from 'next/dist/compiled/http-proxy'
6
import { join, relative, resolve, sep } from 'path'
7 8 9 10 11
import {
  parse as parseQs,
  stringify as stringifyQs,
  ParsedUrlQuery,
} from 'querystring'
12
import { format as formatUrl, parse as parseUrl, UrlWithParsedQuery } from 'url'
13
import { PrerenderManifest } from '../../build'
J
Joe Haddad 已提交
14 15 16 17 18 19
import {
  getRedirectStatus,
  Header,
  Redirect,
  Rewrite,
  RouteType,
20 21
  CustomRoutes,
} from '../../lib/load-custom-routes'
J
JJ Kasper 已提交
22
import { withCoalescedInvoke } from '../../lib/coalesced-function'
J
Joe Haddad 已提交
23 24
import {
  BUILD_ID_FILE,
25
  CLIENT_PUBLIC_FILES_PATH,
J
Joe Haddad 已提交
26 27
  CLIENT_STATIC_FILES_PATH,
  CLIENT_STATIC_FILES_RUNTIME,
28
  PAGES_MANIFEST,
J
Joe Haddad 已提交
29
  PHASE_PRODUCTION_SERVER,
J
Joe Haddad 已提交
30
  PRERENDER_MANIFEST,
31
  ROUTES_MANIFEST,
32
  SERVERLESS_DIRECTORY,
J
Joe Haddad 已提交
33
  SERVER_DIRECTORY,
T
Tim Neutkens 已提交
34
} from '../lib/constants'
J
Joe Haddad 已提交
35 36 37 38
import {
  getRouteMatcher,
  getRouteRegex,
  getSortedRoutes,
39
  isDynamicRoute,
J
Joe Haddad 已提交
40
} from '../lib/router/utils'
41
import * as envConfig from '../lib/runtime-config'
J
Joe Haddad 已提交
42
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
43 44 45 46 47 48 49
import {
  apiResolver,
  setLazyProp,
  getCookieParser,
  tryGetPreviewData,
  __ApiPreviewProps,
} from './api-utils'
50
import loadConfig, { isTargetLikeServerless } from './config'
51
import pathMatch from '../lib/router/utils/path-match'
J
Joe Haddad 已提交
52
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
53
import { loadComponents, LoadComponentsReturnType } from './load-components'
J
Joe Haddad 已提交
54
import { normalizePagePath } from './normalize-page-path'
55
import { RenderOpts, RenderOptsPartial, renderToHTML } from './render'
P
Prateek Bhatnagar 已提交
56
import { getPagePath, requireFontManifest } from './require'
57 58 59
import Router, {
  DynamicRoutes,
  PageChecker,
J
Joe Haddad 已提交
60 61 62
  Params,
  route,
  Route,
63
} from './router'
64
import prepareDestination from '../lib/router/utils/prepare-destination'
65
import { sendPayload } from './send-payload'
J
Joe Haddad 已提交
66
import { serveStatic } from './serve-static'
67
import { IncrementalCache } from './incremental-cache'
68
import { execOnce } from '../lib/utils'
69
import { isBlockedPage } from './utils'
70
import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp'
71
import { loadEnvConfig } from '@next/env'
72
import './node-polyfill-fetch'
J
Jan Potoms 已提交
73
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
74
import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
75
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
P
Prateek Bhatnagar 已提交
76
import { FontManifest } from './font-utils'
77
import { denormalizePagePath } from './denormalize-page-path'
78 79 80
import accept from '@hapi/accept'
import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie'
81
import * as Log from '../../build/output/log'
82
import { detectDomainLocale } from '../lib/i18n/detect-domain-locale'
83
import cookie from 'next/dist/compiled/cookie'
J
JJ Kasper 已提交
84 85

const getCustomRouteMatcher = pathMatch(true)
86 87 88

type NextConfig = any

89 90 91 92 93 94
type Middleware = (
  req: IncomingMessage,
  res: ServerResponse,
  next: (err?: Error) => void
) => void

95 96 97 98 99
type FindComponentsResult = {
  components: LoadComponentsReturnType
  query: ParsedUrlQuery
}

T
Tim Neutkens 已提交
100
export type ServerConstructor = {
101 102 103
  /**
   * Where the Next project is located - @default '.'
   */
J
Joe Haddad 已提交
104
  dir?: string
105 106 107
  /**
   * Hide error messages containing server information - @default false
   */
J
Joe Haddad 已提交
108
  quiet?: boolean
109 110 111
  /**
   * Object what you would use in next.config.js - @default {}
   */
112
  conf?: NextConfig
J
JJ Kasper 已提交
113
  dev?: boolean
114
  customServer?: boolean
115
}
116

N
nkzawa 已提交
117
export default class Server {
118 119 120 121
  dir: string
  quiet: boolean
  nextConfig: NextConfig
  distDir: string
122
  pagesDir?: string
123
  publicDir: string
124
  hasStaticDir: boolean
125
  serverBuildDir: string
J
Jan Potoms 已提交
126
  pagesManifest?: PagesManifest
127 128
  buildId: string
  renderOpts: {
T
Tim Neutkens 已提交
129
    poweredByHeader: boolean
J
Joe Haddad 已提交
130 131 132
    buildId: string
    generateEtags: boolean
    runtimeConfig?: { [key: string]: any }
133 134 135
    assetPrefix?: string
    canonicalBase: string
    dev?: boolean
136
    previewProps: __ApiPreviewProps
137
    customServer?: boolean
138
    ampOptimizerConfig?: { [key: string]: any }
139
    basePath: string
P
Prateek Bhatnagar 已提交
140
    optimizeFonts: boolean
A
Alex Castle 已提交
141
    images: string
P
Prateek Bhatnagar 已提交
142
    fontManifest: FontManifest
143
    optimizeImages: boolean
144 145
    locale?: string
    locales?: string[]
146
    defaultLocale?: string
147
  }
148
  private compression?: Middleware
J
JJ Kasper 已提交
149
  private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
150
  private incrementalCache: IncrementalCache
151
  router: Router
152
  protected dynamicRoutes?: DynamicRoutes
153
  protected customRoutes: CustomRoutes
154

J
Joe Haddad 已提交
155 156 157 158
  public constructor({
    dir = '.',
    quiet = false,
    conf = null,
J
JJ Kasper 已提交
159
    dev = false,
160
    customServer = true,
J
Joe Haddad 已提交
161
  }: ServerConstructor = {}) {
N
nkzawa 已提交
162
    this.dir = resolve(dir)
N
Naoyuki Kanezawa 已提交
163
    this.quiet = quiet
T
Tim Neutkens 已提交
164
    const phase = this.currentPhase()
165
    loadEnvConfig(this.dir, dev, Log)
166

167
    this.nextConfig = loadConfig(phase, this.dir, conf)
168
    this.distDir = join(this.dir, this.nextConfig.distDir)
169
    this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
170
    this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
T
Tim Neutkens 已提交
171

172 173
    // Only serverRuntimeConfig needs the default
    // publicRuntimeConfig gets it's default in client/index.js
J
Joe Haddad 已提交
174 175 176 177 178
    const {
      serverRuntimeConfig = {},
      publicRuntimeConfig,
      assetPrefix,
      generateEtags,
179
      compress,
J
Joe Haddad 已提交
180
    } = this.nextConfig
181

T
Tim Neutkens 已提交
182
    this.buildId = this.readBuildId()
183

184
    this.renderOpts = {
T
Tim Neutkens 已提交
185
      poweredByHeader: this.nextConfig.poweredByHeader,
186
      canonicalBase: this.nextConfig.amp.canonicalBase,
187
      buildId: this.buildId,
188
      generateEtags,
189
      previewProps: this.getPreviewProps(),
190
      customServer: customServer === true ? true : undefined,
191
      ampOptimizerConfig: this.nextConfig.experimental.amp?.optimizer,
192
      basePath: this.nextConfig.basePath,
A
Alex Castle 已提交
193
      images: JSON.stringify(this.nextConfig.images),
194 195 196 197 198
      optimizeFonts: this.nextConfig.experimental.optimizeFonts && !dev,
      fontManifest:
        this.nextConfig.experimental.optimizeFonts && !dev
          ? requireFontManifest(this.distDir, this._isLikeServerless)
          : null,
199
      optimizeImages: this.nextConfig.experimental.optimizeImages,
200
      defaultLocale: this.nextConfig.experimental.i18n?.defaultLocale,
201
    }
N
Naoyuki Kanezawa 已提交
202

203 204
    // Only the `publicRuntimeConfig` key is exposed to the client side
    // It'll be rendered as part of __NEXT_DATA__ on the client side
205
    if (Object.keys(publicRuntimeConfig).length > 0) {
206
      this.renderOpts.runtimeConfig = publicRuntimeConfig
207 208
    }

209
    if (compress && this.nextConfig.target === 'server') {
210 211 212
      this.compression = compression() as Middleware
    }

213
    // Initialize next/config with the environment configuration
214 215 216 217
    envConfig.setConfig({
      serverRuntimeConfig,
      publicRuntimeConfig,
    })
218

219 220 221 222 223 224 225 226 227 228
    this.serverBuildDir = join(
      this.distDir,
      this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
    )
    const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)

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

229
    this.customRoutes = this.getCustomRoutes()
J
JJ Kasper 已提交
230
    this.router = new Router(this.generateRoutes())
231
    this.setAssetPrefix(assetPrefix)
J
JJ Kasper 已提交
232

233 234 235
    // call init-server middleware, this is also handled
    // individually in serverless bundles when deployed
    if (!dev && this.nextConfig.experimental.plugins) {
236 237
      const initServer = require(join(this.serverBuildDir, 'init-server.js'))
        .default
238
      this.onErrorMiddleware = require(join(
239
        this.serverBuildDir,
240 241 242 243 244
        'on-error-server.js'
      )).default
      initServer()
    }

245
    this.incrementalCache = new IncrementalCache({
J
JJ Kasper 已提交
246 247 248 249
      dev,
      distDir: this.distDir,
      pagesDir: join(
        this.distDir,
250
        this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
J
JJ Kasper 已提交
251 252 253 254
        'pages'
      ),
      flushToDisk: this.nextConfig.experimental.sprFlushToDisk,
    })
P
Prateek Bhatnagar 已提交
255 256 257 258 259 260 261 262 263 264

    /**
     * This sets environment variable to be used at the time of SSR by head.tsx.
     * Using this from process.env allows targetting both serverless and SSR by calling
     * `process.env.__NEXT_OPTIMIZE_FONTS`.
     * TODO(prateekbh@): Remove this when experimental.optimizeFonts are being clened up.
     */
    if (this.renderOpts.optimizeFonts) {
      process.env.__NEXT_OPTIMIZE_FONTS = JSON.stringify(true)
    }
265 266 267
    if (this.renderOpts.optimizeImages) {
      process.env.__NEXT_OPTIMIZE_IMAGES = JSON.stringify(true)
    }
N
Naoyuki Kanezawa 已提交
268
  }
N
nkzawa 已提交
269

270
  protected currentPhase(): string {
271
    return PHASE_PRODUCTION_SERVER
272 273
  }

274 275 276 277
  private logError(err: Error): void {
    if (this.onErrorMiddleware) {
      this.onErrorMiddleware({ err })
    }
278
    if (this.quiet) return
279
    console.error(err)
280 281
  }

282
  private async handleRequest(
J
Joe Haddad 已提交
283 284
    req: IncomingMessage,
    res: ServerResponse,
285
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
286
  ): Promise<void> {
287 288
    setLazyProp({ req: req as any }, 'cookies', getCookieParser(req))

289
    // Parse url if parsedUrl not provided
290
    if (!parsedUrl || typeof parsedUrl !== 'object') {
291 292
      const url: any = req.url
      parsedUrl = parseUrl(url, true)
293
    }
294

295 296 297
    // 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 已提交
298
    }
299

300
    const { basePath } = this.nextConfig
301
    const { i18n } = this.nextConfig.experimental
302

303 304 305 306 307
    if (basePath && req.url?.startsWith(basePath)) {
      // store original URL to allow checking if basePath was
      // provided or not
      ;(req as any)._nextHadBasePath = true
      req.url = req.url!.replace(basePath, '') || '/'
T
Tim Neutkens 已提交
308 309
    }

310
    if (i18n && !parsedUrl.pathname?.startsWith('/_next')) {
311 312
      // get pathname from URL with basePath stripped for locale detection
      const { pathname, ...parsed } = parseUrl(req.url || '/')
313
      let defaultLocale = i18n.defaultLocale
314
      let detectedLocale = detectLocaleCookie(req, i18n.locales)
315 316 317 318
      let acceptPreferredLocale = accept.language(
        req.headers['accept-language'],
        i18n.locales
      )
319

320 321 322 323 324
      const detectedDomain = detectDomainLocale(i18n.domains, req)
      if (detectedDomain) {
        defaultLocale = detectedDomain.defaultLocale
        detectedLocale = defaultLocale
      }
325

326 327
      // if not domain specific locale use accept-language preferred
      detectedLocale = detectedLocale || acceptPreferredLocale
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      let localeDomainRedirect: string | undefined
      const localePathResult = normalizeLocalePath(pathname!, i18n.locales)

      if (localePathResult.detectedLocale) {
        detectedLocale = localePathResult.detectedLocale
        req.url = formatUrl({
          ...parsed,
          pathname: localePathResult.pathname,
        })
        parsedUrl.pathname = localePathResult.pathname

        // check if the locale prefix matches a domain's defaultLocale
        // and we're on a locale specific domain if so redirect to that domain
        if (detectedDomain) {
          const matchedDomain = detectDomainLocale(
            i18n.domains,
            undefined,
            detectedLocale
          )

          if (matchedDomain) {
            localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
              matchedDomain?.domain
            }`
          }
        }
      }

357
      const denormalizedPagePath = denormalizePagePath(pathname || '/')
358
      const detectedDefaultLocale =
359 360
        !detectedLocale ||
        detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
361
      const shouldStripDefaultLocale =
362
        detectedDefaultLocale &&
363 364
        denormalizedPagePath.toLowerCase() ===
          `/${i18n.defaultLocale.toLowerCase()}`
365

366 367
      const shouldAddLocalePrefix =
        !detectedDefaultLocale && denormalizedPagePath === '/'
368

369
      detectedLocale = detectedLocale || i18n.defaultLocale
370

371 372
      if (
        i18n.localeDetection !== false &&
373 374 375
        (localeDomainRedirect ||
          shouldAddLocalePrefix ||
          shouldStripDefaultLocale)
376
      ) {
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        // set the NEXT_LOCALE cookie when a user visits the default locale
        // with the locale prefix so that they aren't redirected back to
        // their accept-language preferred locale
        if (
          shouldStripDefaultLocale &&
          acceptPreferredLocale !== defaultLocale
        ) {
          const previous = res.getHeader('set-cookie')

          res.setHeader('set-cookie', [
            ...(typeof previous === 'string'
              ? [previous]
              : Array.isArray(previous)
              ? previous
              : []),
            cookie.serialize('NEXT_LOCALE', defaultLocale, {
              httpOnly: true,
              path: '/',
            }),
          ])
        }

399 400 401 402 403
        res.setHeader(
          'Location',
          formatUrl({
            // make sure to include any query values when redirecting
            ...parsed,
404 405 406 407 408
            pathname: localeDomainRedirect
              ? localeDomainRedirect
              : shouldStripDefaultLocale
              ? '/'
              : `/${detectedLocale}`,
409 410 411 412
          })
        )
        res.statusCode = 307
        res.end()
413
        return
414
      }
415
      parsedUrl.query.__nextLocale = detectedLocale || defaultLocale
416 417
    }

418
    res.statusCode = 200
419 420 421
    try {
      return await this.run(req, res, parsedUrl)
    } catch (err) {
J
Joe Haddad 已提交
422 423 424
      this.logError(err)
      res.statusCode = 500
      res.end('Internal Server Error')
425
    }
426 427
  }

428
  public getRequestHandler() {
429
    return this.handleRequest.bind(this)
N
nkzawa 已提交
430 431
  }

432
  public setAssetPrefix(prefix?: string): void {
433
    this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
434 435
  }

436
  // Backwards compatibility
437
  public async prepare(): Promise<void> {}
N
nkzawa 已提交
438

T
Tim Neutkens 已提交
439
  // Backwards compatibility
440
  protected async close(): Promise<void> {}
T
Tim Neutkens 已提交
441

442
  protected setImmutableAssetCacheControl(res: ServerResponse): void {
T
Tim Neutkens 已提交
443
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
N
nkzawa 已提交
444 445
  }

446
  protected getCustomRoutes(): CustomRoutes {
J
JJ Kasper 已提交
447 448 449
    return require(join(this.distDir, ROUTES_MANIFEST))
  }

450 451 452 453
  private _cachedPreviewManifest: PrerenderManifest | undefined
  protected getPrerenderManifest(): PrerenderManifest {
    if (this._cachedPreviewManifest) {
      return this._cachedPreviewManifest
J
Joe Haddad 已提交
454
    }
455 456 457 458 459 460
    const manifest = require(join(this.distDir, PRERENDER_MANIFEST))
    return (this._cachedPreviewManifest = manifest)
  }

  protected getPreviewProps(): __ApiPreviewProps {
    return this.getPrerenderManifest().preview
J
Joe Haddad 已提交
461 462
  }

463
  protected generateRoutes(): {
464
    basePath: string
465 466
    headers: Route[]
    rewrites: Route[]
467
    fsRoutes: Route[]
468
    redirects: Route[]
469 470
    catchAllRoute: Route
    pageChecker: PageChecker
471
    useFileSystemPublicRoutes: boolean
472 473
    dynamicRoutes: DynamicRoutes | undefined
  } {
474 475 476
    const publicRoutes = fs.existsSync(this.publicDir)
      ? this.generatePublicRoutes()
      : []
J
JJ Kasper 已提交
477

478
    const staticFilesRoute = this.hasStaticDir
479 480 481 482 483
      ? [
          {
            // 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.
484
            // See more: https://github.com/vercel/next.js/issues/2617
485
            match: route('/static/:path*'),
486
            name: 'static catchall',
487
            fn: async (req, res, params, parsedUrl) => {
488
              const p = join(this.dir, 'static', ...params.path)
489
              await this.serveStatic(req, res, p, parsedUrl)
490 491 492
              return {
                finished: true,
              }
493 494 495 496
            },
          } as Route,
        ]
      : []
497

498
    const fsRoutes: Route[] = [
T
Tim Neutkens 已提交
499
      {
500
        match: route('/_next/static/:path*'),
501 502
        type: 'route',
        name: '_next/static catchall',
503
        fn: async (req, res, params, parsedUrl) => {
504
          // make sure to 404 for /_next/static itself
505 506 507 508 509 510
          if (!params.path) {
            await this.render404(req, res, parsedUrl)
            return {
              finished: true,
            }
          }
511

J
Joe Haddad 已提交
512 513 514
          if (
            params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
            params.path[0] === 'chunks' ||
515 516
            params.path[0] === 'css' ||
            params.path[0] === 'media' ||
517
            params.path[0] === this.buildId ||
518
            params.path[0] === 'pages' ||
519
            params.path[1] === 'pages'
J
Joe Haddad 已提交
520
          ) {
T
Tim Neutkens 已提交
521
            this.setImmutableAssetCacheControl(res)
522
          }
J
Joe Haddad 已提交
523 524 525
          const p = join(
            this.distDir,
            CLIENT_STATIC_FILES_PATH,
526
            ...(params.path || [])
J
Joe Haddad 已提交
527
          )
528
          await this.serveStatic(req, res, p, parsedUrl)
529 530 531
          return {
            finished: true,
          }
532
        },
533
      },
J
JJ Kasper 已提交
534 535
      {
        match: route('/_next/data/:path*'),
536 537
        type: 'route',
        name: '_next/data catchall',
J
JJ Kasper 已提交
538
        fn: async (req, res, params, _parsedUrl) => {
J
JJ Kasper 已提交
539 540 541
          // 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) {
542 543 544 545
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
546 547 548 549 550 551
          }
          // 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')) {
552 553 554 555
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
556 557 558
          }

          // re-create page's pathname
559 560
          let pathname = `/${params.path.join('/')}`

561 562 563 564
          const { i18n } = this.nextConfig.experimental

          if (i18n) {
            const localePathResult = normalizeLocalePath(pathname, i18n.locales)
565 566
            const { defaultLocale } =
              detectDomainLocale(i18n.domains, req) || {}
567
            let detectedLocale = defaultLocale
568 569 570 571 572

            if (localePathResult.detectedLocale) {
              pathname = localePathResult.pathname
              detectedLocale = localePathResult.detectedLocale
            }
573
            _parsedUrl.query.__nextLocale = detectedLocale!
574 575
          }
          pathname = getRouteFromAssetPath(pathname, '.json')
J
JJ Kasper 已提交
576

J
JJ Kasper 已提交
577
          const parsedUrl = parseUrl(pathname, true)
578

J
JJ Kasper 已提交
579 580 581 582
          await this.render(
            req,
            res,
            pathname,
583
            { ..._parsedUrl.query, _nextDataReq: '1' },
J
JJ Kasper 已提交
584 585
            parsedUrl
          )
586 587 588
          return {
            finished: true,
          }
J
JJ Kasper 已提交
589 590
        },
      },
T
Tim Neutkens 已提交
591
      {
592
        match: route('/_next/:path*'),
593 594
        type: 'route',
        name: '_next catchall',
T
Tim Neutkens 已提交
595
        // This path is needed because `render()` does a check for `/_next` and the calls the routing again
596
        fn: async (req, res, _params, parsedUrl) => {
T
Tim Neutkens 已提交
597
          await this.render404(req, res, parsedUrl)
598 599 600
          return {
            finished: true,
          }
L
Lukáš Huvar 已提交
601 602
        },
      },
603 604
      ...publicRoutes,
      ...staticFilesRoute,
T
Tim Neutkens 已提交
605
    ]
606

607 608 609 610 611 612
    const getCustomRouteBasePath = (r: { basePath?: false }) => {
      return r.basePath !== false && this.renderOpts.dev
        ? this.nextConfig.basePath
        : ''
    }

613 614 615 616
    const getCustomRoute = (r: Rewrite | Redirect | Header, type: RouteType) =>
      ({
        ...r,
        type,
617
        match: getCustomRouteMatcher(`${getCustomRouteBasePath(r)}${r.source}`),
618 619 620 621 622 623 624
        name: type,
        fn: async (_req, _res, _params, _parsedUrl) => ({ finished: false }),
      } as Route & Rewrite & Header)

    const updateHeaderValue = (value: string, params: Params): string => {
      if (!value.includes(':')) {
        return value
625
      }
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

      for (const key of Object.keys(params)) {
        if (value.includes(`:${key}`)) {
          value = value
            .replace(
              new RegExp(`:${key}\\*`, 'g'),
              `:${key}--ESCAPED_PARAM_ASTERISKS`
            )
            .replace(
              new RegExp(`:${key}\\?`, 'g'),
              `:${key}--ESCAPED_PARAM_QUESTION`
            )
            .replace(
              new RegExp(`:${key}\\+`, 'g'),
              `:${key}--ESCAPED_PARAM_PLUS`
            )
            .replace(
              new RegExp(`:${key}(?!\\w)`, 'g'),
              `--ESCAPED_PARAM_COLON${key}`
            )
        }
647
      }
648 649 650 651 652 653 654 655 656 657 658 659
      value = value
        .replace(/(:|\*|\?|\+|\(|\)|\{|\})/g, '\\$1')
        .replace(/--ESCAPED_PARAM_PLUS/g, '+')
        .replace(/--ESCAPED_PARAM_COLON/g, ':')
        .replace(/--ESCAPED_PARAM_QUESTION/g, '?')
        .replace(/--ESCAPED_PARAM_ASTERISKS/g, '*')

      // the value needs to start with a forward-slash to be compiled
      // correctly
      return compilePathToRegex(`/${value}`, { validate: false })(
        params
      ).substr(1)
660
    }
661

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    // Headers come very first
    const headers = this.customRoutes.headers.map((r) => {
      const headerRoute = getCustomRoute(r, 'header')
      return {
        match: headerRoute.match,
        type: headerRoute.type,
        name: `${headerRoute.type} ${headerRoute.source} header route`,
        fn: async (_req, res, params, _parsedUrl) => {
          const hasParams = Object.keys(params).length > 0

          for (const header of (headerRoute as Header).headers) {
            let { key, value } = header
            if (hasParams) {
              key = updateHeaderValue(key, params)
              value = updateHeaderValue(value, params)
677
            }
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
            res.setHeader(key, value)
          }
          return { finished: false }
        },
      } as Route
    })

    const redirects = this.customRoutes.redirects.map((redirect) => {
      const redirectRoute = getCustomRoute(redirect, 'redirect')
      return {
        type: redirectRoute.type,
        match: redirectRoute.match,
        statusCode: redirectRoute.statusCode,
        name: `Redirect route`,
        fn: async (_req, res, params, parsedUrl) => {
          const { parsedDestination } = prepareDestination(
            redirectRoute.destination,
            params,
696 697 698
            parsedUrl.query,
            false,
            getCustomRouteBasePath(redirectRoute)
699
          )
700 701

          const { query } = parsedDestination
702
          delete (parsedDestination as any).query
703 704 705 706 707

          parsedDestination.search = stringifyQs(query, undefined, undefined, {
            encodeURIComponent: (str: string) => str,
          } as any)

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
          const updatedDestination = formatUrl(parsedDestination)

          res.setHeader('Location', updatedDestination)
          res.statusCode = getRedirectStatus(redirectRoute as Redirect)

          // Since IE11 doesn't support the 308 header add backwards
          // compatibility using refresh header
          if (res.statusCode === 308) {
            res.setHeader('Refresh', `0;url=${updatedDestination}`)
          }

          res.end()
          return {
            finished: true,
          }
        },
      } as Route
    })

    const rewrites = this.customRoutes.rewrites.map((rewrite) => {
      const rewriteRoute = getCustomRoute(rewrite, 'rewrite')
      return {
730
        ...rewriteRoute,
731 732 733 734 735 736 737 738 739
        check: true,
        type: rewriteRoute.type,
        name: `Rewrite route`,
        match: rewriteRoute.match,
        fn: async (req, res, params, parsedUrl) => {
          const { newUrl, parsedDestination } = prepareDestination(
            rewriteRoute.destination,
            params,
            parsedUrl.query,
740 741
            true,
            getCustomRouteBasePath(rewriteRoute)
742
          )
743

744 745
          // external rewrite, proxy it
          if (parsedDestination.protocol) {
746
            const { query } = parsedDestination
747
            delete (parsedDestination as any).query
748 749 750 751 752 753 754
            parsedDestination.search = stringifyQs(
              query,
              undefined,
              undefined,
              { encodeURIComponent: (str) => str }
            )

755 756 757 758 759 760 761 762 763 764 765
            const target = formatUrl(parsedDestination)
            const proxy = new Proxy({
              target,
              changeOrigin: true,
              ignorePath: true,
            })
            proxy.web(req, res)

            proxy.on('error', (err: Error) => {
              console.error(`Error occurred proxying ${target}`, err)
            })
766 767 768
            return {
              finished: true,
            }
769 770
          }
          ;(req as any)._nextRewroteUrl = newUrl
771 772
          ;(req as any)._nextDidRewrite =
            (req as any)._nextRewroteUrl !== req.url
773

774 775 776 777 778 779 780 781
          return {
            finished: false,
            pathname: newUrl,
            query: parsedDestination.query,
          }
        },
      } as Route
    })
782 783 784 785 786 787

    const catchAllRoute: Route = {
      match: route('/:path*'),
      type: 'route',
      name: 'Catchall render',
      fn: async (req, res, params, parsedUrl) => {
J
Jan Potoms 已提交
788
        let { pathname, query } = parsedUrl
789 790 791 792
        if (!pathname) {
          throw new Error('pathname is undefined')
        }

J
Jan Potoms 已提交
793
        // next.js core assumes page path without trailing slash
794
        pathname = removePathTrailingSlash(pathname)
J
Jan Potoms 已提交
795

796
        if (params?.path?.[0] === 'api') {
797 798 799
          const handled = await this.handleApiRequest(
            req as NextApiRequest,
            res as NextApiResponse,
800
            pathname,
801
            query
802 803 804 805 806 807 808
          )
          if (handled) {
            return { finished: true }
          }
        }

        await this.render(req, res, pathname, query, parsedUrl)
809 810 811 812
        return {
          finished: true,
        }
      },
813
    }
814

815
    const { useFileSystemPublicRoutes } = this.nextConfig
J
Joe Haddad 已提交
816

817 818
    if (useFileSystemPublicRoutes) {
      this.dynamicRoutes = this.getDynamicRoutes()
819
    }
N
nkzawa 已提交
820

821
    return {
822
      headers,
823
      fsRoutes,
824 825
      rewrites,
      redirects,
826
      catchAllRoute,
827
      useFileSystemPublicRoutes,
828
      dynamicRoutes: this.dynamicRoutes,
829
      basePath: this.nextConfig.basePath,
830 831
      pageChecker: this.hasPage.bind(this),
    }
T
Tim Neutkens 已提交
832 833
  }

834
  private async getPagePath(pathname: string): Promise<string> {
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    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
  }

852 853 854 855 856
  protected async _beforeCatchAllRender(
    _req: IncomingMessage,
    _res: ServerResponse,
    _params: Params,
    _parsedUrl: UrlWithParsedQuery
857
  ): Promise<boolean> {
858 859 860
    return false
  }

861
  // Used to build API page in development
T
Tim Neutkens 已提交
862
  protected async ensureApiPage(_pathname: string): Promise<void> {}
863

L
Lukáš Huvar 已提交
864 865 866 867 868 869
  /**
   * Resolves `API` request, in development builds on demand
   * @param req http request
   * @param res http response
   * @param pathname path of request
   */
J
Joe Haddad 已提交
870
  private async handleApiRequest(
871 872
    req: IncomingMessage,
    res: ServerResponse,
873 874
    pathname: string,
    query: ParsedUrlQuery
875
  ): Promise<boolean> {
876
    let page = pathname
L
Lukáš Huvar 已提交
877
    let params: Params | boolean = false
878
    let pageFound = await this.hasPage(page)
J
JJ Kasper 已提交
879

880
    if (!pageFound && this.dynamicRoutes) {
L
Lukáš Huvar 已提交
881 882
      for (const dynamicRoute of this.dynamicRoutes) {
        params = dynamicRoute.match(pathname)
883
        if (dynamicRoute.page.startsWith('/api') && params) {
884 885
          page = dynamicRoute.page
          pageFound = true
L
Lukáš Huvar 已提交
886 887 888 889 890
          break
        }
      }
    }

891
    if (!pageFound) {
892
      return false
J
JJ Kasper 已提交
893
    }
894 895 896 897
    // Make sure the page is built before getting the path
    // or else it won't be in the manifest yet
    await this.ensureApiPage(page)

898 899 900 901 902 903 904 905 906 907
    let builtPagePath
    try {
      builtPagePath = await this.getPagePath(page)
    } catch (err) {
      if (err.code === 'ENOENT') {
        return false
      }
      throw err
    }

908
    const pageModule = await require(builtPagePath)
909
    query = { ...query, ...params }
J
JJ Kasper 已提交
910

911
    if (!this.renderOpts.dev && this._isLikeServerless) {
912
      if (typeof pageModule.default === 'function') {
913
        prepareServerlessUrl(req, query)
914 915
        await pageModule.default(req, res)
        return true
J
JJ Kasper 已提交
916 917 918
      }
    }

J
Joe Haddad 已提交
919 920 921 922 923
    await apiResolver(
      req,
      res,
      query,
      pageModule,
924
      this.renderOpts.previewProps,
925
      false,
J
Joe Haddad 已提交
926 927
      this.onErrorMiddleware
    )
928
    return true
L
Lukáš Huvar 已提交
929 930
  }

931
  protected generatePublicRoutes(): Route[] {
932
    const publicFiles = new Set(
933 934 935
      recursiveReadDirSync(this.publicDir).map((p) =>
        encodeURI(p.replace(/\\/g, '/'))
      )
936 937 938 939 940 941 942
    )

    return [
      {
        match: route('/:path*'),
        name: 'public folder catchall',
        fn: async (req, res, params, parsedUrl) => {
943
          const pathParts: string[] = params.path || []
944 945 946 947 948 949 950 951
          const { basePath } = this.nextConfig

          // if basePath is defined require it be present
          if (basePath) {
            if (pathParts[0] !== basePath.substr(1)) return { finished: false }
            pathParts.shift()
          }

952
          const path = `/${pathParts.join('/')}`
953 954 955 956 957

          if (publicFiles.has(path)) {
            await this.serveStatic(
              req,
              res,
958
              join(this.publicDir, ...pathParts),
959 960
              parsedUrl
            )
961 962 963
            return {
              finished: true,
            }
964 965 966 967 968 969 970
          }
          return {
            finished: false,
          }
        },
      } as Route,
    ]
971 972
  }

973
  protected getDynamicRoutes() {
974 975
    return getSortedRoutes(Object.keys(this.pagesManifest!))
      .filter(isDynamicRoute)
J
Joe Haddad 已提交
976
      .map((page) => ({
977 978 979
        page,
        match: getRouteMatcher(getRouteRegex(page)),
      }))
J
Joe Haddad 已提交
980 981
  }

982
  private handleCompression(req: IncomingMessage, res: ServerResponse): void {
983 984 985 986 987
    if (this.compression) {
      this.compression(req, res, () => {})
    }
  }

988
  protected async run(
J
Joe Haddad 已提交
989 990
    req: IncomingMessage,
    res: ServerResponse,
991
    parsedUrl: UrlWithParsedQuery
992
  ): Promise<void> {
993 994
    this.handleCompression(req, res)

995
    try {
996 997
      const matched = await this.router.execute(req, res, parsedUrl)
      if (matched) {
998 999 1000 1001 1002 1003 1004 1005
        return
      }
    } catch (err) {
      if (err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return this.renderError(null, req, res, '/_error', {})
      }
      throw err
1006 1007
    }

1008
    await this.render404(req, res, parsedUrl)
N
nkzawa 已提交
1009 1010
  }

1011
  protected async sendHTML(
J
Joe Haddad 已提交
1012 1013
    req: IncomingMessage,
    res: ServerResponse,
1014
    html: string
1015
  ): Promise<void> {
T
Tim Neutkens 已提交
1016
    const { generateEtags, poweredByHeader } = this.renderOpts
1017 1018 1019 1020
    return sendPayload(req, res, html, 'html', {
      generateEtags,
      poweredByHeader,
    })
1021 1022
  }

J
Joe Haddad 已提交
1023 1024 1025 1026 1027
  public async render(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
1028
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1029
  ): Promise<void> {
1030 1031 1032 1033 1034 1035
    if (!pathname.startsWith('/')) {
      console.warn(
        `Cannot render page with path "${pathname}", did you mean "/${pathname}"?. See more info here: https://err.sh/next.js/render-no-starting-slash`
      )
    }

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    if (
      this.renderOpts.customServer &&
      pathname === '/index' &&
      !(await this.hasPage('/index'))
    ) {
      // maintain backwards compatibility for custom server
      // (see custom-server integration tests)
      pathname = '/'
    }

1046
    const url: any = req.url
1047

1048 1049 1050 1051
    // we allow custom servers to call render for all URLs
    // so check if we need to serve a static _next file or not.
    // we don't modify the URL for _next/data request but still
    // call render so we special case this to prevent an infinite loop
1052
    if (
1053 1054 1055
      !query._nextDataReq &&
      (url.match(/^\/_next\//) ||
        (this.hasStaticDir && url.match(/^\/static\//)))
1056
    ) {
1057 1058 1059
      return this.handleRequest(req, res, parsedUrl)
    }

1060
    if (isBlockedPage(pathname)) {
1061
      return this.render404(req, res, parsedUrl)
1062 1063
    }

1064
    const html = await this.renderToHTML(req, res, pathname, query)
1065 1066
    // Request was ended by the user
    if (html === null) {
1067 1068 1069
      return
    }

1070
    return this.sendHTML(req, res, html)
N
Naoyuki Kanezawa 已提交
1071
  }
N
nkzawa 已提交
1072

J
Joe Haddad 已提交
1073
  private async findPageComponents(
J
Joe Haddad 已提交
1074
    pathname: string,
1075 1076 1077
    query: ParsedUrlQuery = {},
    params: Params | null = null
  ): Promise<FindComponentsResult | null> {
1078
    let paths = [
1079 1080 1081 1082
      // try serving a static AMP version first
      query.amp ? normalizePagePath(pathname) + '.amp' : null,
      pathname,
    ].filter(Boolean)
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

    if (query.__nextLocale) {
      paths = [
        ...paths.map(
          (path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
        ),
        ...paths,
      ]
    }

1093
    for (const pagePath of paths) {
J
JJ Kasper 已提交
1094
      try {
1095
        const components = await loadComponents(
J
Joe Haddad 已提交
1096
          this.distDir,
1097 1098
          pagePath!,
          !this.renderOpts.dev && this._isLikeServerless
J
Joe Haddad 已提交
1099
        )
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        // if loading an static HTML file the locale is required
        // to be present since all HTML files are output under their locale
        if (
          query.__nextLocale &&
          typeof components.Component === 'string' &&
          !pagePath?.startsWith(`/${query.__nextLocale}`)
        ) {
          const err = new Error('NOT_FOUND')
          ;(err as any).code = 'ENOENT'
          throw err
        }

1112 1113 1114
        return {
          components,
          query: {
1115
            ...(components.getStaticProps
1116 1117
              ? {
                  amp: query.amp,
1118
                  __next404: query.__next404,
1119 1120 1121
                  _nextDataReq: query._nextDataReq,
                  __nextLocale: query.__nextLocale,
                }
1122 1123 1124 1125
              : query),
            ...(params || {}),
          },
        }
J
JJ Kasper 已提交
1126 1127 1128 1129
      } catch (err) {
        if (err.code !== 'ENOENT') throw err
      }
    }
1130
    return null
J
Joe Haddad 已提交
1131 1132
  }

1133
  protected async getStaticPaths(
1134 1135 1136
    pathname: string
  ): Promise<{
    staticPaths: string[] | undefined
1137
    fallbackMode: 'static' | 'blocking' | false
1138
  }> {
1139 1140 1141 1142 1143
    // `staticPaths` is intentionally set to `undefined` as it should've
    // been caught when checking disk data.
    const staticPaths = undefined

    // Read whether or not fallback should exist from the manifest.
1144 1145
    const fallbackField = this.getPrerenderManifest().dynamicRoutes[pathname]
      .fallback
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155
    return {
      staticPaths,
      fallbackMode:
        typeof fallbackField === 'string'
          ? 'static'
          : fallbackField === null
          ? 'blocking'
          : false,
    }
1156 1157
  }

J
Joe Haddad 已提交
1158 1159 1160 1161
  private async renderToHTMLWithComponents(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1162
    { components, query }: FindComponentsResult,
1163
    opts: RenderOptsPartial
1164
  ): Promise<string | null> {
1165
    // we need to ensure the status code if /404 is visited directly
1166
    if (pathname === '/404') {
1167 1168 1169
      res.statusCode = 404
    }

J
JJ Kasper 已提交
1170
    // handle static page
1171 1172
    if (typeof components.Component === 'string') {
      return components.Component
J
Joe Haddad 已提交
1173 1174
    }

J
JJ Kasper 已提交
1175 1176
    // check request state
    const isLikeServerless =
1177 1178
      typeof components.Component === 'object' &&
      typeof (components.Component as any).renderReqToHTML === 'function'
1179 1180 1181
    const isSSG = !!components.getStaticProps
    const isServerProps = !!components.getServerSideProps
    const hasStaticPaths = !!components.getStaticPaths
1182

1183 1184 1185 1186
    if (!query.amp) {
      delete query.amp
    }

1187
    // Toggle whether or not this is a Data request
1188
    const isDataReq = !!query._nextDataReq && (isSSG || isServerProps)
1189 1190
    delete query._nextDataReq

J
JJ Kasper 已提交
1191 1192
    const locale = query.__nextLocale as string
    delete query.__nextLocale
1193 1194 1195

    const { i18n } = this.nextConfig.experimental
    const locales = i18n.locales as string[]
J
JJ Kasper 已提交
1196

1197 1198 1199 1200 1201 1202 1203 1204
    let previewData: string | false | object | undefined
    let isPreviewMode = false

    if (isServerProps || isSSG) {
      previewData = tryGetPreviewData(req, res, this.renderOpts.previewProps)
      isPreviewMode = previewData !== false
    }

1205 1206 1207
    // Compute the iSSG cache key. We use the rewroteUrl since
    // pages with fallback: false are allowed to be rewritten to
    // and we need to look up the path by the rewritten path
1208 1209 1210
    let urlPathname = parseUrl(req.url || '').pathname || '/'

    let resolvedUrlPathname = (req as any)._nextRewroteUrl
1211
      ? (req as any)._nextRewroteUrl
1212
      : urlPathname
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222
    resolvedUrlPathname = removePathTrailingSlash(resolvedUrlPathname)
    urlPathname = removePathTrailingSlash(urlPathname)

    const stripNextDataPath = (path: string) => {
      if (path.includes(this.buildId)) {
        path = denormalizePagePath(
          (path.split(this.buildId).pop() || '/').replace(/\.json$/, '')
        )
      }
1223 1224

      if (this.nextConfig.experimental.i18n) {
J
JJ Kasper 已提交
1225
        return normalizeLocalePath(path, locales).pathname
1226
      }
1227 1228
      return path
    }
1229

1230 1231
    // remove /_next/data prefix from urlPathname so it matches
    // for direct page visit and /_next/data visit
1232 1233 1234
    if (isDataReq) {
      resolvedUrlPathname = stripNextDataPath(resolvedUrlPathname)
      urlPathname = stripNextDataPath(urlPathname)
1235 1236
    }

1237 1238 1239
    const ssgCacheKey =
      isPreviewMode || !isSSG
        ? undefined // Preview mode bypasses the cache
1240 1241 1242
        : `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
            query.amp ? '.amp' : ''
          }`
J
JJ Kasper 已提交
1243

1244 1245 1246 1247 1248 1249 1250 1251 1252
    // In development we use a __next404 query to allow signaling we should
    // render the 404 page after attempting to fetch the _next/data for a
    // fallback page since the fallback page will always be available after
    // reload and we don't want to re-serve it and instead want to 404.
    if (this.renderOpts.dev && isSSG && query.__next404) {
      delete query.__next404
      throw new NoFallbackError()
    }

J
JJ Kasper 已提交
1253
    // Complete the response with cached data if its present
1254 1255 1256
    const cachedData = ssgCacheKey
      ? await this.incrementalCache.get(ssgCacheKey)
      : undefined
1257

J
JJ Kasper 已提交
1258
    if (cachedData) {
1259 1260 1261 1262 1263 1264
      if (cachedData.isNotFound) {
        // we don't currently revalidate when notFound is returned
        // so trigger rendering 404 here
        throw new NoFallbackError()
      }

1265
      const data = isDataReq
J
JJ Kasper 已提交
1266 1267 1268
        ? JSON.stringify(cachedData.pageData)
        : cachedData.html

1269
      sendPayload(
1270
        req,
J
JJ Kasper 已提交
1271 1272
        res,
        data,
1273
        isDataReq ? 'json' : 'html',
1274 1275 1276 1277
        {
          generateEtags: this.renderOpts.generateEtags,
          poweredByHeader: this.renderOpts.poweredByHeader,
        },
1278 1279 1280 1281 1282 1283 1284 1285 1286
        !this.renderOpts.dev
          ? {
              private: isPreviewMode,
              stateful: false, // GSP response
              revalidate:
                cachedData.curRevalidate !== undefined
                  ? cachedData.curRevalidate
                  : /* default to minimum revalidate (this should be an invariant) */ 1,
            }
1287
          : undefined
J
JJ Kasper 已提交
1288 1289 1290 1291 1292 1293
      )

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

J
JJ Kasper 已提交
1296
    // If we're here, that means data is missing or it's stale.
1297 1298 1299 1300 1301 1302
    const maybeCoalesceInvoke = ssgCacheKey
      ? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey, [])
      : (fn: any) => async () => {
          const value = await fn()
          return { isOrigin: true, value }
        }
J
JJ Kasper 已提交
1303

1304 1305 1306 1307 1308
    const doRender = maybeCoalesceInvoke(
      async (): Promise<{
        html: string | null
        pageData: any
        sprRevalidate: number | false
1309
        isNotFound?: boolean
1310 1311 1312 1313
      }> => {
        let pageData: any
        let html: string | null
        let sprRevalidate: number | false
1314
        let isNotFound: boolean | undefined
1315 1316 1317 1318 1319 1320 1321

        let renderResult
        // handle serverless
        if (isLikeServerless) {
          renderResult = await (components.Component as any).renderReqToHTML(
            req,
            res,
P
Prateek Bhatnagar 已提交
1322 1323 1324
            'passthrough',
            {
              fontManifest: this.renderOpts.fontManifest,
1325
              locale,
1326 1327
              locales,
              // defaultLocale,
P
Prateek Bhatnagar 已提交
1328
            }
1329
          )
J
JJ Kasper 已提交
1330

1331 1332 1333
          html = renderResult.html
          pageData = renderResult.renderOpts.pageData
          sprRevalidate = renderResult.renderOpts.revalidate
1334
          isNotFound = renderResult.renderOpts.ssgNotFound
1335
        } else {
1336 1337 1338 1339 1340 1341 1342
          const origQuery = parseUrl(req.url || '', true).query
          const resolvedUrl = formatUrl({
            pathname: resolvedUrlPathname,
            // make sure to only add query values from original URL
            query: origQuery,
          })

1343 1344 1345 1346
          const renderOpts: RenderOpts = {
            ...components,
            ...opts,
            isDataReq,
1347
            resolvedUrl,
1348
            locale,
1349 1350
            locales,
            // defaultLocale,
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
            // For getServerSideProps we need to ensure we use the original URL
            // and not the resolved URL to prevent a hydration mismatch on
            // asPath
            resolvedAsPath: isServerProps
              ? formatUrl({
                  // we use the original URL pathname less the _next/data prefix if
                  // present
                  pathname: urlPathname,
                  query: origQuery,
                })
              : resolvedUrl,
1362
          }
1363

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
          renderResult = await renderToHTML(
            req,
            res,
            pathname,
            query,
            renderOpts
          )

          html = renderResult
          // TODO: change this to a different passing mechanism
          pageData = (renderOpts as any).pageData
          sprRevalidate = (renderOpts as any).revalidate
1376
          isNotFound = (renderOpts as any).ssgNotFound
J
JJ Kasper 已提交
1377 1378
        }

1379
        return { html, pageData, sprRevalidate, isNotFound }
J
JJ Kasper 已提交
1380
      }
1381
    )
J
JJ Kasper 已提交
1382

1383
    const isProduction = !this.renderOpts.dev
J
Joe Haddad 已提交
1384
    const isDynamicPathname = isDynamicRoute(pathname)
1385
    const didRespond = isResSent(res)
1386

1387
    const { staticPaths, fallbackMode } = hasStaticPaths
1388
      ? await this.getStaticPaths(pathname)
1389
      : { staticPaths: undefined, fallbackMode: false }
1390

1391 1392 1393 1394 1395
    // When we did not respond from cache, we need to choose to block on
    // rendering or return a skeleton.
    //
    // * Data requests always block.
    //
1396 1397
    // * Blocking mode fallback always blocks.
    //
1398 1399
    // * Preview mode toggles all pages to be resolved in a blocking manner.
    //
1400
    // * Non-dynamic pages should block (though this is an impossible
1401 1402
    //   case in production).
    //
1403 1404
    // * Dynamic pages should return their skeleton if not defined in
    //   getStaticPaths, then finish the data request on the client-side.
1405
    //
J
Joe Haddad 已提交
1406
    if (
1407
      fallbackMode !== 'blocking' &&
1408
      ssgCacheKey &&
1409 1410 1411
      !didRespond &&
      !isPreviewMode &&
      isDynamicPathname &&
1412 1413
      // Development should trigger fallback when the path is not in
      // `getStaticPaths`
1414 1415
      (isProduction ||
        !staticPaths ||
1416 1417 1418 1419 1420
        // static paths always includes locale so make sure it's prefixed
        // with it
        !staticPaths.includes(
          `${locale ? '/' + locale : ''}${resolvedUrlPathname}`
        ))
J
Joe Haddad 已提交
1421
    ) {
1422 1423 1424 1425 1426
      if (
        // In development, fall through to render to handle missing
        // getStaticPaths.
        (isProduction || staticPaths) &&
        // When fallback isn't present, abort this render so we 404
1427
        fallbackMode !== 'static'
1428
      ) {
1429
        throw new NoFallbackError()
1430 1431
      }

1432 1433
      if (!isDataReq) {
        let html: string
1434

1435 1436
        // Production already emitted the fallback as static HTML.
        if (isProduction) {
1437 1438 1439
          html = await this.incrementalCache.getFallback(
            locale ? `/${locale}${pathname}` : pathname
          )
1440 1441 1442 1443 1444 1445 1446 1447 1448
        }
        // We need to generate the fallback on-demand for development.
        else {
          query.__nextFallback = 'true'
          if (isLikeServerless) {
            prepareServerlessUrl(req, query)
          }
          const { value: renderResult } = await doRender()
          html = renderResult.html
1449 1450
        }

1451 1452 1453 1454 1455 1456
        sendPayload(req, res, html, 'html', {
          generateEtags: this.renderOpts.generateEtags,
          poweredByHeader: this.renderOpts.poweredByHeader,
        })
        return null
      }
1457 1458
    }

1459 1460
    const {
      isOrigin,
1461
      value: { html, pageData, sprRevalidate, isNotFound },
1462
    } = await doRender()
1463
    let resHtml = html
1464 1465 1466 1467 1468 1469

    if (
      !isResSent(res) &&
      !isNotFound &&
      (isSSG || isDataReq || isServerProps)
    ) {
1470
      sendPayload(
1471
        req,
1472 1473
        res,
        isDataReq ? JSON.stringify(pageData) : html,
1474
        isDataReq ? 'json' : 'html',
1475 1476 1477 1478
        {
          generateEtags: this.renderOpts.generateEtags,
          poweredByHeader: this.renderOpts.poweredByHeader,
        },
1479
        !this.renderOpts.dev || (isServerProps && !isDataReq)
1480 1481
          ? {
              private: isPreviewMode,
1482
              stateful: !isSSG,
1483 1484
              revalidate: sprRevalidate,
            }
1485
          : undefined
1486
      )
1487
      resHtml = null
1488
    }
J
JJ Kasper 已提交
1489

1490
    // Update the cache if the head request and cacheable
1491
    if (isOrigin && ssgCacheKey) {
1492 1493
      await this.incrementalCache.set(
        ssgCacheKey,
1494
        { html: html!, pageData, isNotFound },
1495 1496
        sprRevalidate
      )
1497 1498
    }

1499 1500 1501
    if (isNotFound) {
      throw new NoFallbackError()
    }
1502
    return resHtml
1503 1504
  }

1505
  public async renderToHTML(
J
Joe Haddad 已提交
1506 1507 1508
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1509
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1510
  ): Promise<string | null> {
1511 1512 1513
    try {
      const result = await this.findPageComponents(pathname, query)
      if (result) {
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
        try {
          return await this.renderToHTMLWithComponents(
            req,
            res,
            pathname,
            result,
            { ...this.renderOpts }
          )
        } catch (err) {
          if (!(err instanceof NoFallbackError)) {
            throw err
          }
1526
        }
1527
      }
J
Joe Haddad 已提交
1528

1529 1530 1531 1532 1533 1534
      if (this.dynamicRoutes) {
        for (const dynamicRoute of this.dynamicRoutes) {
          const params = dynamicRoute.match(pathname)
          if (!params) {
            continue
          }
J
Joe Haddad 已提交
1535

1536
          const dynamicRouteResult = await this.findPageComponents(
1537 1538 1539 1540
            dynamicRoute.page,
            query,
            params
          )
1541
          if (dynamicRouteResult) {
1542 1543 1544 1545 1546
            try {
              return await this.renderToHTMLWithComponents(
                req,
                res,
                dynamicRoute.page,
1547
                dynamicRouteResult,
1548 1549 1550 1551 1552 1553
                { ...this.renderOpts, params }
              )
            } catch (err) {
              if (!(err instanceof NoFallbackError)) {
                throw err
              }
1554
            }
J
Joe Haddad 已提交
1555 1556
          }
        }
1557 1558 1559
      }
    } catch (err) {
      this.logError(err)
1560 1561 1562 1563 1564

      if (err && err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return await this.renderErrorToHTML(err, req, res, pathname, query)
      }
1565 1566 1567 1568 1569
      res.statusCode = 500
      return await this.renderErrorToHTML(err, req, res, pathname, query)
    }
    res.statusCode = 404
    return await this.renderErrorToHTML(null, req, res, pathname, query)
N
Naoyuki Kanezawa 已提交
1570 1571
  }

J
Joe Haddad 已提交
1572 1573 1574 1575 1576
  public async renderError(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1577
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1578 1579 1580
  ): Promise<void> {
    res.setHeader(
      'Cache-Control',
1581
      'no-cache, no-store, max-age=0, must-revalidate'
J
Joe Haddad 已提交
1582
    )
N
Naoyuki Kanezawa 已提交
1583
    const html = await this.renderErrorToHTML(err, req, res, pathname, query)
1584
    if (html === null) {
1585 1586
      return
    }
1587
    return this.sendHTML(req, res, html)
N
nkzawa 已提交
1588 1589
  }

1590 1591 1592 1593 1594 1595 1596 1597 1598
  private customErrorNo404Warn = execOnce(() => {
    console.warn(
      chalk.bold.yellow(`Warning: `) +
        chalk.yellow(
          `You have added a custom /_error page without a custom /404 page. This prevents the 404 page from being auto statically optimized.\nSee here for info: https://err.sh/next.js/custom-error-no-custom-404`
        )
    )
  })

J
Joe Haddad 已提交
1599 1600 1601 1602 1603
  public async renderErrorToHTML(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    _pathname: string,
1604
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1605
  ) {
1606
    let result: null | FindComponentsResult = null
1607

1608 1609 1610
    const is404 = res.statusCode === 404
    let using404Page = false

1611
    // use static 404 page if available and is 404 response
1612
    if (is404) {
1613
      result = await this.findPageComponents('/404', query)
1614
      using404Page = result !== null
1615 1616 1617 1618 1619 1620
    }

    if (!result) {
      result = await this.findPageComponents('/_error', query)
    }

1621 1622 1623
    if (
      process.env.NODE_ENV !== 'production' &&
      !using404Page &&
1624 1625
      (await this.hasPage('/_error')) &&
      !(await this.hasPage('/404'))
1626 1627 1628 1629
    ) {
      this.customErrorNo404Warn()
    }

1630
    let html: string | null
1631
    try {
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
      try {
        html = await this.renderToHTMLWithComponents(
          req,
          res,
          using404Page ? '/404' : '/_error',
          result!,
          {
            ...this.renderOpts,
            err,
          }
        )
1643 1644
      } catch (maybeFallbackError) {
        if (maybeFallbackError instanceof NoFallbackError) {
1645
          throw new Error('invariant: failed to render error page')
1646
        }
1647
        throw maybeFallbackError
1648
      }
1649 1650
    } catch (renderToHtmlError) {
      console.error(renderToHtmlError)
1651 1652 1653 1654
      res.statusCode = 500
      html = 'Internal Server Error'
    }
    return html
N
Naoyuki Kanezawa 已提交
1655 1656
  }

J
Joe Haddad 已提交
1657 1658 1659
  public async render404(
    req: IncomingMessage,
    res: ServerResponse,
1660
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1661
  ): Promise<void> {
1662 1663
    const url: any = req.url
    const { pathname, query } = parsedUrl ? parsedUrl : parseUrl(url, true)
N
Naoyuki Kanezawa 已提交
1664
    res.statusCode = 404
1665
    return this.renderError(null, req, res, pathname!, query)
N
Naoyuki Kanezawa 已提交
1666
  }
N
Naoyuki Kanezawa 已提交
1667

J
Joe Haddad 已提交
1668 1669 1670 1671
  public async serveStatic(
    req: IncomingMessage,
    res: ServerResponse,
    path: string,
1672
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1673
  ): Promise<void> {
A
Arunoda Susiripala 已提交
1674
    if (!this.isServeableUrl(path)) {
1675
      return this.render404(req, res, parsedUrl)
A
Arunoda Susiripala 已提交
1676 1677
    }

1678 1679 1680 1681 1682 1683
    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 已提交
1684
    try {
1685
      await serveStatic(req, res, path)
N
Naoyuki Kanezawa 已提交
1686
    } catch (err) {
T
Tim Neutkens 已提交
1687
      if (err.code === 'ENOENT' || err.statusCode === 404) {
1688
        this.render404(req, res, parsedUrl)
1689 1690 1691
      } else if (err.statusCode === 412) {
        res.statusCode = 412
        return this.renderError(err, req, res, path)
N
Naoyuki Kanezawa 已提交
1692 1693 1694 1695 1696 1697
      } else {
        throw err
      }
    }
  }

1698 1699 1700 1701 1702 1703 1704 1705 1706
  private _validFilesystemPathSet: Set<string> | null = null
  private getFilesystemPaths(): Set<string> {
    if (this._validFilesystemPathSet) {
      return this._validFilesystemPathSet
    }

    const pathUserFilesStatic = join(this.dir, 'static')
    let userFilesStatic: string[] = []
    if (this.hasStaticDir && fs.existsSync(pathUserFilesStatic)) {
J
Joe Haddad 已提交
1707
      userFilesStatic = recursiveReadDirSync(pathUserFilesStatic).map((f) =>
1708 1709 1710 1711 1712 1713
        join('.', 'static', f)
      )
    }

    let userFilesPublic: string[] = []
    if (this.publicDir && fs.existsSync(this.publicDir)) {
J
Joe Haddad 已提交
1714
      userFilesPublic = recursiveReadDirSync(this.publicDir).map((f) =>
1715 1716 1717 1718 1719 1720 1721
        join('.', 'public', f)
      )
    }

    let nextFilesStatic: string[] = []
    nextFilesStatic = recursiveReadDirSync(
      join(this.distDir, 'static')
J
Joe Haddad 已提交
1722
    ).map((f) => join('.', relative(this.dir, this.distDir), 'static', f))
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756

    return (this._validFilesystemPathSet = new Set<string>([
      ...nextFilesStatic,
      ...userFilesPublic,
      ...userFilesStatic,
    ]))
  }

  protected isServeableUrl(untrustedFileUrl: string): boolean {
    // This method mimics what the version of `send` we use does:
    // 1. decodeURIComponent:
    //    https://github.com/pillarjs/send/blob/0.17.1/index.js#L989
    //    https://github.com/pillarjs/send/blob/0.17.1/index.js#L518-L522
    // 2. resolve:
    //    https://github.com/pillarjs/send/blob/de073ed3237ade9ff71c61673a34474b30e5d45b/index.js#L561

    let decodedUntrustedFilePath: string
    try {
      // (1) Decode the URL so we have the proper file name
      decodedUntrustedFilePath = decodeURIComponent(untrustedFileUrl)
    } catch {
      return false
    }

    // (2) Resolve "up paths" to determine real request
    const untrustedFilePath = resolve(decodedUntrustedFilePath)

    // don't allow null bytes anywhere in the file path
    if (untrustedFilePath.indexOf('\0') !== -1) {
      return false
    }

    // Check if .next/static, static and public are in the path.
    // If not the path is not available.
A
Arunoda Susiripala 已提交
1757
    if (
1758 1759 1760
      (untrustedFilePath.startsWith(join(this.distDir, 'static') + sep) ||
        untrustedFilePath.startsWith(join(this.dir, 'static') + sep) ||
        untrustedFilePath.startsWith(join(this.dir, 'public') + sep)) === false
A
Arunoda Susiripala 已提交
1761 1762 1763 1764
    ) {
      return false
    }

1765 1766 1767 1768
    // Check against the real filesystem paths
    const filesystemUrls = this.getFilesystemPaths()
    const resolved = relative(this.dir, untrustedFilePath)
    return filesystemUrls.has(resolved)
A
Arunoda Susiripala 已提交
1769 1770
  }

1771
  protected readBuildId(): string {
1772 1773 1774 1775 1776
    const buildIdFile = join(this.distDir, BUILD_ID_FILE)
    try {
      return fs.readFileSync(buildIdFile, 'utf8').trim()
    } catch (err) {
      if (!fs.existsSync(buildIdFile)) {
J
Joe Haddad 已提交
1777
        throw new Error(
1778
          `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 已提交
1779
        )
1780 1781 1782
      }

      throw err
1783
    }
1784
  }
1785

1786
  protected get _isLikeServerless(): boolean {
1787 1788
    return isTargetLikeServerless(this.nextConfig.target)
  }
1789
}
1790

1791 1792 1793 1794
function prepareServerlessUrl(
  req: IncomingMessage,
  query: ParsedUrlQuery
): void {
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
  const curUrl = parseUrl(req.url!, true)
  req.url = formatUrl({
    ...curUrl,
    search: undefined,
    query: {
      ...curUrl.query,
      ...query,
    },
  })
}
1805 1806

class NoFallbackError extends Error {}