Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export async function initialize(opts: {
req.url = removePathPrefix(origUrl, config.assetPrefix)
}

if (resHeaders) {
if (resHeaders !== null) {
for (const key of Object.keys(resHeaders)) {
res.setHeader(key, resHeaders[key])
}
Expand All @@ -441,8 +441,10 @@ export async function initialize(opts: {
})

// apply any response headers from routing
for (const key of Object.keys(resHeaders || {})) {
res.setHeader(key, resHeaders[key])
if (resHeaders !== null) {
for (const key of Object.keys(resHeaders)) {
res.setHeader(key, resHeaders[key])
}
}

// handle redirect
Expand Down
14 changes: 9 additions & 5 deletions packages/next/src/server/lib/router-utils/resolve-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function getResolveRoutes(
finished: boolean
statusCode?: number
bodyStream?: ReadableStream | null
resHeaders: Record<string, string | string[]>
resHeaders: Record<string, string | string[]> | null
parsedUrl: NextUrlWithParsedQuery
matchedOutput?: FsOutput | null
}> {
Expand Down Expand Up @@ -752,10 +752,12 @@ export function getResolveRoutes(
parsedDestination.pathname
)

// @ts-expect-error // custom ParsedUrl
const unsafeParsedUrl: NextUrlWithParsedQuery = parsedDestination
return {
finished: true,
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
Comment on lines -757 to -758
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This somehow made the whole return type unsound. Once I moved the unsafe cast out of the object literal, TS started to complain about the missing resHeaders

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, any // @ts-expect-error or // @ts-ignore inside an object declaration actually marks the entire object's definition, not just the next line.

parsedUrl: unsafeParsedUrl,
resHeaders: null,
statusCode: getRedirectStatus(route),
}
}
Expand Down Expand Up @@ -826,9 +828,11 @@ export function getResolveRoutes(
}

if (parsedDestination.protocol) {
// @ts-expect-error // custom ParsedUrl
const unsafeParsedUrl: NextUrlWithParsedQuery = parsedDestination
return {
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
parsedUrl: unsafeParsedUrl,
resHeaders: null,
finished: true,
}
}
Expand Down
Loading