rsnext/packages/next/server/next-dev-server.ts

480 lines
14 KiB
TypeScript
Raw Normal View History

import AmpHtmlValidator from 'amphtml-validator'
import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http'
import { join, relative } from 'path'
import React from 'react'
import { UrlWithParsedQuery } from 'url'
import { promisify } from 'util'
import Watchpack from 'watchpack'
import findUp from 'find-up'
import { ampValidation } from '../build/output/index'
import * as Log from '../build/output/log'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
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,
isDynamicRoute,
} from '../next-server/lib/router/utils'
import Server, { ServerConstructor } from '../next-server/server/next-server'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
import { route } from '../next-server/server/router'
import { eventVersion } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import ErrorDebug from './error-debug'
import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
if (typeof React.Suspense === 'undefined') {
throw new Error(
`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 --save react react-dom" https://err.sh/zeit/next.js/invalid-react-version`
)
}
const fsStat = promisify(fs.stat)
export default class DevServer extends Server {
private devReady: Promise<void>
private setDevReady?: Function
private webpackWatcher?: Watchpack | null
private hotReloader?: HotReloader
private isCustomServer: boolean
constructor(options: ServerConstructor & { isNextDevCommand?: boolean }) {
Add experimental SPR support (#8832) * initial commit for SPRv2 * Add initial SPR cache handling * update SPR handling * Implement SPR handling in render * Update tests, handle caching with serverless next start, add TODOs, and update manifest generating * Handle no prerender-manifest from not being used * Fix url.parse error * Apply suggestions from code review Co-Authored-By: Joe Haddad <joe.haddad@zeit.co> * Replace set with constants in next-page-config * simplify sprStatus.used * Add error if getStaticProps is used with getInitialProps * Remove stale TODO * Update revalidate values in SPR cache for non-seeded routes * Apply suggestions from code review * Remove concurrency type * Rename variable for clarity * Add copying prerender files during export * Add comment for clarity * Fix exporting * Update comment * Add additional note * Rename variable * Update to not re-export SPR pages from build * Hard navigate when fetching data fails * Remove default extension * Add brackets * Add checking output files to prerender tests * Adjust export move logic * Clarify behavior of export aggregation * Update variable names for clarity * Update tests * Add comment * s/an oxymoron/contradictory/ * rename * Extract error case * Add tests for exporting SPR pages and update /_next/data endpoint to end with .json * Relocate variable * Adjust route building * Rename to unstable * Rename unstable_getStaticParams * Fix linting * Only add this when a data request * Update prerender data tests * s/isServerless/isLikeServerless/ * Don't rely on query for `next start` in serverless mode * Rename var * Update renderedDuringBuild check * Add test for dynamic param with bracket * Fix serverless next start handling * remove todo * Adjust comment * Update calculateRevalidate * Remove cache logic from render.tsx * Remove extra imports * Move SPR cache logic to next-server * Remove old isDynamic prop * Add calling App getInitialProps for SPR pages * Update revalidate logic * Add isStale to SprCacheValue * Update headers for SPR * add awaiting pendingRevalidation * Dont return null for revalidation render * Adjust logic * Be sure to remove coalesced render * Fix data for serverless * Create a method coalescing utility * Remove TODO * Extract send payload helper * Wrap in-line * Move around some code * Add tests for de-duping and revalidating * Update prerender manifest test
2019-09-24 10:50:04 +02:00
super({ ...options, dev: true })
this.renderOpts.dev = true
;(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
})
;(this.renderOpts as any).ampValidator = (
html: string,
pathname: string
) => {
return AmpHtmlValidator.getInstance().then(validator => {
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')
)
})
}
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`
)
}
this.isCustomServer = !options.isNextDevCommand
this.pagesDir = findPagesDir(this.dir)
}
protected currentPhase() {
return PHASE_DEVELOPMENT_SERVER
}
protected readBuildId() {
return 'development'
}
async addExportPathMapRoutes() {
// 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')
const exportPathMap = await this.nextConfig.exportPathMap(
{},
{
dev: true,
dir: this.dir,
outDir: null,
distDir: this.distDir,
buildId: this.buildId,
}
) // In development we can't give a default path mapping
for (const path in exportPathMap) {
const { page, query = {} } = exportPathMap[path]
// We use unshift so that we're sure the routes is defined before Next's default routes
this.router.add({
match: route(path),
fn: async (req, res, params, parsedUrl) => {
const { query: urlQuery } = parsedUrl
Object.keys(urlQuery)
.filter(key => query[key] === undefined)
.forEach(key =>
console.warn(
`Url '${path}' defines a query parameter '${key}' that is missing in exportPathMap`
)
)
const mergedQuery = { ...urlQuery, ...query }
await this.render(req, res, page, mergedQuery, parsedUrl)
},
})
}
}
}
async startWatcher() {
2019-06-25 16:28:48 +02:00
if (this.webpackWatcher) {
return
}
let resolved = false
return new Promise(resolve => {
const pagesDir = this.pagesDir
// Watchpack doesn't emit an event for an empty directory
fs.readdir(pagesDir!, (_, files) => {
if (files && files.length) {
return
}
if (!resolved) {
resolve()
resolved = true
}
})
let wp = (this.webpackWatcher = new Watchpack())
wp.watch([], [pagesDir!], 0)
wp.on('aggregated', () => {
const dynamicRoutedPages = []
const knownFiles = wp.getTimeInfoEntries()
for (const [fileName, { accuracy }] of knownFiles) {
if (accuracy === undefined) {
continue
}
let pageName =
'/' + relative(pagesDir!, fileName).replace(/\\+/g, '/')
pageName = pageName.replace(
new RegExp(`\\.+(?:${this.nextConfig.pageExtensions.join('|')})$`),
''
)
pageName = pageName.replace(/\/index$/, '') || '/'
if (!isDynamicRoute(pageName)) {
continue
}
dynamicRoutedPages.push(pageName)
}
this.dynamicRoutes = getSortedRoutes(dynamicRoutedPages).map(page => ({
page,
match: getRouteMatcher(getRouteRegex(page)),
}))
if (!resolved) {
resolve()
resolved = true
}
})
})
}
async stopWatcher() {
if (!this.webpackWatcher) {
return
}
this.webpackWatcher.close()
this.webpackWatcher = null
}
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) {
// TODO: don't reach into router instance
this.router.routes = this.generateRoutes()
}
}
this.hotReloader = new HotReloader(this.dir, {
pagesDir: this.pagesDir!,
config: this.nextConfig,
buildId: this.buildId,
})
await super.prepare()
await this.addExportPathMapRoutes()
await this.hotReloader.start()
await this.startWatcher()
this.setDevReady!()
const telemetry = new Telemetry({ distDir: this.distDir })
telemetry.record(
eventVersion({
cliCommand: 'dev',
isSrcDir: relative(this.dir, this.pagesDir!).startsWith('src'),
hasNowJson: !!(await findUp('now.json', { cwd: this.dir })),
isCustomServer: this.isCustomServer,
})
)
}
protected async close() {
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()
}
}
async run(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
) {
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
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) {}
}
// check for a public file, throwing error if there's a
// conflicting page
if (await this.hasPublicFile(pathname!)) {
const pageFile = await findPageFile(
this.pagesDir!,
normalizePagePath(pathname!),
this.nextConfig.pageExtensions
)
if (pageFile) {
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`
)
res.statusCode = 500
return this.renderError(err, req, res, pathname!, {})
}
return this.servePublic(req, res, pathname!)
}
const { finished } = (await this.hotReloader!.run(req, res, parsedUrl)) || {
finished: false,
}
if (finished) {
return
}
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
}
private async loadCustomRoutes() {
const result = {
redirects: [],
rewrites: [],
}
const { redirects, rewrites } = this.nextConfig.experimental
if (typeof redirects === 'function') {
result.redirects = await redirects()
}
if (typeof rewrites === 'function') {
result.rewrites = await rewrites()
}
this.customRoutes = result
}
generateRoutes() {
const routes = super.generateRoutes()
// 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
routes.unshift({
match: route('/_next/development/:path*'),
fn: async (req, res, params) => {
const p = join(this.distDir, ...(params.path || []))
await this.serveStatic(req, res, p)
},
})
return routes
}
2019-05-03 18:57:47 +02:00
// In development public files are not added to the router but handled as a fallback instead
protected generatePublicRoutes() {
2019-05-03 18:57:47 +02:00
return []
}
// In development dynamic routes cannot be known ahead of time
protected getDynamicRoutes() {
return []
}
_filterAmpDevelopmentScript(
html: string,
event: { line: number; col: number; code: string }
) {
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')
}
/**
* Check if resolver function is build or request new build for this function
* @param {string} pathname
*/
protected async resolveApiRequest(pathname: string): Promise<string | null> {
try {
await this.hotReloader!.ensurePage(pathname)
} catch (err) {
// API route dosn't exist => return 404
if (err.code === 'ENOENT') {
return null
}
}
const resolvedPath = await super.resolveApiRequest(pathname)
return resolvedPath
}
async renderToHTML(
req: IncomingMessage,
res: ServerResponse,
pathname: string,
query: { [key: string]: string },
options = {}
) {
const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) {
res.statusCode = 500
return this.renderErrorToHTML(compilationErr, req, res, pathname, query)
}
// In dev mode we use on demand entries to compile the page before rendering
try {
await this.hotReloader!.ensurePage(pathname).catch(async (err: Error) => {
if ((err as any).code !== 'ENOENT') {
throw err
}
for (const dynamicRoute of this.dynamicRoutes || []) {
const params = dynamicRoute.match(pathname)
if (!params) {
continue
}
return this.hotReloader!.ensurePage(dynamicRoute.page).then(() => {
pathname = dynamicRoute.page
query = Object.assign({}, query, params)
})
}
throw err
})
} catch (err) {
if (err.code === 'ENOENT') {
res.statusCode = 404
return this.renderErrorToHTML(null, req, res, pathname, query)
}
if (!this.quiet) console.error(err)
}
const html = await super.renderToHTML(req, res, pathname, query, options)
return html
}
async renderErrorToHTML(
err: Error | null,
req: IncomingMessage,
res: ServerResponse,
pathname: string,
query: { [key: string]: string }
) {
await this.hotReloader!.ensurePage('/_error')
const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) {
res.statusCode = 500
return super.renderErrorToHTML(compilationErr, req, res, pathname, query)
}
if (!err && res.statusCode === 500) {
err = new Error(
'An undefined error was thrown sometime during render... ' +
'See https://err.sh/zeit/next.js/threw-undefined'
)
}
try {
const out = await super.renderErrorToHTML(err, req, res, pathname, query)
return out
} catch (err2) {
if (!this.quiet) Log.error(err2)
res.statusCode = 500
return super.renderErrorToHTML(err2, req, res, pathname, query)
}
}
sendHTML(req: IncomingMessage, res: ServerResponse, html: string) {
// In dev, we should not cache pages for any reason.
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return super.sendHTML(req, res, html)
}
protected setImmutableAssetCacheControl(res: ServerResponse) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
}
servePublic(req: IncomingMessage, res: ServerResponse, path: string) {
2019-05-03 18:57:47 +02:00
const p = join(this.publicDir, path)
return this.serveStatic(req, res, p)
}
async hasPublicFile(path: string) {
try {
const info = await fsStat(join(this.publicDir, path))
return info.isFile()
} catch (_) {
return false
}
}
async getCompilationError(page: string) {
const errors = await this.hotReloader!.getCompilationErrors(page)
if (errors.length === 0) return
// Return the very first error we found.
return errors[0]
}
}