2020-05-04 23:16:03 +02:00
import { ReactDevOverlay } from '@next/react-dev-overlay/lib/client'
2020-02-12 02:16:42 +01:00
import crypto from 'crypto'
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'
2020-05-04 23:16:03 +02:00
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import findUp from 'next/dist/compiled/find-up'
2020-06-01 23:00:22 +02:00
import { join as pathJoin , relative , resolve as pathResolve , sep } from 'path'
2019-10-04 18:11:39 +02:00
import React from 'react'
import { UrlWithParsedQuery } from 'url'
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'
2019-10-04 18:11:39 +02:00
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
2020-06-10 22:35:34 +02:00
import { fileExists } from '../lib/file-exists'
2019-10-04 18:11:39 +02:00
import { findPagesDir } from '../lib/find-pages-dir'
2020-06-10 22:35:34 +02:00
import loadCustomRoutes , { CustomRoutes } from '../lib/load-custom-routes'
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 HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
2020-04-01 10:12:38 +02:00
import { getNodeOptionsWithoutInspect } from './lib/utils'
2020-07-01 16:59:18 +02:00
import { withCoalescedInvoke } from '../lib/coalesced-function'
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-05-27 23:51:11 +02: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/vercel/next.js/invalid-react-version `
2019-05-27 20:20:33 +02:00
)
2019-04-04 23:47:17 +02:00
}
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
2020-07-01 16:59:18 +02:00
protected staticPathsWorker : import ( 'jest-worker' ) . default & {
loadStaticPaths : typeof import ( './static-paths-worker' ) . loadStaticPaths
}
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
2020-05-15 20:14:44 +02:00
; ( this . renderOpts as any ) . ErrorDebug = ReactDevOverlay
2020-05-18 21:24:37 +02:00
this . devReady = new Promise ( ( resolve ) = > {
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 . 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
2020-05-18 21:24:37 +02:00
return AmpHtmlValidator . getInstance ( validatorPath ) . then ( ( validator ) = > {
2019-04-11 20:59:26 +02:00
const result = validator . validateString ( html )
ampValidation (
pathname ,
result . errors
2020-05-18 21:24:37 +02:00
. filter ( ( e ) = > e . severity === 'ERROR' )
. filter ( ( e ) = > this . _filterAmpDevelopmentScript ( html , e ) ) ,
result . errors . filter ( ( e ) = > e . severity !== 'ERROR' )
2019-04-11 20:59:26 +02:00
)
} )
}
2020-06-01 23:00:22 +02:00
if ( fs . existsSync ( pathJoin ( this . dir , 'static' ) ) ) {
2019-10-06 13:57:41 +02:00
console . warn (
2020-05-27 23:51:11 +02:00
` The static directory has been deprecated in favor of the public directory. https://err.sh/vercel/next.js/static-dir-deprecated `
2019-10-06 13:57:41 +02:00
)
}
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 ,
2020-04-01 10:12:38 +02:00
forkOptions : {
env : {
. . . process . env ,
// discard --inspect/--inspect-brk flags from process.env.NODE_OPTIONS. Otherwise multiple Node.js debuggers
// would be started if user launch Next.js in debugging mode. The number of debuggers is linked to
// the number of workers Next.js tries to launch. The only worker users are interested in debugging
// is the main Next.js one
NODE_OPTIONS : getNodeOptionsWithoutInspect ( ) ,
} ,
} ,
2020-02-24 22:36:59 +01:00
}
) 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
}
2020-05-18 19:31:06 +02:00
protected currentPhase ( ) : string {
2018-09-28 14:05:23 +02:00
return PHASE_DEVELOPMENT_SERVER
}
2020-05-18 19:31:06 +02:00
protected readBuildId ( ) : string {
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 ` ,
2020-06-03 17:58:58 +02:00
fn : async ( req , res , _params , parsedUrl ) = > {
2018-10-01 16:31:47 +02:00
const { query : urlQuery } = parsedUrl
Object . keys ( urlQuery )
2020-05-18 21:24:37 +02:00
. filter ( ( key ) = > query [ key ] === undefined )
. forEach ( ( key ) = >
2019-05-27 20:20:33 +02:00
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
} )
}
}
}
2020-05-18 19:31:06 +02:00
async startWatcher ( ) : Promise < void > {
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
2020-05-18 15:47:13 +02:00
return new Promise ( ( resolve , reject ) = > {
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' , ( ) = > {
2020-05-18 15:47:13 +02:00
const routedPages = [ ]
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$/ , '' ) || '/'
2020-05-18 15:47:13 +02:00
routedPages . push ( pageName )
2019-05-27 20:20:33 +02:00
}
2020-05-18 15:47:13 +02:00
try {
this . dynamicRoutes = getSortedRoutes ( routedPages )
. filter ( isDynamicRoute )
2020-05-18 21:24:37 +02:00
. map ( ( page ) = > ( {
2020-05-18 15:47:13 +02:00
page ,
match : getRouteMatcher ( getRouteRegex ( page ) ) ,
} ) )
2019-05-27 20:20:33 +02:00
2020-05-18 15:47:13 +02:00
this . router . setDynamicRoutes ( this . dynamicRoutes )
if ( ! resolved ) {
resolve ( )
resolved = true
}
} catch ( e ) {
if ( ! resolved ) {
reject ( e )
resolved = true
} else {
console . warn ( 'Failed to reload dynamic routes:' , e )
}
2019-07-10 17:45:53 +02:00
}
2019-05-27 20:20:33 +02:00
} )
} )
}
2020-05-18 19:31:06 +02:00
async stopWatcher ( ) : Promise < void > {
2019-05-27 20:20:33 +02:00
if ( ! this . webpackWatcher ) {
return
}
this . webpackWatcher . close ( )
this . webpackWatcher = null
}
2020-05-18 19:31:06 +02:00
async prepare ( ) : Promise < void > {
2020-05-29 10:16:22 +02:00
await verifyTypeScriptSetup ( this . dir , this . pagesDir ! , false )
2019-11-09 23:34:53 +01:00
2020-06-09 22:16:23 +02:00
this . customRoutes = await loadCustomRoutes ( this . nextConfig )
2019-11-09 23:34:53 +01:00
2020-06-09 22:16:23 +02:00
// reload router
const { redirects , rewrites , headers } = this . customRoutes
if ( redirects . length || rewrites . length || headers . length ) {
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
}
2020-05-18 19:31:06 +02:00
protected async close ( ) : Promise < void > {
2019-05-27 20:20:33 +02:00
await this . stopWatcher ( )
2020-07-17 11:07:48 +02:00
await this . staticPathsWorker . end ( )
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 > {
2020-05-04 18:58:19 +02:00
let normalizedPath : string
try {
normalizedPath = normalizePagePath ( pathname )
} catch ( err ) {
console . error ( err )
// if normalizing the page fails it means it isn't valid
// so it doesn't exist so don't throw and return false
// to ensure we return 404 instead of 500
return false
}
2019-12-10 16:08:42 +01:00
const pageFile = await findPageFile (
this . pagesDir ! ,
2020-05-04 18:58:19 +02:00
normalizedPath ,
2019-12-10 16:08:42 +01:00
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
2020-05-18 19:31:06 +02:00
) : Promise < boolean > {
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 (
2020-05-27 23:51:11 +02:00
` A conflicting public file and page file was found for path ${ pathname } https://err.sh/vercel/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
2020-05-18 19:31:06 +02:00
) : Promise < void > {
2019-11-18 01:12:48 +01:00
await this . devReady
2020-07-12 21:03:49 +02:00
const { basePath } = this . nextConfig
2020-07-13 16:59:40 +02:00
let originalPathname : string | null = null
2020-07-12 21:03:49 +02:00
if ( basePath && parsedUrl . pathname ? . startsWith ( basePath ) ) {
// strip basePath before handling dev bundles
// If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
2020-07-13 16:59:40 +02:00
originalPathname = parsedUrl . pathname
parsedUrl . pathname = parsedUrl . pathname ! . slice ( basePath . length ) || '/'
2020-07-12 21:03:49 +02:00
}
2019-11-18 01:12:48 +01:00
const { pathname } = parsedUrl
if ( pathname ! . startsWith ( '/_next' ) ) {
2020-06-10 22:35:34 +02:00
if ( await fileExists ( pathJoin ( this . publicDir , '_next' ) ) ) {
2019-11-18 01:12:48 +01:00
throw new Error ( PUBLIC_DIR_MIDDLEWARE_CONFLICT )
2020-06-10 22:35:34 +02:00
}
2019-09-16 23:06:30 +02:00
}
2020-07-13 16:59:40 +02:00
const { finished = false } = await this . hotReloader ! . run (
req ,
res ,
parsedUrl
)
2018-09-28 14:32:26 +02:00
if ( finished ) {
return
2018-09-28 14:05:23 +02:00
}
2020-07-13 16:59:40 +02:00
if ( originalPathname ) {
// restore the path before continuing so that custom-routes can accurately determine
// if they should match against the basePath or not
parsedUrl . pathname = originalPathname
2020-07-12 21:03:49 +02:00
}
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
2020-06-09 22:16:23 +02:00
protected getCustomRoutes ( ) : CustomRoutes {
// actual routes will be loaded asynchronously during .prepare()
return { redirects : [ ] , rewrites : [ ] , headers : [ ] }
2019-11-09 23:34:53 +01:00
}
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-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 ) = > {
2020-06-01 23:00:22 +02:00
const p = pathJoin ( this . distDir , . . . ( params . path || [ ] ) )
2018-09-28 14:05:23 +02:00
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' ,
2020-07-12 21:03:49 +02:00
requireBasePath : false ,
name : 'catchall public directory route' ,
2020-02-11 00:06:38 +01:00
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
2020-05-18 19:31:06 +02:00
protected generatePublicRoutes ( ) : never [ ] {
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
2020-05-18 19:31:06 +02:00
protected getDynamicRoutes ( ) : never [ ] {
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 }
2020-05-18 19:31:06 +02:00
) : boolean {
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' )
}
2020-07-01 16:59:18 +02:00
protected async getStaticPaths (
pathname : string
) : Promise < {
staticPaths : string [ ] | undefined
hasStaticFallback : boolean
} > {
// we lazy load the staticPaths to prevent the user
// from waiting on them for the page to load in dev mode
const __getStaticPaths = async ( ) = > {
const paths = await this . staticPathsWorker . loadStaticPaths (
this . distDir ,
pathname ,
! this . renderOpts . dev && this . _isLikeServerless
)
return paths
}
const { paths : staticPaths , fallback : hasStaticFallback } = (
await withCoalescedInvoke ( __getStaticPaths ) ( ` staticPaths- ${ pathname } ` , [ ] )
) . value
return { staticPaths , hasStaticFallback }
}
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 }
2020-05-18 19:31:06 +02:00
) : Promise < string | null > {
2020-06-26 06:26:09 +02:00
await this . devReady
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' )
2020-06-01 23:00:22 +02:00
} catch ( hotReloaderError ) {
if ( hotReloaderError . code !== 'ENOENT' ) {
throw hotReloaderError
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 }
2020-05-18 19:31:06 +02:00
) : Promise < string | null > {
2020-06-26 06:26:09 +02:00
await this . devReady
2020-04-22 07:36:49 +02:00
if ( res . statusCode === 404 && ( await this . hasPage ( '/404' ) ) ) {
await this . hotReloader ! . ensurePage ( '/404' )
} else {
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... ' +
2020-05-27 23:51:11 +02:00
'See https://err.sh/vercel/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 )
}
}
2020-05-18 19:31:06 +02:00
sendHTML (
req : IncomingMessage ,
res : ServerResponse ,
html : string
) : Promise < void > {
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 )
}
2020-05-18 19:31:06 +02:00
protected setImmutableAssetCacheControl ( res : ServerResponse ) : void {
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 [ ]
2020-05-18 19:31:06 +02:00
) : Promise < void > {
2020-06-01 23:00:22 +02:00
const p = pathJoin ( this . publicDir , . . . pathParts . map ( encodeURIComponent ) )
2019-05-03 18:57:47 +02:00
return this . serveStatic ( req , res , p )
}
2020-05-18 19:31:06 +02:00
async hasPublicFile ( path : string ) : Promise < boolean > {
2019-09-16 23:06:30 +02:00
try {
2020-06-01 23:00:22 +02:00
const info = await fs . promises . stat ( pathJoin ( this . publicDir , path ) )
2019-09-16 23:06:30 +02:00
return info . isFile ( )
} catch ( _ ) {
return false
}
}
2020-05-18 19:31:06 +02:00
async getCompilationError ( page : string ) : Promise < any > {
2019-10-04 18:11:39 +02:00
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
2020-06-01 23:00:22 +02:00
const untrustedFilePath = pathResolve ( decodedUntrustedFilePath )
2020-03-26 17:58:15 +01:00
// 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 (
2020-06-01 23:00:22 +02:00
untrustedFilePath . startsWith ( pathJoin ( this . distDir , 'static' ) + sep ) ||
untrustedFilePath . startsWith ( pathJoin ( this . distDir , 'server' ) + sep ) ||
untrustedFilePath . startsWith ( pathJoin ( this . dir , 'static' ) + sep ) ||
untrustedFilePath . startsWith ( pathJoin ( this . dir , 'public' ) + sep )
2020-03-26 17:58:15 +01:00
) {
return true
}
return false
}
2018-09-28 14:05:23 +02:00
}