2020-03-29 00:31:06 +01:00
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
2020-02-12 02:16:42 +01:00
import crypto from 'crypto'
2020-03-29 18:21:53 +02:00
import findUp from 'next/dist/compiled/find-up'
2019-10-04 18:11:39 +02:00
import fs from 'fs'
import { IncomingMessage , ServerResponse } from 'http'
2020-03-26 17:58:15 +01:00
import Worker from 'jest-worker'
import { join , relative , resolve , sep } from 'path'
2019-10-04 18:11:39 +02:00
import React from 'react'
import { UrlWithParsedQuery } from 'url'
2019-09-16 23:06:30 +02:00
import { promisify } from 'util'
2019-10-04 18:11:39 +02:00
import Watchpack from 'watchpack'
2019-03-19 00:21:18 +01:00
import { ampValidation } from '../build/output/index'
2019-04-09 17:52:03 +02:00
import * as Log from '../build/output/log'
2020-02-12 02:16:42 +01:00
import checkCustomRoutes from '../lib/check-custom-routes'
2019-10-04 18:11:39 +02:00
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
2019-05-09 04:51:23 +02:00
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
2019-10-04 18:11:39 +02:00
import { PHASE_DEVELOPMENT_SERVER } from '../next-server/lib/constants'
2019-05-31 11:27:56 +02:00
import {
getRouteMatcher ,
getRouteRegex ,
2019-06-20 20:41:02 +02:00
getSortedRoutes ,
2019-10-04 18:11:39 +02:00
isDynamicRoute ,
2019-09-04 16:00:54 +02:00
} from '../next-server/lib/router/utils'
2020-02-12 02:16:42 +01:00
import { __ApiPreviewProps } from '../next-server/server/api-utils'
2019-10-04 18:11:39 +02:00
import Server , { ServerConstructor } from '../next-server/server/next-server'
2019-09-16 23:06:30 +02:00
import { normalizePagePath } from '../next-server/server/normalize-page-path'
2020-02-12 02:16:42 +01:00
import Router , { Params , route } from '../next-server/server/router'
2020-02-14 21:42:44 +01:00
import { eventCliSession } from '../telemetry/events'
2019-10-10 19:18:07 +02:00
import { Telemetry } from '../telemetry/storage'
2019-10-04 18:11:39 +02:00
import ErrorDebug from './error-debug'
import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
2019-04-04 23:47:17 +02:00
if ( typeof React . Suspense === 'undefined' ) {
2019-05-27 20:20:33 +02:00
throw new Error (
2020-01-27 14:35:12 +01:00
` The version of React you are using is lower than the minimum required version needed for Next.js. Please upgrade "react" and "react-dom": "npm install react react-dom" https://err.sh/zeit/next.js/invalid-react-version `
2019-05-27 20:20:33 +02:00
)
2019-04-04 23:47:17 +02:00
}
2019-09-16 23:06:30 +02:00
const fsStat = promisify ( fs . stat )
2018-09-28 14:05:23 +02:00
export default class DevServer extends Server {
2019-10-04 18:11:39 +02:00
private devReady : Promise < void >
private setDevReady? : Function
private webpackWatcher? : Watchpack | null
private hotReloader? : HotReloader
2019-11-08 18:03:50 +01:00
private isCustomServer : boolean
2019-10-04 18:11:39 +02:00
2019-11-08 18:03:50 +01:00
constructor ( options : ServerConstructor & { isNextDevCommand? : boolean } ) {
2019-09-24 10:50:04 +02:00
super ( { . . . options , dev : true } )
2018-09-28 14:05:23 +02:00
this . renderOpts . dev = true
2019-10-04 18:11:39 +02:00
; ( this . renderOpts as any ) . ErrorDebug = ErrorDebug
Improve dev experience by listening faster (#5902)
As I detailed in [this thread on Spectrum](https://spectrum.chat/?t=3df7b1fb-7331-4ca4-af35-d9a8b1cacb2c), the dev experience would be a lot nicer if the server started listening as soon as possible, before the slow initialization steps. That way, instead of manually polling the dev URL until the server's up (this can take a long time!), I can open it right away and the responses will be delivered when the dev server is done initializing.
This makes a few changes to the dev server:
* Move `HotReloader` creation to `prepare`. Ideally, more things (from the non-dev `Server`) would be moved to a later point as well, because creating `next({ ... })` is quite slow.
* In `run`, wait for a promise to resolve before doing anything. This promise automatically gets resolved whenever `prepare` finishes successfully.
And the `next dev` and `next start` scripts:
* Since we want to log that the server is ready/listening before the intensive build process kicks off, we return the app instance from `startServer` and the scripts call `app.prepare()`.
This should all be backwards compatible, including with all existing custom server recommendations that essentially say `app.prepare().then(listen)`. But now, we could make an even better recommendation: start listening right away, then call `app.prepare()` in the `listen` callback. Users would be free to make that change and get better DX.
Try it and I doubt you'll want to go back to the old way. :)
2018-12-17 12:09:44 +01:00
this . devReady = new Promise ( resolve = > {
this . setDevReady = resolve
} )
2020-03-24 09:31:04 +01:00
; ( this . renderOpts as any ) . ampSkipValidation =
this . nextConfig . experimental ? . amp ? . skipValidation ? ? false
2019-10-04 18:11:39 +02:00
; ( this . renderOpts as any ) . ampValidator = (
html : string ,
pathname : string
) = > {
2019-11-26 10:47:55 +01:00
const validatorPath =
this . nextConfig . experimental &&
this . nextConfig . experimental . amp &&
this . nextConfig . experimental . amp . validator
return AmpHtmlValidator . getInstance ( validatorPath ) . then ( validator = > {
2019-04-11 20:59:26 +02:00
const result = validator . validateString ( html )
ampValidation (
pathname ,
result . errors
. filter ( e = > e . severity === 'ERROR' )
. filter ( e = > this . _filterAmpDevelopmentScript ( html , e ) ) ,
result . errors . filter ( e = > e . severity !== 'ERROR' )
)
} )
}
2019-10-06 13:57:41 +02:00
if ( fs . existsSync ( join ( this . dir , 'static' ) ) ) {
console . warn (
` The static directory has been deprecated in favor of the public directory. https://err.sh/zeit/next.js/static-dir-deprecated `
)
}
2019-11-08 18:03:50 +01:00
this . isCustomServer = ! options . isNextDevCommand
2019-09-24 17:15:14 +02:00
this . pagesDir = findPagesDir ( this . dir )
2020-02-24 22:36:59 +01:00
this . staticPathsWorker = new Worker (
require . resolve ( './static-paths-worker' ) ,
{
maxRetries : 0 ,
numWorkers : this.nextConfig.experimental.cpus ,
}
) as Worker & {
loadStaticPaths : typeof import ( './static-paths-worker' ) . loadStaticPaths
}
this . staticPathsWorker . getStdout ( ) . pipe ( process . stdout )
this . staticPathsWorker . getStderr ( ) . pipe ( process . stderr )
2018-09-28 14:05:23 +02:00
}
2019-10-04 18:11:39 +02:00
protected currentPhase() {
2018-09-28 14:05:23 +02:00
return PHASE_DEVELOPMENT_SERVER
}
2019-10-04 18:11:39 +02:00
protected readBuildId() {
2018-09-28 14:05:23 +02:00
return 'development'
}
2019-10-04 18:11:39 +02:00
async addExportPathMapRoutes() {
2018-10-01 16:31:47 +02:00
// Makes `next export` exportPathMap work in development mode.
// So that the user doesn't have to define a custom server reading the exportPathMap
if ( this . nextConfig . exportPathMap ) {
console . log ( 'Defining routes from exportPathMap' )
2019-05-27 20:20:33 +02:00
const exportPathMap = await this . nextConfig . exportPathMap (
{ } ,
{
dev : true ,
dir : this.dir ,
outDir : null ,
distDir : this.distDir ,
2019-10-04 18:11:39 +02:00
buildId : this.buildId ,
2019-05-27 20:20:33 +02:00
}
) // In development we can't give a default path mapping
2018-10-01 16:31:47 +02:00
for ( const path in exportPathMap ) {
2019-02-19 22:45:07 +01:00
const { page , query = { } } = exportPathMap [ path ]
2018-10-01 16:31:47 +02:00
// We use unshift so that we're sure the routes is defined before Next's default routes
2020-02-11 00:06:38 +01:00
this . router . addFsRoute ( {
2018-10-01 16:31:47 +02:00
match : route ( path ) ,
2019-11-18 01:12:48 +01:00
type : 'route' ,
name : ` ${ path } exportpathmap route ` ,
2018-10-01 16:31:47 +02:00
fn : async ( req , res , params , parsedUrl ) = > {
const { query : urlQuery } = parsedUrl
Object . keys ( urlQuery )
. filter ( key = > query [ key ] === undefined )
2019-05-27 20:20:33 +02:00
. forEach ( key = >
console . warn (
2019-06-30 00:13:23 +02:00
` Url ' ${ path } ' defines a query parameter ' ${ key } ' that is missing in exportPathMap `
2019-05-27 20:20:33 +02:00
)
)
2018-10-01 16:31:47 +02:00
2019-02-19 22:45:07 +01:00
const mergedQuery = { . . . urlQuery , . . . query }
2018-10-01 16:31:47 +02:00
await this . render ( req , res , page , mergedQuery , parsedUrl )
2019-11-18 01:12:48 +01:00
return {
finished : true ,
}
2019-10-04 18:11:39 +02:00
} ,
2018-10-01 16:31:47 +02:00
} )
}
}
}
2019-10-04 18:11:39 +02:00
async startWatcher() {
2019-06-25 16:28:48 +02:00
if ( this . webpackWatcher ) {
2019-05-27 20:20:33 +02:00
return
}
2020-02-19 17:13:04 +01:00
const regexPageExtension = new RegExp (
` \\ .+(?: ${ this . nextConfig . pageExtensions . join ( '|' ) } ) $ `
)
2019-07-10 17:45:53 +02:00
let resolved = false
2019-05-27 20:20:33 +02:00
return new Promise ( resolve = > {
2019-09-24 17:15:14 +02:00
const pagesDir = this . pagesDir
2019-05-27 20:20:33 +02:00
2019-07-10 17:45:53 +02:00
// Watchpack doesn't emit an event for an empty directory
2019-10-04 18:11:39 +02:00
fs . readdir ( pagesDir ! , ( _ , files ) = > {
2020-01-08 17:30:53 +01:00
if ( files ? . length ) {
2019-07-10 17:45:53 +02:00
return
}
if ( ! resolved ) {
resolve ( )
resolved = true
}
} )
2019-05-27 20:20:33 +02:00
let wp = ( this . webpackWatcher = new Watchpack ( ) )
2019-10-04 18:11:39 +02:00
wp . watch ( [ ] , [ pagesDir ! ] , 0 )
2019-05-27 20:20:33 +02:00
wp . on ( 'aggregated' , ( ) = > {
2019-05-30 22:42:45 +02:00
const dynamicRoutedPages = [ ]
2019-05-27 20:20:33 +02:00
const knownFiles = wp . getTimeInfoEntries ( )
for ( const [ fileName , { accuracy } ] of knownFiles ) {
2020-02-19 17:13:04 +01:00
if ( accuracy === undefined || ! regexPageExtension . test ( fileName ) ) {
2019-05-27 20:20:33 +02:00
continue
}
2019-10-04 18:11:39 +02:00
let pageName =
'/' + relative ( pagesDir ! , fileName ) . replace ( /\\+/g , '/' )
2020-02-19 17:13:04 +01:00
pageName = pageName . replace ( regexPageExtension , '' )
2019-06-21 00:27:04 +02:00
pageName = pageName . replace ( /\/index$/ , '' ) || '/'
if ( ! isDynamicRoute ( pageName ) ) {
continue
}
2019-05-30 22:42:45 +02:00
dynamicRoutedPages . push ( pageName )
2019-05-27 20:20:33 +02:00
}
2019-05-30 22:42:45 +02:00
this . dynamicRoutes = getSortedRoutes ( dynamicRoutedPages ) . map ( page = > ( {
page ,
2019-10-04 18:11:39 +02:00
match : getRouteMatcher ( getRouteRegex ( page ) ) ,
2019-05-30 22:42:45 +02:00
} ) )
2019-12-23 22:20:17 +01:00
this . router . setDynamicRoutes ( this . dynamicRoutes )
2019-05-30 22:42:45 +02:00
2019-07-10 17:45:53 +02:00
if ( ! resolved ) {
resolve ( )
resolved = true
}
2019-05-27 20:20:33 +02:00
} )
} )
}
2019-10-04 18:11:39 +02:00
async stopWatcher() {
2019-05-27 20:20:33 +02:00
if ( ! this . webpackWatcher ) {
return
}
this . webpackWatcher . close ( )
this . webpackWatcher = null
}
2019-10-04 18:11:39 +02:00
async prepare() {
await verifyTypeScriptSetup ( this . dir , this . pagesDir ! )
2019-11-09 23:34:53 +01:00
await this . loadCustomRoutes ( )
if ( this . customRoutes ) {
const { redirects , rewrites } = this . customRoutes
if ( redirects . length || rewrites . length ) {
2019-12-23 22:20:17 +01:00
this . router = new Router ( this . generateRoutes ( ) )
2019-11-09 23:34:53 +01:00
}
}
2019-05-09 04:51:23 +02:00
2019-05-27 20:20:33 +02:00
this . hotReloader = new HotReloader ( this . dir , {
2019-10-04 18:11:39 +02:00
pagesDir : this.pagesDir ! ,
2019-05-27 20:20:33 +02:00
config : this.nextConfig ,
2020-02-12 02:16:42 +01:00
previewProps : this.getPreviewProps ( ) ,
2019-10-04 18:11:39 +02:00
buildId : this.buildId ,
2019-05-27 20:20:33 +02:00
} )
2018-09-28 14:05:23 +02:00
await super . prepare ( )
2018-10-01 16:31:47 +02:00
await this . addExportPathMapRoutes ( )
2018-09-28 14:32:26 +02:00
await this . hotReloader . start ( )
2019-05-27 20:20:33 +02:00
await this . startWatcher ( )
2019-10-04 18:11:39 +02:00
this . setDevReady ! ( )
2019-08-29 18:43:06 +02:00
2019-10-10 19:18:07 +02:00
const telemetry = new Telemetry ( { distDir : this.distDir } )
2019-10-23 04:42:16 +02:00
telemetry . record (
2020-02-14 21:42:44 +01:00
eventCliSession ( PHASE_DEVELOPMENT_SERVER , this . distDir , {
2019-10-23 04:42:16 +02:00
cliCommand : 'dev' ,
isSrcDir : relative ( this . dir , this . pagesDir ! ) . startsWith ( 'src' ) ,
2019-11-08 18:03:50 +01:00
hasNowJson : ! ! ( await findUp ( 'now.json' , { cwd : this.dir } ) ) ,
isCustomServer : this.isCustomServer ,
2019-10-23 04:42:16 +02:00
} )
)
2018-09-28 14:05:23 +02:00
}
2019-10-04 18:11:39 +02:00
protected async close() {
2019-05-27 20:20:33 +02:00
await this . stopWatcher ( )
Improve dev experience by listening faster (#5902)
As I detailed in [this thread on Spectrum](https://spectrum.chat/?t=3df7b1fb-7331-4ca4-af35-d9a8b1cacb2c), the dev experience would be a lot nicer if the server started listening as soon as possible, before the slow initialization steps. That way, instead of manually polling the dev URL until the server's up (this can take a long time!), I can open it right away and the responses will be delivered when the dev server is done initializing.
This makes a few changes to the dev server:
* Move `HotReloader` creation to `prepare`. Ideally, more things (from the non-dev `Server`) would be moved to a later point as well, because creating `next({ ... })` is quite slow.
* In `run`, wait for a promise to resolve before doing anything. This promise automatically gets resolved whenever `prepare` finishes successfully.
And the `next dev` and `next start` scripts:
* Since we want to log that the server is ready/listening before the intensive build process kicks off, we return the app instance from `startServer` and the scripts call `app.prepare()`.
This should all be backwards compatible, including with all existing custom server recommendations that essentially say `app.prepare().then(listen)`. But now, we could make an even better recommendation: start listening right away, then call `app.prepare()` in the `listen` callback. Users would be free to make that change and get better DX.
Try it and I doubt you'll want to go back to the old way. :)
2018-12-17 12:09:44 +01:00
if ( this . hotReloader ) {
await this . hotReloader . stop ( )
}
2018-09-28 14:05:23 +02:00
}
2019-12-10 16:08:42 +01:00
protected async hasPage ( pathname : string ) : Promise < boolean > {
const pageFile = await findPageFile (
this . pagesDir ! ,
normalizePagePath ( pathname ) ,
this . nextConfig . pageExtensions
)
return ! ! pageFile
}
2019-11-18 01:12:48 +01:00
protected async _beforeCatchAllRender (
2019-10-04 18:11:39 +02:00
req : IncomingMessage ,
res : ServerResponse ,
2020-01-10 03:56:05 +01:00
params : Params ,
2019-10-04 18:11:39 +02:00
parsedUrl : UrlWithParsedQuery
) {
2019-09-16 23:06:30 +02:00
const { pathname } = parsedUrl
2020-03-26 17:58:15 +01:00
const pathParts = params . path || [ ]
const path = ` / ${ pathParts . join ( '/' ) } `
2019-09-16 23:06:30 +02:00
// check for a public file, throwing error if there's a
// conflicting page
2020-01-10 03:56:05 +01:00
if ( await this . hasPublicFile ( path ) ) {
2019-12-10 16:08:42 +01:00
if ( await this . hasPage ( pathname ! ) ) {
2019-10-06 13:44:03 +02:00
const err = new Error (
` A conflicting public file and page file was found for path ${ pathname } https://err.sh/zeit/next.js/conflicting-public-file-page `
2019-09-16 23:06:30 +02:00
)
2019-10-06 13:44:03 +02:00
res . statusCode = 500
2019-11-18 01:12:48 +01:00
await this . renderError ( err , req , res , pathname ! , { } )
return true
2019-09-16 23:06:30 +02:00
}
2020-03-26 17:58:15 +01:00
await this . servePublic ( req , res , pathParts )
2019-11-18 01:12:48 +01:00
return true
}
return false
}
async run (
req : IncomingMessage ,
res : ServerResponse ,
parsedUrl : UrlWithParsedQuery
) {
await this . devReady
const { pathname } = parsedUrl
if ( pathname ! . startsWith ( '/_next' ) ) {
try {
await fsStat ( join ( this . publicDir , '_next' ) )
throw new Error ( PUBLIC_DIR_MIDDLEWARE_CONFLICT )
} catch ( err ) { }
2019-09-16 23:06:30 +02:00
}
2019-10-04 18:11:39 +02:00
const { finished } = ( await this . hotReloader ! . run ( req , res , parsedUrl ) ) || {
finished : false ,
}
2018-09-28 14:32:26 +02:00
if ( finished ) {
return
2018-09-28 14:05:23 +02:00
}
return super . run ( req , res , parsedUrl )
}
2019-11-09 23:34:53 +01:00
// override production loading of routes-manifest
protected getCustomRoutes() {
return this . customRoutes
}
2020-02-12 02:16:42 +01:00
private _devCachedPreviewProps : __ApiPreviewProps | undefined
protected getPreviewProps() {
if ( this . _devCachedPreviewProps ) {
return this . _devCachedPreviewProps
}
return ( this . _devCachedPreviewProps = {
previewModeId : crypto.randomBytes ( 16 ) . toString ( 'hex' ) ,
previewModeSigningKey : crypto.randomBytes ( 32 ) . toString ( 'hex' ) ,
previewModeEncryptionKey : crypto.randomBytes ( 32 ) . toString ( 'hex' ) ,
} )
}
2019-11-09 23:34:53 +01:00
private async loadCustomRoutes() {
const result = {
redirects : [ ] ,
rewrites : [ ] ,
2020-01-01 13:47:58 +01:00
headers : [ ] ,
2019-11-09 23:34:53 +01:00
}
2020-01-01 13:47:58 +01:00
const { redirects , rewrites , headers } = this . nextConfig . experimental
2019-11-09 23:34:53 +01:00
if ( typeof redirects === 'function' ) {
result . redirects = await redirects ( )
2019-12-10 15:54:56 +01:00
checkCustomRoutes ( result . redirects , 'redirect' )
2019-11-09 23:34:53 +01:00
}
if ( typeof rewrites === 'function' ) {
result . rewrites = await rewrites ( )
2019-12-10 15:54:56 +01:00
checkCustomRoutes ( result . rewrites , 'rewrite' )
2019-11-09 23:34:53 +01:00
}
2020-01-01 13:47:58 +01:00
if ( typeof headers === 'function' ) {
result . headers = await headers ( )
checkCustomRoutes ( result . headers , 'header' )
}
2019-11-09 23:34:53 +01:00
this . customRoutes = result
}
2019-10-04 18:11:39 +02:00
generateRoutes() {
2020-02-11 00:06:38 +01:00
const { fsRoutes , . . . otherRoutes } = super . generateRoutes ( )
2018-09-28 14:05:23 +02:00
// In development we expose all compiled files for react-error-overlay's line show feature
// We use unshift so that we're sure the routes is defined before Next's default routes
2019-12-23 22:20:17 +01:00
fsRoutes . unshift ( {
2018-10-01 16:31:47 +02:00
match : route ( '/_next/development/:path*' ) ,
2019-11-18 01:12:48 +01:00
type : 'route' ,
name : '_next/development catchall' ,
2018-09-28 14:05:23 +02:00
fn : async ( req , res , params ) = > {
const p = join ( this . distDir , . . . ( params . path || [ ] ) )
await this . serveStatic ( req , res , p )
2019-11-18 01:12:48 +01:00
return {
finished : true ,
}
2019-10-04 18:11:39 +02:00
} ,
2018-09-28 14:05:23 +02:00
} )
2020-02-11 00:06:38 +01:00
fsRoutes . push ( {
match : route ( '/:path*' ) ,
type : 'route' ,
name : 'Catchall public directory route' ,
fn : async ( req , res , params , parsedUrl ) = > {
const { pathname } = parsedUrl
if ( ! pathname ) {
throw new Error ( 'pathname is undefined' )
}
// Used in development to check public directory paths
if ( await this . _beforeCatchAllRender ( req , res , params , parsedUrl ) ) {
return {
finished : true ,
}
}
return {
finished : false ,
}
} ,
} )
return { fsRoutes , . . . otherRoutes }
2018-09-28 14:05:23 +02:00
}
2019-05-03 18:57:47 +02:00
// In development public files are not added to the router but handled as a fallback instead
2019-10-04 18:11:39 +02:00
protected generatePublicRoutes() {
2019-05-03 18:57:47 +02:00
return [ ]
}
2019-05-27 20:20:33 +02:00
// In development dynamic routes cannot be known ahead of time
2019-10-04 18:11:39 +02:00
protected getDynamicRoutes() {
2019-05-27 20:20:33 +02:00
return [ ]
}
2019-10-04 18:11:39 +02:00
_filterAmpDevelopmentScript (
html : string ,
event : { line : number ; col : number ; code : string }
) {
2019-03-19 00:21:18 +01:00
if ( event . code !== 'DISALLOWED_SCRIPT_TAG' ) {
return true
}
const snippetChunks = html . split ( '\n' )
let snippet
if (
! ( snippet = html . split ( '\n' ) [ event . line - 1 ] ) ||
! ( snippet = snippet . substring ( event . col ) )
) {
return true
}
snippet = snippet + snippetChunks . slice ( event . line ) . join ( '\n' )
snippet = snippet . substring ( 0 , snippet . indexOf ( '</script>' ) )
return ! snippet . includes ( 'data-amp-development-mode-only' )
}
2019-12-10 16:08:42 +01:00
protected async ensureApiPage ( pathname : string ) {
return this . hotReloader ! . ensurePage ( pathname )
2019-05-11 13:18:56 +02:00
}
2019-10-04 18:11:39 +02:00
async renderToHTML (
req : IncomingMessage ,
res : ServerResponse ,
pathname : string ,
2020-03-01 18:26:31 +01:00
query : { [ key : string ] : string }
2019-10-04 18:11:39 +02:00
) {
2018-09-28 14:05:23 +02:00
const compilationErr = await this . getCompilationError ( pathname )
if ( compilationErr ) {
res . statusCode = 500
return this . renderErrorToHTML ( compilationErr , req , res , pathname , query )
}
2018-11-03 01:24:43 +01:00
// In dev mode we use on demand entries to compile the page before rendering
try {
2019-10-04 18:11:39 +02:00
await this . hotReloader ! . ensurePage ( pathname ) . catch ( async ( err : Error ) = > {
if ( ( err as any ) . code !== 'ENOENT' ) {
2019-09-16 23:06:30 +02:00
throw err
2019-05-27 20:20:33 +02:00
}
2019-10-04 18:11:39 +02:00
for ( const dynamicRoute of this . dynamicRoutes || [ ] ) {
2019-05-27 20:20:33 +02:00
const params = dynamicRoute . match ( pathname )
if ( ! params ) {
continue
}
2019-12-13 19:14:09 +01:00
return this . hotReloader ! . ensurePage ( dynamicRoute . page )
2019-05-27 20:20:33 +02:00
}
2019-09-16 23:06:30 +02:00
throw err
2019-05-27 20:20:33 +02:00
} )
2018-11-03 01:24:43 +01:00
} catch ( err ) {
if ( err . code === 'ENOENT' ) {
2020-02-19 20:54:38 +01:00
try {
await this . hotReloader ! . ensurePage ( '/404' )
} catch ( err ) {
if ( err . code !== 'ENOENT' ) {
throw err
2020-02-01 15:47:42 +01:00
}
}
2019-09-16 23:06:30 +02:00
res . statusCode = 404
return this . renderErrorToHTML ( null , req , res , pathname , query )
2018-11-03 01:24:43 +01:00
}
if ( ! this . quiet ) console . error ( err )
}
2020-03-01 18:26:31 +01:00
const html = await super . renderToHTML ( req , res , pathname , query )
2019-03-19 00:21:18 +01:00
return html
2018-09-28 14:05:23 +02:00
}
2019-10-04 18:11:39 +02:00
async renderErrorToHTML (
err : Error | null ,
req : IncomingMessage ,
res : ServerResponse ,
pathname : string ,
query : { [ key : string ] : string }
) {
await this . hotReloader ! . ensurePage ( '/_error' )
2019-02-03 15:34:28 +01:00
2018-09-28 14:05:23 +02:00
const compilationErr = await this . getCompilationError ( pathname )
if ( compilationErr ) {
res . statusCode = 500
return super . renderErrorToHTML ( compilationErr , req , res , pathname , query )
}
2019-03-13 19:39:05 +01:00
if ( ! err && res . statusCode === 500 ) {
2019-03-19 00:21:18 +01:00
err = new Error (
'An undefined error was thrown sometime during render... ' +
'See https://err.sh/zeit/next.js/threw-undefined'
2019-03-13 21:08:06 +01:00
)
2019-03-13 19:39:05 +01:00
}
2018-09-28 14:05:23 +02:00
try {
const out = await super . renderErrorToHTML ( err , req , res , pathname , query )
return out
} catch ( err2 ) {
2019-04-09 17:52:03 +02:00
if ( ! this . quiet ) Log . error ( err2 )
2018-09-28 14:05:23 +02:00
res . statusCode = 500
return super . renderErrorToHTML ( err2 , req , res , pathname , query )
}
}
2019-10-04 18:11:39 +02:00
sendHTML ( req : IncomingMessage , res : ServerResponse , html : string ) {
2018-12-09 22:46:45 +01:00
// In dev, we should not cache pages for any reason.
res . setHeader ( 'Cache-Control' , 'no-store, must-revalidate' )
return super . sendHTML ( req , res , html )
}
2019-10-04 18:11:39 +02:00
protected setImmutableAssetCacheControl ( res : ServerResponse ) {
2018-09-28 14:05:23 +02:00
res . setHeader ( 'Cache-Control' , 'no-store, must-revalidate' )
}
2020-03-26 17:58:15 +01:00
private servePublic (
req : IncomingMessage ,
res : ServerResponse ,
pathParts : string [ ]
) {
const p = join ( this . publicDir , . . . pathParts . map ( encodeURIComponent ) )
2019-05-03 18:57:47 +02:00
return this . serveStatic ( req , res , p )
}
2019-10-04 18:11:39 +02:00
async hasPublicFile ( path : string ) {
2019-09-16 23:06:30 +02:00
try {
const info = await fsStat ( join ( this . publicDir , path ) )
return info . isFile ( )
} catch ( _ ) {
return false
}
}
2019-10-04 18:11:39 +02:00
async getCompilationError ( page : string ) {
const errors = await this . hotReloader ! . getCompilationErrors ( page )
2018-09-28 14:05:23 +02:00
if ( errors . length === 0 ) return
// Return the very first error we found.
return errors [ 0 ]
}
2020-03-26 17:58:15 +01:00
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
}
// During development mode, files can be added while the server is running.
// Checks for .next/static, .next/server, static and public.
// Note that in development .next/server is available for error reporting purposes.
// see `packages/next/next-server/server/next-server.ts` for more details.
if (
untrustedFilePath . startsWith ( join ( this . distDir , 'static' ) + sep ) ||
untrustedFilePath . startsWith ( join ( this . distDir , 'server' ) + sep ) ||
untrustedFilePath . startsWith ( join ( this . dir , 'static' ) + sep ) ||
untrustedFilePath . startsWith ( join ( this . dir , 'public' ) + sep )
) {
return true
}
return false
}
2018-09-28 14:05:23 +02:00
}