next-server.ts 54.1 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 '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,
29
  PERMANENT_REDIRECT_STATUS,
J
Joe Haddad 已提交
30
  PHASE_PRODUCTION_SERVER,
J
Joe Haddad 已提交
31
  PRERENDER_MANIFEST,
32
  ROUTES_MANIFEST,
33
  SERVERLESS_DIRECTORY,
J
Joe Haddad 已提交
34
  SERVER_DIRECTORY,
T
Tim Neutkens 已提交
35
} from '../lib/constants'
J
Joe Haddad 已提交
36 37 38 39
import {
  getRouteMatcher,
  getRouteRegex,
  getSortedRoutes,
40
  isDynamicRoute,
J
Joe Haddad 已提交
41
} from '../lib/router/utils'
42
import * as envConfig from '../lib/runtime-config'
J
Joe Haddad 已提交
43
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
44 45 46 47 48 49 50
import {
  apiResolver,
  setLazyProp,
  getCookieParser,
  tryGetPreviewData,
  __ApiPreviewProps,
} from './api-utils'
51
import loadConfig, { isTargetLikeServerless } from './config'
52
import pathMatch from '../lib/router/utils/path-match'
J
Joe Haddad 已提交
53
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
54
import { loadComponents, LoadComponentsReturnType } from './load-components'
J
Joe Haddad 已提交
55
import { normalizePagePath } from './normalize-page-path'
56
import { RenderOpts, RenderOptsPartial, renderToHTML } from './render'
P
Prateek Bhatnagar 已提交
57
import { getPagePath, requireFontManifest } from './require'
58 59 60
import Router, {
  DynamicRoutes,
  PageChecker,
J
Joe Haddad 已提交
61 62 63
  Params,
  route,
  Route,
64
} from './router'
65 66 67
import prepareDestination, {
  compileNonPath,
} from '../lib/router/utils/prepare-destination'
68
import { sendPayload } from './send-payload'
J
Joe Haddad 已提交
69
import { serveStatic } from './serve-static'
70
import { IncrementalCache } from './incremental-cache'
71
import { execOnce } from '../lib/utils'
72
import { isBlockedPage } from './utils'
73
import { loadEnvConfig } from '@next/env'
74
import './node-polyfill-fetch'
J
Jan Potoms 已提交
75
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
76
import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
77
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
P
Prateek Bhatnagar 已提交
78
import { FontManifest } from './font-utils'
79
import { denormalizePagePath } from './denormalize-page-path'
80 81 82
import accept from '@hapi/accept'
import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie'
83
import * as Log from '../../build/output/log'
S
Steven 已提交
84
import { imageOptimizer } from './image-optimizer'
85
import { detectDomainLocale } from '../lib/i18n/detect-domain-locale'
86
import cookie from 'next/dist/compiled/cookie'
J
JJ Kasper 已提交
87 88

const getCustomRouteMatcher = pathMatch(true)
89 90 91

type NextConfig = any

92 93 94 95 96 97
type Middleware = (
  req: IncomingMessage,
  res: ServerResponse,
  next: (err?: Error) => void
) => void

98 99 100 101 102
type FindComponentsResult = {
  components: LoadComponentsReturnType
  query: ParsedUrlQuery
}

103 104 105 106 107
type DynamicRouteItem = {
  page: string
  match: ReturnType<typeof getRouteMatcher>
}

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

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

J
Joe Haddad 已提交
163 164 165 166
  public constructor({
    dir = '.',
    quiet = false,
    conf = null,
J
JJ Kasper 已提交
167
    dev = false,
168
    customServer = true,
J
Joe Haddad 已提交
169
  }: ServerConstructor = {}) {
N
nkzawa 已提交
170
    this.dir = resolve(dir)
N
Naoyuki Kanezawa 已提交
171
    this.quiet = quiet
T
Tim Neutkens 已提交
172
    const phase = this.currentPhase()
173
    loadEnvConfig(this.dir, dev, Log)
174

175
    this.nextConfig = loadConfig(phase, this.dir, conf)
176
    this.distDir = join(this.dir, this.nextConfig.distDir)
177
    this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
178
    this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
T
Tim Neutkens 已提交
179

180 181
    // Only serverRuntimeConfig needs the default
    // publicRuntimeConfig gets it's default in client/index.js
J
Joe Haddad 已提交
182 183 184 185 186
    const {
      serverRuntimeConfig = {},
      publicRuntimeConfig,
      assetPrefix,
      generateEtags,
187
      compress,
J
Joe Haddad 已提交
188
    } = this.nextConfig
189

T
Tim Neutkens 已提交
190
    this.buildId = this.readBuildId()
191

192
    this.renderOpts = {
T
Tim Neutkens 已提交
193
      poweredByHeader: this.nextConfig.poweredByHeader,
194
      canonicalBase: this.nextConfig.amp.canonicalBase,
195
      buildId: this.buildId,
196
      generateEtags,
197
      previewProps: this.getPreviewProps(),
198
      customServer: customServer === true ? true : undefined,
199
      ampOptimizerConfig: this.nextConfig.experimental.amp?.optimizer,
200
      basePath: this.nextConfig.basePath,
A
Alex Castle 已提交
201
      images: JSON.stringify(this.nextConfig.images),
202 203 204 205 206
      optimizeFonts: this.nextConfig.experimental.optimizeFonts && !dev,
      fontManifest:
        this.nextConfig.experimental.optimizeFonts && !dev
          ? requireFontManifest(this.distDir, this._isLikeServerless)
          : null,
207
      optimizeImages: this.nextConfig.experimental.optimizeImages,
J
Joe Haddad 已提交
208
      defaultLocale: this.nextConfig.i18n?.defaultLocale,
209
    }
N
Naoyuki Kanezawa 已提交
210

211 212
    // Only the `publicRuntimeConfig` key is exposed to the client side
    // It'll be rendered as part of __NEXT_DATA__ on the client side
213
    if (Object.keys(publicRuntimeConfig).length > 0) {
214
      this.renderOpts.runtimeConfig = publicRuntimeConfig
215 216
    }

217
    if (compress && this.nextConfig.target === 'server') {
218 219 220
      this.compression = compression() as Middleware
    }

221
    // Initialize next/config with the environment configuration
222 223 224 225
    envConfig.setConfig({
      serverRuntimeConfig,
      publicRuntimeConfig,
    })
226

227 228 229 230 231 232 233 234 235 236
    this.serverBuildDir = join(
      this.distDir,
      this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
    )
    const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)

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

237
    this.customRoutes = this.getCustomRoutes()
J
JJ Kasper 已提交
238
    this.router = new Router(this.generateRoutes())
239
    this.setAssetPrefix(assetPrefix)
J
JJ Kasper 已提交
240

241 242 243
    // call init-server middleware, this is also handled
    // individually in serverless bundles when deployed
    if (!dev && this.nextConfig.experimental.plugins) {
244 245
      const initServer = require(join(this.serverBuildDir, 'init-server.js'))
        .default
246
      this.onErrorMiddleware = require(join(
247
        this.serverBuildDir,
248 249 250 251 252
        'on-error-server.js'
      )).default
      initServer()
    }

253
    this.incrementalCache = new IncrementalCache({
J
JJ Kasper 已提交
254 255 256 257
      dev,
      distDir: this.distDir,
      pagesDir: join(
        this.distDir,
258
        this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
J
JJ Kasper 已提交
259 260 261 262
        'pages'
      ),
      flushToDisk: this.nextConfig.experimental.sprFlushToDisk,
    })
P
Prateek Bhatnagar 已提交
263 264 265 266 267 268 269 270 271 272

    /**
     * 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)
    }
273 274 275
    if (this.renderOpts.optimizeImages) {
      process.env.__NEXT_OPTIMIZE_IMAGES = JSON.stringify(true)
    }
N
Naoyuki Kanezawa 已提交
276
  }
N
nkzawa 已提交
277

278
  protected currentPhase(): string {
279
    return PHASE_PRODUCTION_SERVER
280 281
  }

S
Steven 已提交
282
  public logError(err: Error): void {
283 284 285
    if (this.onErrorMiddleware) {
      this.onErrorMiddleware({ err })
    }
286
    if (this.quiet) return
287
    console.error(err)
288 289
  }

290
  private async handleRequest(
J
Joe Haddad 已提交
291 292
    req: IncomingMessage,
    res: ServerResponse,
293
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
294
  ): Promise<void> {
295 296
    setLazyProp({ req: req as any }, 'cookies', getCookieParser(req))

297
    // Parse url if parsedUrl not provided
298
    if (!parsedUrl || typeof parsedUrl !== 'object') {
299 300
      const url: any = req.url
      parsedUrl = parseUrl(url, true)
301
    }
302

303 304 305
    // 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 已提交
306
    }
307
    ;(req as any).__NEXT_INIT_QUERY = Object.assign({}, parsedUrl.query)
308

J
Joe Haddad 已提交
309
    const { basePath, i18n } = this.nextConfig
310

311 312 313 314 315
    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 已提交
316 317
    }

318
    if (i18n && !parsedUrl.pathname?.startsWith('/_next')) {
319 320
      // get pathname from URL with basePath stripped for locale detection
      const { pathname, ...parsed } = parseUrl(req.url || '/')
321
      let defaultLocale = i18n.defaultLocale
322
      let detectedLocale = detectLocaleCookie(req, i18n.locales)
323 324 325 326
      let acceptPreferredLocale =
        i18n.localeDetection !== false
          ? accept.language(req.headers['accept-language'], i18n.locales)
          : detectedLocale
327

328 329 330 331 332
      const { host } = req?.headers || {}
      // remove port from host and remove port if present
      const hostname = host?.split(':')[0].toLowerCase()

      const detectedDomain = detectDomainLocale(i18n.domains, hostname)
333 334 335 336
      if (detectedDomain) {
        defaultLocale = detectedDomain.defaultLocale
        detectedLocale = defaultLocale
      }
337

338 339
      // if not domain specific locale use accept-language preferred
      detectedLocale = detectedLocale || acceptPreferredLocale
340

341 342 343 344 345 346 347 348 349
      let localeDomainRedirect: string | undefined
      const localePathResult = normalizeLocalePath(pathname!, i18n.locales)

      if (localePathResult.detectedLocale) {
        detectedLocale = localePathResult.detectedLocale
        req.url = formatUrl({
          ...parsed,
          pathname: localePathResult.pathname,
        })
J
JJ Kasper 已提交
350
        ;(req as any).__nextStrippedLocale = true
351
        parsedUrl.pathname = localePathResult.pathname
352 353 354 355 356 357 358 359 360 361
      }

      // If a detected locale is a domain specific locale and we aren't already
      // on that domain and path prefix redirect to it to prevent duplicate
      // content from multiple domains
      if (detectedDomain && parsedUrl.pathname === '/') {
        const localeToCheck = acceptPreferredLocale
        // const localeToCheck = localePathResult.detectedLocale
        //   ? detectedLocale
        //   : acceptPreferredLocale
362

363 364 365
        const matchedDomain = detectDomainLocale(
          i18n.domains,
          undefined,
366
          localeToCheck
367 368
        )

369 370 371 372 373
        if (
          matchedDomain &&
          (matchedDomain.domain !== detectedDomain.domain ||
            localeToCheck !== matchedDomain.defaultLocale)
        ) {
374 375
          localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
            matchedDomain.domain
376 377
          }/${
            localeToCheck === matchedDomain.defaultLocale ? '' : localeToCheck
378
          }`
379 380 381
        }
      }

382
      const denormalizedPagePath = denormalizePagePath(pathname || '/')
383
      const detectedDefaultLocale =
384 385
        !detectedLocale ||
        detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
386 387 388 389
      const shouldStripDefaultLocale = false
      // detectedDefaultLocale &&
      // denormalizedPagePath.toLowerCase() ===
      //   `/${i18n.defaultLocale.toLowerCase()}`
390

391 392
      const shouldAddLocalePrefix =
        !detectedDefaultLocale && denormalizedPagePath === '/'
393

394
      detectedLocale = detectedLocale || i18n.defaultLocale
395

396 397
      if (
        i18n.localeDetection !== false &&
398 399 400
        (localeDomainRedirect ||
          shouldAddLocalePrefix ||
          shouldStripDefaultLocale)
401
      ) {
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        // 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: '/',
            }),
          ])
        }

424 425 426 427 428
        res.setHeader(
          'Location',
          formatUrl({
            // make sure to include any query values when redirecting
            ...parsed,
429 430 431 432 433
            pathname: localeDomainRedirect
              ? localeDomainRedirect
              : shouldStripDefaultLocale
              ? '/'
              : `/${detectedLocale}`,
434 435 436 437
          })
        )
        res.statusCode = 307
        res.end()
438
        return
439
      }
440 441 442 443 444

      parsedUrl.query.__nextLocale =
        localePathResult.detectedLocale ||
        detectedDomain?.defaultLocale ||
        defaultLocale
445 446
    }

447
    res.statusCode = 200
448 449 450
    try {
      return await this.run(req, res, parsedUrl)
    } catch (err) {
J
Joe Haddad 已提交
451 452 453
      this.logError(err)
      res.statusCode = 500
      res.end('Internal Server Error')
454
    }
455 456
  }

457
  public getRequestHandler() {
458
    return this.handleRequest.bind(this)
N
nkzawa 已提交
459 460
  }

461
  public setAssetPrefix(prefix?: string): void {
462
    this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
463 464
  }

465
  // Backwards compatibility
466
  public async prepare(): Promise<void> {}
N
nkzawa 已提交
467

T
Tim Neutkens 已提交
468
  // Backwards compatibility
469
  protected async close(): Promise<void> {}
T
Tim Neutkens 已提交
470

471
  protected setImmutableAssetCacheControl(res: ServerResponse): void {
T
Tim Neutkens 已提交
472
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
N
nkzawa 已提交
473 474
  }

475
  protected getCustomRoutes(): CustomRoutes {
J
JJ Kasper 已提交
476 477 478
    return require(join(this.distDir, ROUTES_MANIFEST))
  }

479 480 481 482
  private _cachedPreviewManifest: PrerenderManifest | undefined
  protected getPrerenderManifest(): PrerenderManifest {
    if (this._cachedPreviewManifest) {
      return this._cachedPreviewManifest
J
Joe Haddad 已提交
483
    }
484 485 486 487 488 489
    const manifest = require(join(this.distDir, PRERENDER_MANIFEST))
    return (this._cachedPreviewManifest = manifest)
  }

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

492
  protected generateRoutes(): {
493
    basePath: string
494 495
    headers: Route[]
    rewrites: Route[]
496
    fsRoutes: Route[]
497
    redirects: Route[]
498 499
    catchAllRoute: Route
    pageChecker: PageChecker
500
    useFileSystemPublicRoutes: boolean
501 502
    dynamicRoutes: DynamicRoutes | undefined
  } {
S
Steven 已提交
503
    const server: Server = this
504 505 506
    const publicRoutes = fs.existsSync(this.publicDir)
      ? this.generatePublicRoutes()
      : []
J
JJ Kasper 已提交
507

508
    const staticFilesRoute = this.hasStaticDir
509 510 511 512 513
      ? [
          {
            // 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.
514
            // See more: https://github.com/vercel/next.js/issues/2617
515
            match: route('/static/:path*'),
516
            name: 'static catchall',
517
            fn: async (req, res, params, parsedUrl) => {
518
              const p = join(this.dir, 'static', ...params.path)
519
              await this.serveStatic(req, res, p, parsedUrl)
520 521 522
              return {
                finished: true,
              }
523 524 525 526
            },
          } as Route,
        ]
      : []
527

528
    const fsRoutes: Route[] = [
T
Tim Neutkens 已提交
529
      {
530
        match: route('/_next/static/:path*'),
531 532
        type: 'route',
        name: '_next/static catchall',
533
        fn: async (req, res, params, parsedUrl) => {
534
          // make sure to 404 for /_next/static itself
535 536 537 538 539 540
          if (!params.path) {
            await this.render404(req, res, parsedUrl)
            return {
              finished: true,
            }
          }
541

J
Joe Haddad 已提交
542 543 544
          if (
            params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
            params.path[0] === 'chunks' ||
545 546
            params.path[0] === 'css' ||
            params.path[0] === 'media' ||
547
            params.path[0] === this.buildId ||
548
            params.path[0] === 'pages' ||
549
            params.path[1] === 'pages'
J
Joe Haddad 已提交
550
          ) {
T
Tim Neutkens 已提交
551
            this.setImmutableAssetCacheControl(res)
552
          }
J
Joe Haddad 已提交
553 554 555
          const p = join(
            this.distDir,
            CLIENT_STATIC_FILES_PATH,
556
            ...(params.path || [])
J
Joe Haddad 已提交
557
          )
558
          await this.serveStatic(req, res, p, parsedUrl)
559 560 561
          return {
            finished: true,
          }
562
        },
563
      },
J
JJ Kasper 已提交
564 565
      {
        match: route('/_next/data/:path*'),
566 567
        type: 'route',
        name: '_next/data catchall',
J
JJ Kasper 已提交
568
        fn: async (req, res, params, _parsedUrl) => {
J
JJ Kasper 已提交
569 570 571
          // 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) {
572 573 574 575
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
576 577 578 579 580 581
          }
          // 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')) {
582 583 584 585
            await this.render404(req, res, _parsedUrl)
            return {
              finished: true,
            }
J
JJ Kasper 已提交
586 587 588
          }

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

J
Joe Haddad 已提交
591
          const { i18n } = this.nextConfig
592 593

          if (i18n) {
594 595 596
            const { host } = req?.headers || {}
            // remove port from host and remove port if present
            const hostname = host?.split(':')[0].toLowerCase()
597
            const localePathResult = normalizeLocalePath(pathname, i18n.locales)
598
            const { defaultLocale } =
599
              detectDomainLocale(i18n.domains, hostname) || {}
600
            let detectedLocale = defaultLocale
601 602 603 604 605

            if (localePathResult.detectedLocale) {
              pathname = localePathResult.pathname
              detectedLocale = localePathResult.detectedLocale
            }
606
            _parsedUrl.query.__nextLocale = detectedLocale!
607 608
          }
          pathname = getRouteFromAssetPath(pathname, '.json')
J
JJ Kasper 已提交
609

J
JJ Kasper 已提交
610
          const parsedUrl = parseUrl(pathname, true)
611

J
JJ Kasper 已提交
612 613 614 615
          await this.render(
            req,
            res,
            pathname,
616
            { ..._parsedUrl.query, _nextDataReq: '1' },
J
JJ Kasper 已提交
617 618
            parsedUrl
          )
619 620 621
          return {
            finished: true,
          }
J
JJ Kasper 已提交
622 623
        },
      },
S
Steven 已提交
624 625 626 627 628 629 630
      {
        match: route('/_next/image'),
        type: 'route',
        name: '_next/image catchall',
        fn: (req, res, _params, parsedUrl) =>
          imageOptimizer(server, req, res, parsedUrl),
      },
T
Tim Neutkens 已提交
631
      {
632
        match: route('/_next/:path*'),
633 634
        type: 'route',
        name: '_next catchall',
T
Tim Neutkens 已提交
635
        // This path is needed because `render()` does a check for `/_next` and the calls the routing again
636
        fn: async (req, res, _params, parsedUrl) => {
T
Tim Neutkens 已提交
637
          await this.render404(req, res, parsedUrl)
638 639 640
          return {
            finished: true,
          }
L
Lukáš Huvar 已提交
641 642
        },
      },
643 644
      ...publicRoutes,
      ...staticFilesRoute,
T
Tim Neutkens 已提交
645
    ]
646

647 648 649 650 651 652
    const getCustomRouteBasePath = (r: { basePath?: false }) => {
      return r.basePath !== false && this.renderOpts.dev
        ? this.nextConfig.basePath
        : ''
    }

653 654 655 656
    const getCustomRoute = (r: Rewrite | Redirect | Header, type: RouteType) =>
      ({
        ...r,
        type,
657
        match: getCustomRouteMatcher(`${getCustomRouteBasePath(r)}${r.source}`),
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
        name: type,
        fn: async (_req, _res, _params, _parsedUrl) => ({ finished: false }),
      } as Route & Rewrite & Header)

    // 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) {
675 676
              key = compileNonPath(key, params)
              value = compileNonPath(value, params)
677
            }
678 679 680 681 682 683 684
            res.setHeader(key, value)
          }
          return { finished: false }
        },
      } as Route
    })

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    // since initial query values are decoded by querystring.parse
    // we need to re-encode them here but still allow passing through
    // values from rewrites/redirects
    const stringifyQuery = (req: IncomingMessage, query: ParsedUrlQuery) => {
      const initialQueryValues = Object.values((req as any).__NEXT_INIT_QUERY)

      return stringifyQs(query, undefined, undefined, {
        encodeURIComponent(value) {
          if (initialQueryValues.some((val) => val === value)) {
            return encodeURIComponent(value)
          }
          return value
        },
      })
    }

701 702 703 704 705 706 707
    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`,
708
        fn: async (req, res, params, parsedUrl) => {
709 710 711
          const { parsedDestination } = prepareDestination(
            redirectRoute.destination,
            params,
712 713 714
            parsedUrl.query,
            false,
            getCustomRouteBasePath(redirectRoute)
715
          )
716 717

          const { query } = parsedDestination
718
          delete (parsedDestination as any).query
719

720
          parsedDestination.search = stringifyQuery(req, query)
721

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
          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 {
744
        ...rewriteRoute,
745 746 747 748 749 750 751 752 753
        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,
754 755
            true,
            getCustomRouteBasePath(rewriteRoute)
756
          )
757

758 759
          // external rewrite, proxy it
          if (parsedDestination.protocol) {
760
            const { query } = parsedDestination
761
            delete (parsedDestination as any).query
762
            parsedDestination.search = stringifyQuery(req, query)
763

764 765 766 767 768 769 770 771 772 773 774
            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)
            })
775 776 777
            return {
              finished: true,
            }
778 779
          }
          ;(req as any)._nextRewroteUrl = newUrl
780 781
          ;(req as any)._nextDidRewrite =
            (req as any)._nextRewroteUrl !== req.url
782

783 784 785 786 787 788 789 790
          return {
            finished: false,
            pathname: newUrl,
            query: parsedDestination.query,
          }
        },
      } as Route
    })
791 792 793 794 795 796

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

J
Jan Potoms 已提交
802
        // next.js core assumes page path without trailing slash
803
        pathname = removePathTrailingSlash(pathname)
J
Jan Potoms 已提交
804

805
        if (params?.path?.[0] === 'api') {
806 807 808
          const handled = await this.handleApiRequest(
            req as NextApiRequest,
            res as NextApiResponse,
809
            pathname,
810
            query
811 812 813 814 815 816 817
          )
          if (handled) {
            return { finished: true }
          }
        }

        await this.render(req, res, pathname, query, parsedUrl)
818 819 820 821
        return {
          finished: true,
        }
      },
822
    }
823

824
    const { useFileSystemPublicRoutes } = this.nextConfig
J
Joe Haddad 已提交
825

826 827
    if (useFileSystemPublicRoutes) {
      this.dynamicRoutes = this.getDynamicRoutes()
828
    }
N
nkzawa 已提交
829

830
    return {
831
      headers,
832
      fsRoutes,
833 834
      rewrites,
      redirects,
835
      catchAllRoute,
836
      useFileSystemPublicRoutes,
837
      dynamicRoutes: this.dynamicRoutes,
838
      basePath: this.nextConfig.basePath,
839 840
      pageChecker: this.hasPage.bind(this),
    }
T
Tim Neutkens 已提交
841 842
  }

843
  private async getPagePath(pathname: string): Promise<string> {
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    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
  }

861 862 863 864 865
  protected async _beforeCatchAllRender(
    _req: IncomingMessage,
    _res: ServerResponse,
    _params: Params,
    _parsedUrl: UrlWithParsedQuery
866
  ): Promise<boolean> {
867 868 869
    return false
  }

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

L
Lukáš Huvar 已提交
873 874 875 876 877 878
  /**
   * Resolves `API` request, in development builds on demand
   * @param req http request
   * @param res http response
   * @param pathname path of request
   */
J
Joe Haddad 已提交
879
  private async handleApiRequest(
880 881
    req: IncomingMessage,
    res: ServerResponse,
882 883
    pathname: string,
    query: ParsedUrlQuery
884
  ): Promise<boolean> {
885
    let page = pathname
L
Lukáš Huvar 已提交
886
    let params: Params | boolean = false
887
    let pageFound = await this.hasPage(page)
J
JJ Kasper 已提交
888

889
    if (!pageFound && this.dynamicRoutes) {
L
Lukáš Huvar 已提交
890 891
      for (const dynamicRoute of this.dynamicRoutes) {
        params = dynamicRoute.match(pathname)
892
        if (dynamicRoute.page.startsWith('/api') && params) {
893 894
          page = dynamicRoute.page
          pageFound = true
L
Lukáš Huvar 已提交
895 896 897 898 899
          break
        }
      }
    }

900
    if (!pageFound) {
901
      return false
J
JJ Kasper 已提交
902
    }
903 904 905 906
    // Make sure the page is built before getting the path
    // or else it won't be in the manifest yet
    await this.ensureApiPage(page)

907 908 909 910 911 912 913 914 915 916
    let builtPagePath
    try {
      builtPagePath = await this.getPagePath(page)
    } catch (err) {
      if (err.code === 'ENOENT') {
        return false
      }
      throw err
    }

917
    const pageModule = await require(builtPagePath)
918
    query = { ...query, ...params }
J
JJ Kasper 已提交
919

920
    if (!this.renderOpts.dev && this._isLikeServerless) {
921
      if (typeof pageModule.default === 'function') {
922
        prepareServerlessUrl(req, query)
923 924
        await pageModule.default(req, res)
        return true
J
JJ Kasper 已提交
925 926 927
      }
    }

J
Joe Haddad 已提交
928 929 930 931 932
    await apiResolver(
      req,
      res,
      query,
      pageModule,
933
      this.renderOpts.previewProps,
934
      false,
J
Joe Haddad 已提交
935 936
      this.onErrorMiddleware
    )
937
    return true
L
Lukáš Huvar 已提交
938 939
  }

940
  protected generatePublicRoutes(): Route[] {
941
    const publicFiles = new Set(
942 943 944
      recursiveReadDirSync(this.publicDir).map((p) =>
        encodeURI(p.replace(/\\/g, '/'))
      )
945 946 947 948 949 950 951
    )

    return [
      {
        match: route('/:path*'),
        name: 'public folder catchall',
        fn: async (req, res, params, parsedUrl) => {
952
          const pathParts: string[] = params.path || []
953 954 955 956
          const { basePath } = this.nextConfig

          // if basePath is defined require it be present
          if (basePath) {
957 958 959 960 961 962 963 964 965 966 967 968 969
            const basePathParts = basePath.split('/')
            // remove first empty value
            basePathParts.shift()

            if (
              !basePathParts.every((part: string, idx: number) => {
                return part === pathParts[idx]
              })
            ) {
              return { finished: false }
            }

            pathParts.splice(0, basePathParts.length)
970 971
          }

972
          const path = `/${pathParts.join('/')}`
973 974 975 976 977

          if (publicFiles.has(path)) {
            await this.serveStatic(
              req,
              res,
978
              join(this.publicDir, ...pathParts),
979 980
              parsedUrl
            )
981 982 983
            return {
              finished: true,
            }
984 985 986 987 988 989 990
          }
          return {
            finished: false,
          }
        },
      } as Route,
    ]
991 992
  }

993 994 995
  protected getDynamicRoutes(): Array<DynamicRouteItem> {
    const addedPages = new Set<string>()

996 997
    return getSortedRoutes(Object.keys(this.pagesManifest!))
      .filter(isDynamicRoute)
998 999 1000 1001 1002 1003 1004 1005 1006 1007
      .map((page) => {
        page = normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname
        if (addedPages.has(page)) return null
        addedPages.add(page)
        return {
          page,
          match: getRouteMatcher(getRouteRegex(page)),
        }
      })
      .filter((item): item is DynamicRouteItem => Boolean(item))
J
Joe Haddad 已提交
1008 1009
  }

1010
  private handleCompression(req: IncomingMessage, res: ServerResponse): void {
1011 1012 1013 1014 1015
    if (this.compression) {
      this.compression(req, res, () => {})
    }
  }

1016
  protected async run(
J
Joe Haddad 已提交
1017 1018
    req: IncomingMessage,
    res: ServerResponse,
1019
    parsedUrl: UrlWithParsedQuery
1020
  ): Promise<void> {
1021 1022
    this.handleCompression(req, res)

1023
    try {
1024 1025
      const matched = await this.router.execute(req, res, parsedUrl)
      if (matched) {
1026 1027 1028 1029 1030 1031 1032 1033
        return
      }
    } catch (err) {
      if (err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return this.renderError(null, req, res, '/_error', {})
      }
      throw err
1034 1035
    }

1036
    await this.render404(req, res, parsedUrl)
N
nkzawa 已提交
1037 1038
  }

1039
  protected async sendHTML(
J
Joe Haddad 已提交
1040 1041
    req: IncomingMessage,
    res: ServerResponse,
1042
    html: string
1043
  ): Promise<void> {
T
Tim Neutkens 已提交
1044
    const { generateEtags, poweredByHeader } = this.renderOpts
1045 1046 1047 1048
    return sendPayload(req, res, html, 'html', {
      generateEtags,
      poweredByHeader,
    })
1049 1050
  }

J
Joe Haddad 已提交
1051 1052 1053 1054 1055
  public async render(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
1056
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1057
  ): Promise<void> {
1058 1059 1060 1061 1062 1063
    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`
      )
    }

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    if (
      this.renderOpts.customServer &&
      pathname === '/index' &&
      !(await this.hasPage('/index'))
    ) {
      // maintain backwards compatibility for custom server
      // (see custom-server integration tests)
      pathname = '/'
    }

1074
    const url: any = req.url
1075

1076 1077 1078 1079
    // 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
1080
    if (
1081 1082 1083
      !query._nextDataReq &&
      (url.match(/^\/_next\//) ||
        (this.hasStaticDir && url.match(/^\/static\//)))
1084
    ) {
1085 1086 1087
      return this.handleRequest(req, res, parsedUrl)
    }

1088
    if (isBlockedPage(pathname)) {
1089
      return this.render404(req, res, parsedUrl)
1090 1091
    }

1092
    const html = await this.renderToHTML(req, res, pathname, query)
1093 1094
    // Request was ended by the user
    if (html === null) {
1095 1096 1097
      return
    }

1098
    return this.sendHTML(req, res, html)
N
Naoyuki Kanezawa 已提交
1099
  }
N
nkzawa 已提交
1100

J
Joe Haddad 已提交
1101
  private async findPageComponents(
J
Joe Haddad 已提交
1102
    pathname: string,
1103 1104 1105
    query: ParsedUrlQuery = {},
    params: Params | null = null
  ): Promise<FindComponentsResult | null> {
1106
    let paths = [
1107 1108 1109 1110
      // try serving a static AMP version first
      query.amp ? normalizePagePath(pathname) + '.amp' : null,
      pathname,
    ].filter(Boolean)
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

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

1121
    for (const pagePath of paths) {
J
JJ Kasper 已提交
1122
      try {
1123
        const components = await loadComponents(
J
Joe Haddad 已提交
1124
          this.distDir,
1125 1126
          pagePath!,
          !this.renderOpts.dev && this._isLikeServerless
J
Joe Haddad 已提交
1127
        )
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
        // 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
        }

1140 1141 1142
        return {
          components,
          query: {
1143
            ...(components.getStaticProps
1144 1145 1146 1147 1148
              ? {
                  amp: query.amp,
                  _nextDataReq: query._nextDataReq,
                  __nextLocale: query.__nextLocale,
                }
1149 1150 1151 1152
              : query),
            ...(params || {}),
          },
        }
J
JJ Kasper 已提交
1153 1154 1155 1156
      } catch (err) {
        if (err.code !== 'ENOENT') throw err
      }
    }
1157
    return null
J
Joe Haddad 已提交
1158 1159
  }

1160
  protected async getStaticPaths(
1161 1162 1163
    pathname: string
  ): Promise<{
    staticPaths: string[] | undefined
1164
    fallbackMode: 'static' | 'blocking' | false
1165
  }> {
1166 1167 1168 1169 1170
    // `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.
1171 1172
    const fallbackField = this.getPrerenderManifest().dynamicRoutes[pathname]
      .fallback
1173

1174 1175 1176 1177 1178 1179 1180 1181 1182
    return {
      staticPaths,
      fallbackMode:
        typeof fallbackField === 'string'
          ? 'static'
          : fallbackField === null
          ? 'blocking'
          : false,
    }
1183 1184
  }

J
Joe Haddad 已提交
1185 1186 1187 1188
  private async renderToHTMLWithComponents(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1189
    { components, query }: FindComponentsResult,
1190
    opts: RenderOptsPartial
1191
  ): Promise<string | null> {
1192 1193
    const is404Page = pathname === '/404'

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    const isLikeServerless =
      typeof components.Component === 'object' &&
      typeof (components.Component as any).renderReqToHTML === 'function'
    const isSSG = !!components.getStaticProps
    const isServerProps = !!components.getServerSideProps
    const hasStaticPaths = !!components.getStaticPaths

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

1205
    // we need to ensure the status code if /404 is visited directly
1206
    if (is404Page && !isDataReq) {
1207 1208 1209
      res.statusCode = 404
    }

J
JJ Kasper 已提交
1210
    // handle static page
1211 1212
    if (typeof components.Component === 'string') {
      return components.Component
J
Joe Haddad 已提交
1213 1214
    }

1215 1216 1217 1218
    if (!query.amp) {
      delete query.amp
    }

J
JJ Kasper 已提交
1219 1220
    const locale = query.__nextLocale as string
    delete query.__nextLocale
1221

J
Joe Haddad 已提交
1222
    const { i18n } = this.nextConfig
1223
    const locales = i18n.locales as string[]
J
JJ Kasper 已提交
1224

1225 1226 1227 1228 1229 1230 1231 1232
    let previewData: string | false | object | undefined
    let isPreviewMode = false

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

1233 1234 1235
    // 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
1236 1237 1238
    let urlPathname = parseUrl(req.url || '').pathname || '/'

    let resolvedUrlPathname = (req as any)._nextRewroteUrl
1239
      ? (req as any)._nextRewroteUrl
1240
      : urlPathname
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250
    resolvedUrlPathname = removePathTrailingSlash(resolvedUrlPathname)
    urlPathname = removePathTrailingSlash(urlPathname)

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

J
Joe Haddad 已提交
1252
      if (this.nextConfig.i18n) {
J
JJ Kasper 已提交
1253
        return normalizeLocalePath(path, locales).pathname
1254
      }
1255 1256
      return path
    }
1257

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    const handleRedirect = (pageData: any) => {
      const redirect = {
        destination: pageData.pageProps.__N_REDIRECT,
        statusCode: pageData.pageProps.__N_REDIRECT_STATUS,
      }
      const statusCode = getRedirectStatus(redirect)

      if (statusCode === PERMANENT_REDIRECT_STATUS) {
        res.setHeader('Refresh', `0;url=${redirect.destination}`)
      }

      res.statusCode = statusCode
      res.setHeader('Location', redirect.destination)
      res.end()
    }

1274 1275
    // remove /_next/data prefix from urlPathname so it matches
    // for direct page visit and /_next/data visit
1276 1277 1278
    if (isDataReq) {
      resolvedUrlPathname = stripNextDataPath(resolvedUrlPathname)
      urlPathname = stripNextDataPath(urlPathname)
1279 1280
    }

1281
    let ssgCacheKey =
1282 1283
      isPreviewMode || !isSSG
        ? undefined // Preview mode bypasses the cache
1284 1285 1286
        : `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
            query.amp ? '.amp' : ''
          }`
J
JJ Kasper 已提交
1287

1288 1289 1290 1291 1292 1293
    if (is404Page && isSSG) {
      ssgCacheKey = `${locale ? `/${locale}` : ''}${pathname}${
        query.amp ? '.amp' : ''
      }`
    }

J
JJ Kasper 已提交
1294
    // Complete the response with cached data if its present
1295 1296 1297
    const cachedData = ssgCacheKey
      ? await this.incrementalCache.get(ssgCacheKey)
      : undefined
1298

J
JJ Kasper 已提交
1299
    if (cachedData) {
1300 1301 1302 1303 1304 1305
      if (cachedData.isNotFound) {
        // we don't currently revalidate when notFound is returned
        // so trigger rendering 404 here
        throw new NoFallbackError()
      }

1306
      const data = isDataReq
J
JJ Kasper 已提交
1307 1308 1309
        ? JSON.stringify(cachedData.pageData)
        : cachedData.html

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
      if (!isDataReq && cachedData.pageData?.pageProps?.__N_REDIRECT) {
        await handleRedirect(cachedData.pageData)
      } else {
        sendPayload(
          req,
          res,
          data,
          isDataReq ? 'json' : 'html',
          {
            generateEtags: this.renderOpts.generateEtags,
            poweredByHeader: this.renderOpts.poweredByHeader,
          },
          !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,
              }
            : undefined
        )
      }
J
JJ Kasper 已提交
1334 1335 1336 1337 1338

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

J
JJ Kasper 已提交
1341
    // If we're here, that means data is missing or it's stale.
1342
    const maybeCoalesceInvoke = ssgCacheKey
1343
      ? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey!, [])
1344 1345 1346 1347
      : (fn: any) => async () => {
          const value = await fn()
          return { isOrigin: true, value }
        }
J
JJ Kasper 已提交
1348

1349 1350 1351 1352 1353
    const doRender = maybeCoalesceInvoke(
      async (): Promise<{
        html: string | null
        pageData: any
        sprRevalidate: number | false
1354
        isNotFound?: boolean
1355
        isRedirect?: boolean
1356 1357 1358 1359
      }> => {
        let pageData: any
        let html: string | null
        let sprRevalidate: number | false
1360
        let isNotFound: boolean | undefined
1361
        let isRedirect: boolean | undefined
1362 1363 1364 1365 1366 1367 1368

        let renderResult
        // handle serverless
        if (isLikeServerless) {
          renderResult = await (components.Component as any).renderReqToHTML(
            req,
            res,
P
Prateek Bhatnagar 已提交
1369 1370 1371
            'passthrough',
            {
              fontManifest: this.renderOpts.fontManifest,
1372
              locale,
1373 1374
              locales,
              // defaultLocale,
P
Prateek Bhatnagar 已提交
1375
            }
1376
          )
J
JJ Kasper 已提交
1377

1378 1379 1380
          html = renderResult.html
          pageData = renderResult.renderOpts.pageData
          sprRevalidate = renderResult.renderOpts.revalidate
1381
          isNotFound = renderResult.renderOpts.isNotFound
1382
          isRedirect = renderResult.renderOpts.isRedirect
1383
        } else {
1384 1385 1386 1387 1388 1389 1390
          const origQuery = parseUrl(req.url || '', true).query
          const resolvedUrl = formatUrl({
            pathname: resolvedUrlPathname,
            // make sure to only add query values from original URL
            query: origQuery,
          })

1391 1392 1393 1394
          const renderOpts: RenderOpts = {
            ...components,
            ...opts,
            isDataReq,
1395
            resolvedUrl,
1396
            locale,
1397 1398
            locales,
            // defaultLocale,
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
            // 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,
1410
          }
1411

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
          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
1424
          isNotFound = (renderOpts as any).isNotFound
1425
          isRedirect = (renderOpts as any).isRedirect
J
JJ Kasper 已提交
1426 1427
        }

1428
        return { html, pageData, sprRevalidate, isNotFound, isRedirect }
J
JJ Kasper 已提交
1429
      }
1430
    )
J
JJ Kasper 已提交
1431

1432
    const isProduction = !this.renderOpts.dev
J
Joe Haddad 已提交
1433
    const isDynamicPathname = isDynamicRoute(pathname)
1434
    const didRespond = isResSent(res)
1435

1436
    const { staticPaths, fallbackMode } = hasStaticPaths
1437
      ? await this.getStaticPaths(pathname)
1438
      : { staticPaths: undefined, fallbackMode: false }
1439

1440 1441 1442 1443 1444
    // When we did not respond from cache, we need to choose to block on
    // rendering or return a skeleton.
    //
    // * Data requests always block.
    //
1445 1446
    // * Blocking mode fallback always blocks.
    //
1447 1448
    // * Preview mode toggles all pages to be resolved in a blocking manner.
    //
1449
    // * Non-dynamic pages should block (though this is an impossible
1450 1451
    //   case in production).
    //
1452 1453
    // * Dynamic pages should return their skeleton if not defined in
    //   getStaticPaths, then finish the data request on the client-side.
1454
    //
J
Joe Haddad 已提交
1455
    if (
1456
      fallbackMode !== 'blocking' &&
1457
      ssgCacheKey &&
1458 1459 1460
      !didRespond &&
      !isPreviewMode &&
      isDynamicPathname &&
1461 1462
      // Development should trigger fallback when the path is not in
      // `getStaticPaths`
1463 1464
      (isProduction ||
        !staticPaths ||
1465 1466 1467 1468 1469
        // static paths always includes locale so make sure it's prefixed
        // with it
        !staticPaths.includes(
          `${locale ? '/' + locale : ''}${resolvedUrlPathname}`
        ))
J
Joe Haddad 已提交
1470
    ) {
1471 1472 1473 1474 1475
      if (
        // In development, fall through to render to handle missing
        // getStaticPaths.
        (isProduction || staticPaths) &&
        // When fallback isn't present, abort this render so we 404
1476
        fallbackMode !== 'static'
1477
      ) {
1478
        throw new NoFallbackError()
1479 1480
      }

1481 1482
      if (!isDataReq) {
        let html: string
1483

1484 1485
        // Production already emitted the fallback as static HTML.
        if (isProduction) {
1486 1487 1488
          html = await this.incrementalCache.getFallback(
            locale ? `/${locale}${pathname}` : pathname
          )
1489 1490 1491 1492 1493 1494 1495 1496 1497
        }
        // 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
1498 1499
        }

1500 1501 1502 1503 1504 1505
        sendPayload(req, res, html, 'html', {
          generateEtags: this.renderOpts.generateEtags,
          poweredByHeader: this.renderOpts.poweredByHeader,
        })
        return null
      }
1506 1507
    }

1508 1509
    const {
      isOrigin,
1510
      value: { html, pageData, sprRevalidate, isNotFound, isRedirect },
1511
    } = await doRender()
1512
    let resHtml = html
1513 1514 1515 1516 1517 1518

    if (
      !isResSent(res) &&
      !isNotFound &&
      (isSSG || isDataReq || isServerProps)
    ) {
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
      if (isRedirect && !isDataReq) {
        await handleRedirect(pageData)
      } else {
        sendPayload(
          req,
          res,
          isDataReq ? JSON.stringify(pageData) : html,
          isDataReq ? 'json' : 'html',
          {
            generateEtags: this.renderOpts.generateEtags,
            poweredByHeader: this.renderOpts.poweredByHeader,
          },
          !this.renderOpts.dev || (isServerProps && !isDataReq)
            ? {
                private: isPreviewMode,
                stateful: !isSSG,
                revalidate: sprRevalidate,
              }
            : undefined
        )
      }
1540
      resHtml = null
1541
    }
J
JJ Kasper 已提交
1542

1543
    // Update the cache if the head request and cacheable
1544
    if (isOrigin && ssgCacheKey) {
1545 1546
      await this.incrementalCache.set(
        ssgCacheKey,
1547
        { html: html!, pageData, isNotFound, isRedirect },
1548 1549
        sprRevalidate
      )
1550 1551
    }

1552 1553 1554
    if (isNotFound) {
      throw new NoFallbackError()
    }
1555
    return resHtml
1556 1557
  }

1558
  public async renderToHTML(
J
Joe Haddad 已提交
1559 1560 1561
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1562
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1563
  ): Promise<string | null> {
1564 1565 1566
    try {
      const result = await this.findPageComponents(pathname, query)
      if (result) {
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
        try {
          return await this.renderToHTMLWithComponents(
            req,
            res,
            pathname,
            result,
            { ...this.renderOpts }
          )
        } catch (err) {
          if (!(err instanceof NoFallbackError)) {
            throw err
          }
1579
        }
1580
      }
J
Joe Haddad 已提交
1581

1582 1583 1584 1585 1586 1587
      if (this.dynamicRoutes) {
        for (const dynamicRoute of this.dynamicRoutes) {
          const params = dynamicRoute.match(pathname)
          if (!params) {
            continue
          }
J
Joe Haddad 已提交
1588

1589
          const dynamicRouteResult = await this.findPageComponents(
1590 1591 1592 1593
            dynamicRoute.page,
            query,
            params
          )
1594
          if (dynamicRouteResult) {
1595 1596 1597 1598 1599
            try {
              return await this.renderToHTMLWithComponents(
                req,
                res,
                dynamicRoute.page,
1600
                dynamicRouteResult,
1601 1602 1603 1604 1605 1606
                { ...this.renderOpts, params }
              )
            } catch (err) {
              if (!(err instanceof NoFallbackError)) {
                throw err
              }
1607
            }
J
Joe Haddad 已提交
1608 1609
          }
        }
1610 1611 1612
      }
    } catch (err) {
      this.logError(err)
1613 1614 1615 1616 1617

      if (err && err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return await this.renderErrorToHTML(err, req, res, pathname, query)
      }
1618 1619 1620 1621 1622
      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 已提交
1623 1624
  }

J
Joe Haddad 已提交
1625 1626 1627 1628 1629
  public async renderError(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1630
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1631 1632 1633
  ): Promise<void> {
    res.setHeader(
      'Cache-Control',
1634
      'no-cache, no-store, max-age=0, must-revalidate'
J
Joe Haddad 已提交
1635
    )
N
Naoyuki Kanezawa 已提交
1636
    const html = await this.renderErrorToHTML(err, req, res, pathname, query)
1637
    if (html === null) {
1638 1639
      return
    }
1640
    return this.sendHTML(req, res, html)
N
nkzawa 已提交
1641 1642
  }

1643 1644 1645 1646 1647 1648 1649 1650 1651
  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 已提交
1652 1653 1654 1655 1656
  public async renderErrorToHTML(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    _pathname: string,
1657
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1658
  ) {
1659
    let result: null | FindComponentsResult = null
1660

1661 1662 1663
    const is404 = res.statusCode === 404
    let using404Page = false

1664
    // use static 404 page if available and is 404 response
1665
    if (is404) {
1666
      result = await this.findPageComponents('/404', query)
1667
      using404Page = result !== null
1668 1669 1670 1671 1672 1673
    }

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

1674 1675 1676
    if (
      process.env.NODE_ENV !== 'production' &&
      !using404Page &&
1677 1678
      (await this.hasPage('/_error')) &&
      !(await this.hasPage('/404'))
1679 1680 1681 1682
    ) {
      this.customErrorNo404Warn()
    }

1683
    let html: string | null
1684
    try {
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
      try {
        html = await this.renderToHTMLWithComponents(
          req,
          res,
          using404Page ? '/404' : '/_error',
          result!,
          {
            ...this.renderOpts,
            err,
          }
        )
1696 1697
      } catch (maybeFallbackError) {
        if (maybeFallbackError instanceof NoFallbackError) {
1698
          throw new Error('invariant: failed to render error page')
1699
        }
1700
        throw maybeFallbackError
1701
      }
1702 1703
    } catch (renderToHtmlError) {
      console.error(renderToHtmlError)
1704 1705 1706 1707
      res.statusCode = 500
      html = 'Internal Server Error'
    }
    return html
N
Naoyuki Kanezawa 已提交
1708 1709
  }

J
Joe Haddad 已提交
1710 1711 1712
  public async render404(
    req: IncomingMessage,
    res: ServerResponse,
1713
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1714
  ): Promise<void> {
1715 1716
    const url: any = req.url
    const { pathname, query } = parsedUrl ? parsedUrl : parseUrl(url, true)
N
Naoyuki Kanezawa 已提交
1717
    res.statusCode = 404
1718
    return this.renderError(null, req, res, pathname!, query)
N
Naoyuki Kanezawa 已提交
1719
  }
N
Naoyuki Kanezawa 已提交
1720

J
Joe Haddad 已提交
1721 1722 1723 1724
  public async serveStatic(
    req: IncomingMessage,
    res: ServerResponse,
    path: string,
1725
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1726
  ): Promise<void> {
A
Arunoda Susiripala 已提交
1727
    if (!this.isServeableUrl(path)) {
1728
      return this.render404(req, res, parsedUrl)
A
Arunoda Susiripala 已提交
1729 1730
    }

1731 1732 1733 1734 1735 1736
    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 已提交
1737
    try {
1738
      await serveStatic(req, res, path)
N
Naoyuki Kanezawa 已提交
1739
    } catch (err) {
T
Tim Neutkens 已提交
1740
      if (err.code === 'ENOENT' || err.statusCode === 404) {
1741
        this.render404(req, res, parsedUrl)
1742 1743 1744
      } else if (err.statusCode === 412) {
        res.statusCode = 412
        return this.renderError(err, req, res, path)
N
Naoyuki Kanezawa 已提交
1745 1746 1747 1748 1749 1750
      } else {
        throw err
      }
    }
  }

1751 1752 1753 1754 1755 1756 1757 1758 1759
  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 已提交
1760
      userFilesStatic = recursiveReadDirSync(pathUserFilesStatic).map((f) =>
1761 1762 1763 1764 1765 1766
        join('.', 'static', f)
      )
    }

    let userFilesPublic: string[] = []
    if (this.publicDir && fs.existsSync(this.publicDir)) {
J
Joe Haddad 已提交
1767
      userFilesPublic = recursiveReadDirSync(this.publicDir).map((f) =>
1768 1769 1770 1771 1772 1773 1774
        join('.', 'public', f)
      )
    }

    let nextFilesStatic: string[] = []
    nextFilesStatic = recursiveReadDirSync(
      join(this.distDir, 'static')
J
Joe Haddad 已提交
1775
    ).map((f) => join('.', relative(this.dir, this.distDir), 'static', f))
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809

    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 已提交
1810
    if (
1811 1812 1813
      (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 已提交
1814 1815 1816 1817
    ) {
      return false
    }

1818 1819 1820 1821
    // Check against the real filesystem paths
    const filesystemUrls = this.getFilesystemPaths()
    const resolved = relative(this.dir, untrustedFilePath)
    return filesystemUrls.has(resolved)
A
Arunoda Susiripala 已提交
1822 1823
  }

1824
  protected readBuildId(): string {
1825 1826 1827 1828 1829
    const buildIdFile = join(this.distDir, BUILD_ID_FILE)
    try {
      return fs.readFileSync(buildIdFile, 'utf8').trim()
    } catch (err) {
      if (!fs.existsSync(buildIdFile)) {
J
Joe Haddad 已提交
1830
        throw new Error(
1831
          `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 已提交
1832
        )
1833 1834 1835
      }

      throw err
1836
    }
1837
  }
1838

1839
  protected get _isLikeServerless(): boolean {
1840 1841
    return isTargetLikeServerless(this.nextConfig.target)
  }
1842
}
1843

1844 1845 1846 1847
function prepareServerlessUrl(
  req: IncomingMessage,
  query: ParsedUrlQuery
): void {
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
  const curUrl = parseUrl(req.url!, true)
  req.url = formatUrl({
    ...curUrl,
    search: undefined,
    query: {
      ...curUrl.query,
      ...query,
    },
  })
}
1858 1859

class NoFallbackError extends Error {}