next-server.ts 53.7 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
}

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

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

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

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

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

T
Tim Neutkens 已提交
185
    this.buildId = this.readBuildId()
186

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

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

212
    if (compress && this.nextConfig.target === 'server') {
213 214 215
      this.compression = compression() as Middleware
    }

216
    // Initialize next/config with the environment configuration
217 218 219 220
    envConfig.setConfig({
      serverRuntimeConfig,
      publicRuntimeConfig,
    })
221

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

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

232
    this.customRoutes = this.getCustomRoutes()
J
JJ Kasper 已提交
233
    this.router = new Router(this.generateRoutes())
234
    this.setAssetPrefix(assetPrefix)
J
JJ Kasper 已提交
235

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

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

    /**
     * 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)
    }
268 269 270
    if (this.renderOpts.optimizeImages) {
      process.env.__NEXT_OPTIMIZE_IMAGES = JSON.stringify(true)
    }
N
Naoyuki Kanezawa 已提交
271
  }
N
nkzawa 已提交
272

273
  protected currentPhase(): string {
274
    return PHASE_PRODUCTION_SERVER
275 276
  }

S
Steven 已提交
277
  public logError(err: Error): void {
278 279 280
    if (this.onErrorMiddleware) {
      this.onErrorMiddleware({ err })
    }
281
    if (this.quiet) return
282
    console.error(err)
283 284
  }

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

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

298 299 300
    // 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 已提交
301
    }
302
    ;(req as any).__NEXT_INIT_QUERY = Object.assign({}, parsedUrl.query)
303

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

306 307 308 309 310
    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 已提交
311 312
    }

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

323 324 325 326 327
      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)
328 329 330 331
      if (detectedDomain) {
        defaultLocale = detectedDomain.defaultLocale
        detectedLocale = defaultLocale
      }
332

333 334
      // if not domain specific locale use accept-language preferred
      detectedLocale = detectedLocale || acceptPreferredLocale
335

336 337 338 339 340 341 342 343 344
      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 已提交
345
        ;(req as any).__nextStrippedLocale = true
346
        parsedUrl.pathname = localePathResult.pathname
347 348 349 350 351 352 353 354 355 356
      }

      // 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
357

358 359 360
        const matchedDomain = detectDomainLocale(
          i18n.domains,
          undefined,
361
          localeToCheck
362 363
        )

364 365 366 367 368
        if (
          matchedDomain &&
          (matchedDomain.domain !== detectedDomain.domain ||
            localeToCheck !== matchedDomain.defaultLocale)
        ) {
369 370
          localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
            matchedDomain.domain
371 372
          }/${
            localeToCheck === matchedDomain.defaultLocale ? '' : localeToCheck
373
          }`
374 375 376
        }
      }

377
      const denormalizedPagePath = denormalizePagePath(pathname || '/')
378
      const detectedDefaultLocale =
379 380
        !detectedLocale ||
        detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
381 382 383 384
      const shouldStripDefaultLocale = false
      // detectedDefaultLocale &&
      // denormalizedPagePath.toLowerCase() ===
      //   `/${i18n.defaultLocale.toLowerCase()}`
385

386 387
      const shouldAddLocalePrefix =
        !detectedDefaultLocale && denormalizedPagePath === '/'
388

389
      detectedLocale = detectedLocale || i18n.defaultLocale
390

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

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

      parsedUrl.query.__nextLocale =
        localePathResult.detectedLocale ||
        detectedDomain?.defaultLocale ||
        defaultLocale
440 441
    }

442
    res.statusCode = 200
443 444 445
    try {
      return await this.run(req, res, parsedUrl)
    } catch (err) {
J
Joe Haddad 已提交
446 447 448
      this.logError(err)
      res.statusCode = 500
      res.end('Internal Server Error')
449
    }
450 451
  }

452
  public getRequestHandler() {
453
    return this.handleRequest.bind(this)
N
nkzawa 已提交
454 455
  }

456
  public setAssetPrefix(prefix?: string): void {
457
    this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
458 459
  }

460
  // Backwards compatibility
461
  public async prepare(): Promise<void> {}
N
nkzawa 已提交
462

T
Tim Neutkens 已提交
463
  // Backwards compatibility
464
  protected async close(): Promise<void> {}
T
Tim Neutkens 已提交
465

466
  protected setImmutableAssetCacheControl(res: ServerResponse): void {
T
Tim Neutkens 已提交
467
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
N
nkzawa 已提交
468 469
  }

470
  protected getCustomRoutes(): CustomRoutes {
J
JJ Kasper 已提交
471 472 473
    return require(join(this.distDir, ROUTES_MANIFEST))
  }

474 475 476 477
  private _cachedPreviewManifest: PrerenderManifest | undefined
  protected getPrerenderManifest(): PrerenderManifest {
    if (this._cachedPreviewManifest) {
      return this._cachedPreviewManifest
J
Joe Haddad 已提交
478
    }
479 480 481 482 483 484
    const manifest = require(join(this.distDir, PRERENDER_MANIFEST))
    return (this._cachedPreviewManifest = manifest)
  }

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

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

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

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

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

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

J
Joe Haddad 已提交
586
          const { i18n } = this.nextConfig
587 588

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

            if (localePathResult.detectedLocale) {
              pathname = localePathResult.pathname
              detectedLocale = localePathResult.detectedLocale
            }
601
            _parsedUrl.query.__nextLocale = detectedLocale!
602 603
          }
          pathname = getRouteFromAssetPath(pathname, '.json')
J
JJ Kasper 已提交
604

J
JJ Kasper 已提交
605
          const parsedUrl = parseUrl(pathname, true)
606

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

642 643 644 645 646 647
    const getCustomRouteBasePath = (r: { basePath?: false }) => {
      return r.basePath !== false && this.renderOpts.dev
        ? this.nextConfig.basePath
        : ''
    }

648 649 650 651
    const getCustomRoute = (r: Rewrite | Redirect | Header, type: RouteType) =>
      ({
        ...r,
        type,
652
        match: getCustomRouteMatcher(`${getCustomRouteBasePath(r)}${r.source}`),
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        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) {
670 671
              key = compileNonPath(key, params)
              value = compileNonPath(value, params)
672
            }
673 674 675 676 677 678 679
            res.setHeader(key, value)
          }
          return { finished: false }
        },
      } as Route
    })

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
    // 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
        },
      })
    }

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

          const { query } = parsedDestination
713
          delete (parsedDestination as any).query
714

715
          parsedDestination.search = stringifyQuery(req, query)
716

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

753 754
          // external rewrite, proxy it
          if (parsedDestination.protocol) {
755
            const { query } = parsedDestination
756
            delete (parsedDestination as any).query
757
            parsedDestination.search = stringifyQuery(req, query)
758

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

778 779 780 781 782 783 784 785
          return {
            finished: false,
            pathname: newUrl,
            query: parsedDestination.query,
          }
        },
      } as Route
    })
786 787 788 789 790 791

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

J
Jan Potoms 已提交
797
        // next.js core assumes page path without trailing slash
798
        pathname = removePathTrailingSlash(pathname)
J
Jan Potoms 已提交
799

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

        await this.render(req, res, pathname, query, parsedUrl)
813 814 815 816
        return {
          finished: true,
        }
      },
817
    }
818

819
    const { useFileSystemPublicRoutes } = this.nextConfig
J
Joe Haddad 已提交
820

821 822
    if (useFileSystemPublicRoutes) {
      this.dynamicRoutes = this.getDynamicRoutes()
823
    }
N
nkzawa 已提交
824

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

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

856 857 858 859 860
  protected async _beforeCatchAllRender(
    _req: IncomingMessage,
    _res: ServerResponse,
    _params: Params,
    _parsedUrl: UrlWithParsedQuery
861
  ): Promise<boolean> {
862 863 864
    return false
  }

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

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

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

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

902 903 904 905 906 907 908 909 910 911
    let builtPagePath
    try {
      builtPagePath = await this.getPagePath(page)
    } catch (err) {
      if (err.code === 'ENOENT') {
        return false
      }
      throw err
    }

912
    const pageModule = await require(builtPagePath)
913
    query = { ...query, ...params }
J
JJ Kasper 已提交
914

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

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

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

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

          // if basePath is defined require it be present
          if (basePath) {
952 953 954 955 956 957 958 959 960 961 962 963 964
            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)
965 966
          }

967
          const path = `/${pathParts.join('/')}`
968 969 970 971 972

          if (publicFiles.has(path)) {
            await this.serveStatic(
              req,
              res,
973
              join(this.publicDir, ...pathParts),
974 975
              parsedUrl
            )
976 977 978
            return {
              finished: true,
            }
979 980 981 982 983 984 985
          }
          return {
            finished: false,
          }
        },
      } as Route,
    ]
986 987
  }

988
  protected getDynamicRoutes() {
989 990
    return getSortedRoutes(Object.keys(this.pagesManifest!))
      .filter(isDynamicRoute)
J
Joe Haddad 已提交
991
      .map((page) => ({
992 993 994
        page,
        match: getRouteMatcher(getRouteRegex(page)),
      }))
J
Joe Haddad 已提交
995 996
  }

997
  private handleCompression(req: IncomingMessage, res: ServerResponse): void {
998 999 1000 1001 1002
    if (this.compression) {
      this.compression(req, res, () => {})
    }
  }

1003
  protected async run(
J
Joe Haddad 已提交
1004 1005
    req: IncomingMessage,
    res: ServerResponse,
1006
    parsedUrl: UrlWithParsedQuery
1007
  ): Promise<void> {
1008 1009
    this.handleCompression(req, res)

1010
    try {
1011 1012
      const matched = await this.router.execute(req, res, parsedUrl)
      if (matched) {
1013 1014 1015 1016 1017 1018 1019 1020
        return
      }
    } catch (err) {
      if (err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return this.renderError(null, req, res, '/_error', {})
      }
      throw err
1021 1022
    }

1023
    await this.render404(req, res, parsedUrl)
N
nkzawa 已提交
1024 1025
  }

1026
  protected async sendHTML(
J
Joe Haddad 已提交
1027 1028
    req: IncomingMessage,
    res: ServerResponse,
1029
    html: string
1030
  ): Promise<void> {
T
Tim Neutkens 已提交
1031
    const { generateEtags, poweredByHeader } = this.renderOpts
1032 1033 1034 1035
    return sendPayload(req, res, html, 'html', {
      generateEtags,
      poweredByHeader,
    })
1036 1037
  }

J
Joe Haddad 已提交
1038 1039 1040 1041 1042
  public async render(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
    query: ParsedUrlQuery = {},
1043
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1044
  ): Promise<void> {
1045 1046 1047 1048 1049 1050
    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`
      )
    }

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    if (
      this.renderOpts.customServer &&
      pathname === '/index' &&
      !(await this.hasPage('/index'))
    ) {
      // maintain backwards compatibility for custom server
      // (see custom-server integration tests)
      pathname = '/'
    }

1061
    const url: any = req.url
1062

1063 1064 1065 1066
    // 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
1067
    if (
1068 1069 1070
      !query._nextDataReq &&
      (url.match(/^\/_next\//) ||
        (this.hasStaticDir && url.match(/^\/static\//)))
1071
    ) {
1072 1073 1074
      return this.handleRequest(req, res, parsedUrl)
    }

1075
    if (isBlockedPage(pathname)) {
1076
      return this.render404(req, res, parsedUrl)
1077 1078
    }

1079
    const html = await this.renderToHTML(req, res, pathname, query)
1080 1081
    // Request was ended by the user
    if (html === null) {
1082 1083 1084
      return
    }

1085
    return this.sendHTML(req, res, html)
N
Naoyuki Kanezawa 已提交
1086
  }
N
nkzawa 已提交
1087

J
Joe Haddad 已提交
1088
  private async findPageComponents(
J
Joe Haddad 已提交
1089
    pathname: string,
1090 1091 1092
    query: ParsedUrlQuery = {},
    params: Params | null = null
  ): Promise<FindComponentsResult | null> {
1093
    let paths = [
1094 1095 1096 1097
      // try serving a static AMP version first
      query.amp ? normalizePagePath(pathname) + '.amp' : null,
      pathname,
    ].filter(Boolean)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

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

1108
    for (const pagePath of paths) {
J
JJ Kasper 已提交
1109
      try {
1110
        const components = await loadComponents(
J
Joe Haddad 已提交
1111
          this.distDir,
1112 1113
          pagePath!,
          !this.renderOpts.dev && this._isLikeServerless
J
Joe Haddad 已提交
1114
        )
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
        // 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
        }

1127 1128 1129
        return {
          components,
          query: {
1130
            ...(components.getStaticProps
1131 1132 1133 1134 1135
              ? {
                  amp: query.amp,
                  _nextDataReq: query._nextDataReq,
                  __nextLocale: query.__nextLocale,
                }
1136 1137 1138 1139
              : query),
            ...(params || {}),
          },
        }
J
JJ Kasper 已提交
1140 1141 1142 1143
      } catch (err) {
        if (err.code !== 'ENOENT') throw err
      }
    }
1144
    return null
J
Joe Haddad 已提交
1145 1146
  }

1147
  protected async getStaticPaths(
1148 1149 1150
    pathname: string
  ): Promise<{
    staticPaths: string[] | undefined
1151
    fallbackMode: 'static' | 'blocking' | false
1152
  }> {
1153 1154 1155 1156 1157
    // `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.
1158 1159
    const fallbackField = this.getPrerenderManifest().dynamicRoutes[pathname]
      .fallback
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169
    return {
      staticPaths,
      fallbackMode:
        typeof fallbackField === 'string'
          ? 'static'
          : fallbackField === null
          ? 'blocking'
          : false,
    }
1170 1171
  }

J
Joe Haddad 已提交
1172 1173 1174 1175
  private async renderToHTMLWithComponents(
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1176
    { components, query }: FindComponentsResult,
1177
    opts: RenderOptsPartial
1178
  ): Promise<string | null> {
1179 1180
    const is404Page = pathname === '/404'

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    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

1192
    // we need to ensure the status code if /404 is visited directly
1193
    if (is404Page && !isDataReq) {
1194 1195 1196
      res.statusCode = 404
    }

J
JJ Kasper 已提交
1197
    // handle static page
1198 1199
    if (typeof components.Component === 'string') {
      return components.Component
J
Joe Haddad 已提交
1200 1201
    }

1202 1203 1204 1205
    if (!query.amp) {
      delete query.amp
    }

J
JJ Kasper 已提交
1206 1207
    const locale = query.__nextLocale as string
    delete query.__nextLocale
1208

J
Joe Haddad 已提交
1209
    const { i18n } = this.nextConfig
1210
    const locales = i18n.locales as string[]
J
JJ Kasper 已提交
1211

1212 1213 1214 1215 1216 1217 1218 1219
    let previewData: string | false | object | undefined
    let isPreviewMode = false

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

1220 1221 1222
    // 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
1223 1224 1225
    let urlPathname = parseUrl(req.url || '').pathname || '/'

    let resolvedUrlPathname = (req as any)._nextRewroteUrl
1226
      ? (req as any)._nextRewroteUrl
1227
      : urlPathname
1228

1229 1230 1231 1232 1233 1234 1235 1236 1237
    resolvedUrlPathname = removePathTrailingSlash(resolvedUrlPathname)
    urlPathname = removePathTrailingSlash(urlPathname)

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

J
Joe Haddad 已提交
1239
      if (this.nextConfig.i18n) {
J
JJ Kasper 已提交
1240
        return normalizeLocalePath(path, locales).pathname
1241
      }
1242 1243
      return path
    }
1244

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    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()
    }

1261 1262
    // remove /_next/data prefix from urlPathname so it matches
    // for direct page visit and /_next/data visit
1263 1264 1265
    if (isDataReq) {
      resolvedUrlPathname = stripNextDataPath(resolvedUrlPathname)
      urlPathname = stripNextDataPath(urlPathname)
1266 1267
    }

1268
    let ssgCacheKey =
1269 1270
      isPreviewMode || !isSSG
        ? undefined // Preview mode bypasses the cache
1271 1272 1273
        : `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
            query.amp ? '.amp' : ''
          }`
J
JJ Kasper 已提交
1274

1275 1276 1277 1278 1279 1280
    if (is404Page && isSSG) {
      ssgCacheKey = `${locale ? `/${locale}` : ''}${pathname}${
        query.amp ? '.amp' : ''
      }`
    }

J
JJ Kasper 已提交
1281
    // Complete the response with cached data if its present
1282 1283 1284
    const cachedData = ssgCacheKey
      ? await this.incrementalCache.get(ssgCacheKey)
      : undefined
1285

J
JJ Kasper 已提交
1286
    if (cachedData) {
1287 1288 1289 1290 1291 1292
      if (cachedData.isNotFound) {
        // we don't currently revalidate when notFound is returned
        // so trigger rendering 404 here
        throw new NoFallbackError()
      }

1293
      const data = isDataReq
J
JJ Kasper 已提交
1294 1295 1296
        ? JSON.stringify(cachedData.pageData)
        : cachedData.html

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
      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 已提交
1321 1322 1323 1324 1325

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

J
JJ Kasper 已提交
1328
    // If we're here, that means data is missing or it's stale.
1329
    const maybeCoalesceInvoke = ssgCacheKey
1330
      ? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey!, [])
1331 1332 1333 1334
      : (fn: any) => async () => {
          const value = await fn()
          return { isOrigin: true, value }
        }
J
JJ Kasper 已提交
1335

1336 1337 1338 1339 1340
    const doRender = maybeCoalesceInvoke(
      async (): Promise<{
        html: string | null
        pageData: any
        sprRevalidate: number | false
1341
        isNotFound?: boolean
1342
        isRedirect?: boolean
1343 1344 1345 1346
      }> => {
        let pageData: any
        let html: string | null
        let sprRevalidate: number | false
1347
        let isNotFound: boolean | undefined
1348
        let isRedirect: boolean | undefined
1349 1350 1351 1352 1353 1354 1355

        let renderResult
        // handle serverless
        if (isLikeServerless) {
          renderResult = await (components.Component as any).renderReqToHTML(
            req,
            res,
P
Prateek Bhatnagar 已提交
1356 1357 1358
            'passthrough',
            {
              fontManifest: this.renderOpts.fontManifest,
1359
              locale,
1360 1361
              locales,
              // defaultLocale,
P
Prateek Bhatnagar 已提交
1362
            }
1363
          )
J
JJ Kasper 已提交
1364

1365 1366 1367
          html = renderResult.html
          pageData = renderResult.renderOpts.pageData
          sprRevalidate = renderResult.renderOpts.revalidate
1368
          isNotFound = renderResult.renderOpts.isNotFound
1369
          isRedirect = renderResult.renderOpts.isRedirect
1370
        } else {
1371 1372 1373 1374 1375 1376 1377
          const origQuery = parseUrl(req.url || '', true).query
          const resolvedUrl = formatUrl({
            pathname: resolvedUrlPathname,
            // make sure to only add query values from original URL
            query: origQuery,
          })

1378 1379 1380 1381
          const renderOpts: RenderOpts = {
            ...components,
            ...opts,
            isDataReq,
1382
            resolvedUrl,
1383
            locale,
1384 1385
            locales,
            // defaultLocale,
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
            // 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,
1397
          }
1398

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
          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
1411
          isNotFound = (renderOpts as any).isNotFound
1412
          isRedirect = (renderOpts as any).isRedirect
J
JJ Kasper 已提交
1413 1414
        }

1415
        return { html, pageData, sprRevalidate, isNotFound, isRedirect }
J
JJ Kasper 已提交
1416
      }
1417
    )
J
JJ Kasper 已提交
1418

1419
    const isProduction = !this.renderOpts.dev
J
Joe Haddad 已提交
1420
    const isDynamicPathname = isDynamicRoute(pathname)
1421
    const didRespond = isResSent(res)
1422

1423
    const { staticPaths, fallbackMode } = hasStaticPaths
1424
      ? await this.getStaticPaths(pathname)
1425
      : { staticPaths: undefined, fallbackMode: false }
1426

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

1468 1469
      if (!isDataReq) {
        let html: string
1470

1471 1472
        // Production already emitted the fallback as static HTML.
        if (isProduction) {
1473 1474 1475
          html = await this.incrementalCache.getFallback(
            locale ? `/${locale}${pathname}` : pathname
          )
1476 1477 1478 1479 1480 1481 1482 1483 1484
        }
        // 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
1485 1486
        }

1487 1488 1489 1490 1491 1492
        sendPayload(req, res, html, 'html', {
          generateEtags: this.renderOpts.generateEtags,
          poweredByHeader: this.renderOpts.poweredByHeader,
        })
        return null
      }
1493 1494
    }

1495 1496
    const {
      isOrigin,
1497
      value: { html, pageData, sprRevalidate, isNotFound, isRedirect },
1498
    } = await doRender()
1499
    let resHtml = html
1500 1501 1502 1503 1504 1505

    if (
      !isResSent(res) &&
      !isNotFound &&
      (isSSG || isDataReq || isServerProps)
    ) {
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
      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
        )
      }
1527
      resHtml = null
1528
    }
J
JJ Kasper 已提交
1529

1530
    // Update the cache if the head request and cacheable
1531
    if (isOrigin && ssgCacheKey) {
1532 1533
      await this.incrementalCache.set(
        ssgCacheKey,
1534
        { html: html!, pageData, isNotFound, isRedirect },
1535 1536
        sprRevalidate
      )
1537 1538
    }

1539 1540 1541
    if (isNotFound) {
      throw new NoFallbackError()
    }
1542
    return resHtml
1543 1544
  }

1545
  public async renderToHTML(
J
Joe Haddad 已提交
1546 1547 1548
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1549
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1550
  ): Promise<string | null> {
1551 1552 1553
    try {
      const result = await this.findPageComponents(pathname, query)
      if (result) {
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
        try {
          return await this.renderToHTMLWithComponents(
            req,
            res,
            pathname,
            result,
            { ...this.renderOpts }
          )
        } catch (err) {
          if (!(err instanceof NoFallbackError)) {
            throw err
          }
1566
        }
1567
      }
J
Joe Haddad 已提交
1568

1569 1570 1571 1572 1573 1574
      if (this.dynamicRoutes) {
        for (const dynamicRoute of this.dynamicRoutes) {
          const params = dynamicRoute.match(pathname)
          if (!params) {
            continue
          }
J
Joe Haddad 已提交
1575

1576
          const dynamicRouteResult = await this.findPageComponents(
1577 1578 1579 1580
            dynamicRoute.page,
            query,
            params
          )
1581
          if (dynamicRouteResult) {
1582 1583 1584 1585 1586
            try {
              return await this.renderToHTMLWithComponents(
                req,
                res,
                dynamicRoute.page,
1587
                dynamicRouteResult,
1588 1589 1590 1591 1592 1593
                { ...this.renderOpts, params }
              )
            } catch (err) {
              if (!(err instanceof NoFallbackError)) {
                throw err
              }
1594
            }
J
Joe Haddad 已提交
1595 1596
          }
        }
1597 1598 1599
      }
    } catch (err) {
      this.logError(err)
1600 1601 1602 1603 1604

      if (err && err.code === 'DECODE_FAILED') {
        res.statusCode = 400
        return await this.renderErrorToHTML(err, req, res, pathname, query)
      }
1605 1606 1607 1608 1609
      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 已提交
1610 1611
  }

J
Joe Haddad 已提交
1612 1613 1614 1615 1616
  public async renderError(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    pathname: string,
1617
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1618 1619 1620
  ): Promise<void> {
    res.setHeader(
      'Cache-Control',
1621
      'no-cache, no-store, max-age=0, must-revalidate'
J
Joe Haddad 已提交
1622
    )
N
Naoyuki Kanezawa 已提交
1623
    const html = await this.renderErrorToHTML(err, req, res, pathname, query)
1624
    if (html === null) {
1625 1626
      return
    }
1627
    return this.sendHTML(req, res, html)
N
nkzawa 已提交
1628 1629
  }

1630 1631 1632 1633 1634 1635 1636 1637 1638
  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 已提交
1639 1640 1641 1642 1643
  public async renderErrorToHTML(
    err: Error | null,
    req: IncomingMessage,
    res: ServerResponse,
    _pathname: string,
1644
    query: ParsedUrlQuery = {}
J
Joe Haddad 已提交
1645
  ) {
1646
    let result: null | FindComponentsResult = null
1647

1648 1649 1650
    const is404 = res.statusCode === 404
    let using404Page = false

1651
    // use static 404 page if available and is 404 response
1652
    if (is404) {
1653
      result = await this.findPageComponents('/404', query)
1654
      using404Page = result !== null
1655 1656 1657 1658 1659 1660
    }

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

1661 1662 1663
    if (
      process.env.NODE_ENV !== 'production' &&
      !using404Page &&
1664 1665
      (await this.hasPage('/_error')) &&
      !(await this.hasPage('/404'))
1666 1667 1668 1669
    ) {
      this.customErrorNo404Warn()
    }

1670
    let html: string | null
1671
    try {
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
      try {
        html = await this.renderToHTMLWithComponents(
          req,
          res,
          using404Page ? '/404' : '/_error',
          result!,
          {
            ...this.renderOpts,
            err,
          }
        )
1683 1684
      } catch (maybeFallbackError) {
        if (maybeFallbackError instanceof NoFallbackError) {
1685
          throw new Error('invariant: failed to render error page')
1686
        }
1687
        throw maybeFallbackError
1688
      }
1689 1690
    } catch (renderToHtmlError) {
      console.error(renderToHtmlError)
1691 1692 1693 1694
      res.statusCode = 500
      html = 'Internal Server Error'
    }
    return html
N
Naoyuki Kanezawa 已提交
1695 1696
  }

J
Joe Haddad 已提交
1697 1698 1699
  public async render404(
    req: IncomingMessage,
    res: ServerResponse,
1700
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1701
  ): Promise<void> {
1702 1703
    const url: any = req.url
    const { pathname, query } = parsedUrl ? parsedUrl : parseUrl(url, true)
N
Naoyuki Kanezawa 已提交
1704
    res.statusCode = 404
1705
    return this.renderError(null, req, res, pathname!, query)
N
Naoyuki Kanezawa 已提交
1706
  }
N
Naoyuki Kanezawa 已提交
1707

J
Joe Haddad 已提交
1708 1709 1710 1711
  public async serveStatic(
    req: IncomingMessage,
    res: ServerResponse,
    path: string,
1712
    parsedUrl?: UrlWithParsedQuery
J
Joe Haddad 已提交
1713
  ): Promise<void> {
A
Arunoda Susiripala 已提交
1714
    if (!this.isServeableUrl(path)) {
1715
      return this.render404(req, res, parsedUrl)
A
Arunoda Susiripala 已提交
1716 1717
    }

1718 1719 1720 1721 1722 1723
    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 已提交
1724
    try {
1725
      await serveStatic(req, res, path)
N
Naoyuki Kanezawa 已提交
1726
    } catch (err) {
T
Tim Neutkens 已提交
1727
      if (err.code === 'ENOENT' || err.statusCode === 404) {
1728
        this.render404(req, res, parsedUrl)
1729 1730 1731
      } else if (err.statusCode === 412) {
        res.statusCode = 412
        return this.renderError(err, req, res, path)
N
Naoyuki Kanezawa 已提交
1732 1733 1734 1735 1736 1737
      } else {
        throw err
      }
    }
  }

1738 1739 1740 1741 1742 1743 1744 1745 1746
  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 已提交
1747
      userFilesStatic = recursiveReadDirSync(pathUserFilesStatic).map((f) =>
1748 1749 1750 1751 1752 1753
        join('.', 'static', f)
      )
    }

    let userFilesPublic: string[] = []
    if (this.publicDir && fs.existsSync(this.publicDir)) {
J
Joe Haddad 已提交
1754
      userFilesPublic = recursiveReadDirSync(this.publicDir).map((f) =>
1755 1756 1757 1758 1759 1760 1761
        join('.', 'public', f)
      )
    }

    let nextFilesStatic: string[] = []
    nextFilesStatic = recursiveReadDirSync(
      join(this.distDir, 'static')
J
Joe Haddad 已提交
1762
    ).map((f) => join('.', relative(this.dir, this.distDir), 'static', f))
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

    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 已提交
1797
    if (
1798 1799 1800
      (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 已提交
1801 1802 1803 1804
    ) {
      return false
    }

1805 1806 1807 1808
    // Check against the real filesystem paths
    const filesystemUrls = this.getFilesystemPaths()
    const resolved = relative(this.dir, untrustedFilePath)
    return filesystemUrls.has(resolved)
A
Arunoda Susiripala 已提交
1809 1810
  }

1811
  protected readBuildId(): string {
1812 1813 1814 1815 1816
    const buildIdFile = join(this.distDir, BUILD_ID_FILE)
    try {
      return fs.readFileSync(buildIdFile, 'utf8').trim()
    } catch (err) {
      if (!fs.existsSync(buildIdFile)) {
J
Joe Haddad 已提交
1817
        throw new Error(
1818
          `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 已提交
1819
        )
1820 1821 1822
      }

      throw err
1823
    }
1824
  }
1825

1826
  protected get _isLikeServerless(): boolean {
1827 1828
    return isTargetLikeServerless(this.nextConfig.target)
  }
1829
}
1830

1831 1832 1833 1834
function prepareServerlessUrl(
  req: IncomingMessage,
  query: ParsedUrlQuery
): void {
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
  const curUrl = parseUrl(req.url!, true)
  req.url = formatUrl({
    ...curUrl,
    search: undefined,
    query: {
      ...curUrl.query,
      ...query,
    },
  })
}
1845 1846

class NoFallbackError extends Error {}