Skip to content

Commit

Permalink
trace and conflicting case
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jul 12, 2023
1 parent c3f9827 commit 6eebf0d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
11 changes: 10 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,16 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}

res.statusCode = Number(req.headers['x-invoke-status'])
return this.renderError(null, req, res, '/_error', parsedUrl.query)
let err = null

if (typeof req.headers['x-invoke-error'] === 'string') {
const invokeError = JSON.parse(
req.headers['x-invoke-error'] || '{}'
)
err = new Error(invokeError.message)
}

return this.renderError(err, req, res, '/_error', parsedUrl.query)
}

const parsedMatchedPath = new URL(matchedPath || '/', 'http://n')
Expand Down
49 changes: 34 additions & 15 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ export async function initialize(opts: {
type: keyof typeof renderWorkers,
handleIndex: number,
invokePath: string,
invokeStatus?: string,
invokeOutput?: string
additionalInvokeHeaders: Record<string, string> = {}
) {
// invokeRender expects /api routes to not be locale prefixed
// so normalize here before continuing
Expand Down Expand Up @@ -335,13 +334,8 @@ export async function initialize(opts: {
...req.headers,
'x-middleware-invoke': '',
'x-invoke-path': invokePath,
'x-invoke-output': invokeOutput,
'x-invoke-query': encodeURIComponent(JSON.stringify(parsedUrl.query)),
...(invokeStatus
? {
'x-invoke-status': invokeStatus,
}
: {}),
...(additionalInvokeHeaders || {}),
}

debug('invokeRender', renderUrl, invokeHeaders)
Expand Down Expand Up @@ -507,6 +501,20 @@ export async function initialize(opts: {
}

if (matchedOutput?.fsPath && matchedOutput.itemPath) {
if (
opts.dev &&
(fsChecker.appFiles.has(matchedOutput.itemPath) ||
fsChecker.pageFiles.has(matchedOutput.itemPath))
) {
await invokeRender(parsedUrl, 'pages', handleIndex, '/_error', {
'x-invoke-status': '500',
'x-invoke-error': JSON.stringify({
message: `A conflicting public file and page file was found for path ${matchedOutput.itemPath} https://nextjs.org/docs/messages/conflicting-public-file-page`,
}),
})
return
}

if (
!res.getHeader('cache-control') &&
matchedOutput.type === 'nextStaticFolder'
Expand All @@ -527,7 +535,9 @@ export async function initialize(opts: {
'pages',
handleIndex,
'/405',
'405'
{
'x-invoke-status': '405',
}
)
}

Expand Down Expand Up @@ -588,7 +598,9 @@ export async function initialize(opts: {
'pages',
handleIndex,
invokePath,
invokeStatus
{
'x-invoke-status': invokeStatus,
}
)
}
throw err
Expand All @@ -601,8 +613,9 @@ export async function initialize(opts: {
matchedOutput.type === 'appFile' ? 'app' : 'pages',
handleIndex,
parsedUrl.pathname || '/',
undefined,
matchedOutput.itemPath
{
'x-invoke-output': matchedOutput.itemPath,
}
)
}

Expand All @@ -622,10 +635,14 @@ export async function initialize(opts: {
'app',
handleIndex,
'/_not-found',
'404'
{
'x-invoke-status': '404',
}
)
}
await invokeRender(parsedUrl, 'pages', handleIndex, '/404', '404')
await invokeRender(parsedUrl, 'pages', handleIndex, '/404', {
'x-invoke-status': '404',
})
}

try {
Expand All @@ -650,7 +667,9 @@ export async function initialize(opts: {
'pages',
0,
invokePath,
invokeStatus
{
'x-invoke-status': invokeStatus,
}
)
} catch (err2) {
console.error(err2)
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/lib/router-utils/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher'
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix'
import { normalizeLocalePath } from '../../../shared/lib/i18n/normalize-locale-path'
import { removePathPrefix } from '../../../shared/lib/router/utils/remove-path-prefix'

import {
MiddlewareRouteMatch,
getMiddlewareRouteMatcher,
Expand Down
31 changes: 19 additions & 12 deletions packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@ import path from 'path'
import qs from 'querystring'
import Watchpack from 'watchpack'
import { loadEnvConfig } from '@next/env'
import isError from '../../../lib/is-error'
import findUp from 'next/dist/compiled/find-up'
import { buildCustomRoute } from './filesystem'
import * as Log from '../../../build/output/log'
import HotReloader from '../../dev/hot-reloader'
import { traceGlobals } from '../../../trace/shared'
import { Telemetry } from '../../../telemetry/storage'
import { IncomingMessage, ServerResponse } from 'http'
import loadJsConfig from '../../../build/load-jsconfig'
import { createValidFileMatcher } from '../find-page-file'
import { eventCliSession } from '../../../telemetry/events'
import { getDefineEnv } from '../../../build/webpack-config'
import { logAppDirError } from '../../dev/log-app-dir-error'
import { UnwrapPromise } from '../../../lib/coalesced-function'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { getStaticInfoIncludingLayouts } from '../../../build/entries'
import {
CLIENT_STATIC_FILES_PATH,
COMPILER_NAMES,
DEV_CLIENT_PAGES_MANIFEST,
DEV_MIDDLEWARE_MANIFEST,
} from '../../../shared/lib/constants'
import { verifyTypeScriptSetup } from '../../../lib/verifyTypeScriptSetup'
import { verifyPartytownSetup } from '../../../lib/verify-partytown-setup'
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths'
import { buildDataRoute } from '../../../shared/lib/router/utils/sorted-routes'
import { MiddlewareMatcher } from '../../../build/analysis/get-page-static-info'
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { createClientRouterFilter } from '../../../lib/create-client-router-filter'
import isError from '../../../lib/is-error'
import { logAppDirError } from '../../dev/log-app-dir-error'
import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path-to-page'
import { IncomingMessage, ServerResponse } from 'http'
import { buildDataRoute } from '../../../shared/lib/router/utils/sorted-routes'
import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites'

import {
CLIENT_STATIC_FILES_PATH,
COMPILER_NAMES,
DEV_CLIENT_PAGES_MANIFEST,
DEV_MIDDLEWARE_MANIFEST,
PHASE_DEVELOPMENT_SERVER,
} from '../../../shared/lib/constants'

import {
MiddlewareRouteMatch,
getMiddlewareRouteMatcher,
Expand All @@ -55,8 +61,6 @@ import {
getSourceById,
parseStack,
} from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites'
import { buildCustomRoute } from './filesystem'

type SetupOpts = {
dir: string
Expand Down Expand Up @@ -96,6 +100,9 @@ async function startWatcher(opts: SetupOpts) {

const distDir = path.join(opts.dir, opts.nextConfig.distDir)

traceGlobals.set('distDir', distDir)
traceGlobals.set('phase', PHASE_DEVELOPMENT_SERVER)

const validFileMatcher = createValidFileMatcher(
nextConfig.pageExtensions,
appDir
Expand Down

0 comments on commit 6eebf0d

Please sign in to comment.