Skip to content

Commit

Permalink
Merge branch 'canary' into respect-cache-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle authored Jul 6, 2021
2 parents 173a383 + d476c91 commit 1be90b3
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parse as parseUrl } from 'url'
import { IncomingMessage, ServerResponse } from 'http'
import { apiResolver } from '../../../../server/api-utils'
import { getUtils, vercelHeader, ServerlessHandlerCtx } from './utils'
import { DecodeError } from '../../../../shared/lib/utils'

export function getApiHandler(ctx: ServerlessHandlerCtx) {
const { pageModule, encodedPreviewProps, pageIsDynamic } = ctx
Expand Down Expand Up @@ -50,8 +51,7 @@ export function getApiHandler(ctx: ServerlessHandlerCtx) {
} catch (err) {
console.error(err)

// TODO: better error for DECODE_FAILED?
if (err.code === 'DECODE_FAILED') {
if (err instanceof DecodeError) {
res.statusCode = 400
res.end('Bad Request')
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage, ServerResponse } from 'http'
import { parse as parseUrl, format as formatUrl, UrlWithParsedQuery } from 'url'
import { isResSent } from '../../../../shared/lib/utils'
import { DecodeError, isResSent } from '../../../../shared/lib/utils'
import { sendPayload } from '../../../../server/send-payload'
import { getUtils, vercelHeader, ServerlessHandlerCtx } from './utils'

Expand Down Expand Up @@ -409,8 +409,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {

if (err.code === 'ENOENT') {
res.statusCode = 404
} else if (err.code === 'DECODE_FAILED' || err.code === 'ENAMETOOLONG') {
// TODO: better error?
} else if (err instanceof DecodeError) {
res.statusCode = 400
} else {
console.error('Unhandled error during request:', err)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/route-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function resolvePromiseWithTimeout<T>(

// TODO: stop exporting or cache the failure
// It'd be best to stop exporting this. It's an implementation detail. We're
// only exporting it for backwards compatibilty with the `page-loader`.
// only exporting it for backwards compatibility with the `page-loader`.
// Only cache this response as a last resort if we cannot eliminate all other
// code branches that use the Build Manifest Callback and push them through
// the Route Loader interface.
Expand Down
4 changes: 2 additions & 2 deletions packages/next/lib/eslint/writeDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export async function writeDefaultConfig(
}
} else {
await fs.writeFile(
'.eslintrc',
'.eslintrc.json',
CommentJson.stringify(defaultConfig, null, 2) + os.EOL
)

console.log(
chalk.green(
`We created the ${chalk.bold(
'.eslintrc'
'.eslintrc.json'
)} file for you and included the base Next.js ESLint configuration.`
)
)
Expand Down
7 changes: 2 additions & 5 deletions packages/next/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { stringify } from 'querystring'
import { difference } from '../../build/utils'
import { NextConfig } from '../config'
import { CustomRoutes } from '../../lib/load-custom-routes'
import { DecodeError } from '../../shared/lib/utils'

export async function renderScriptError(
res: ServerResponse,
Expand Down Expand Up @@ -212,11 +213,7 @@ export default class HotReloader {
.map((param) => decodeURIComponent(param))
.join('/')}`
} catch (_) {
const err: Error & { code?: string } = new Error(
'failed to decode param'
)
err.code = 'DECODE_FAILED'
throw err
throw new DecodeError('failed to decode param')
}

const page = denormalizePagePath(decodedPagePath)
Expand Down
12 changes: 6 additions & 6 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Server, {
WrappedBuildError,
ServerConstructor,
FindComponentsResult,
ResponsePayload,
} from '../next-server'
import { normalizePagePath } from '../normalize-page-path'
import Router, { Params, route } from '../router'
Expand All @@ -46,6 +47,7 @@ import {
LoadComponentsReturnType,
loadDefaultErrorComponents,
} from '../load-components'
import { DecodeError } from '../../shared/lib/utils'

if (typeof React.Suspense === 'undefined') {
throw new Error(
Expand Down Expand Up @@ -376,9 +378,7 @@ export default class DevServer extends Server {
try {
decodedPath = decodeURIComponent(path)
} catch (_) {
const err: Error & { code?: string } = new Error('failed to decode param')
err.code = 'DECODE_FAILED'
throw err
throw new DecodeError('failed to decode param')
}

if (await this.hasPublicFile(decodedPath)) {
Expand Down Expand Up @@ -637,14 +637,14 @@ export default class DevServer extends Server {
return await loadDefaultErrorComponents(this.distDir)
}

sendHTML(
sendResponse(
req: IncomingMessage,
res: ServerResponse,
html: string
response: ResponsePayload
): Promise<void> {
// In dev, we should not cache pages for any reason.
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return super.sendHTML(req, res, html)
return super.sendResponse(req, res, response)
}

protected setImmutableAssetCacheControl(res: ServerResponse): void {
Expand Down
Loading

0 comments on commit 1be90b3

Please sign in to comment.