Skip to content

Commit

Permalink
fix gssp test and rsc test head warning
Browse files Browse the repository at this point in the history
fix dynamic routes

fix dynamic routes

catch find page and rendering error thrown

revert dynamic routes render change

reverts
  • Loading branch information
huozhi committed Jun 17, 2022
1 parent c5bebba commit 2061291
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
120 changes: 92 additions & 28 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1758,45 +1758,90 @@ export default abstract class Server<ServerOptions extends Options = Options> {
ctx: RequestContext
): Promise<ResponsePayload | null> {
const { res, query, pathname } = ctx
let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query._nextBubbleNoFallback

let result
let page = pathname
let params: Params | undefined = undefined
let pageFound = !isDynamicRoute(page) && (await this.hasPage(page))
if (!pageFound && this.dynamicRoutes) {
for (const dynamicRoute of this.dynamicRoutes) {
params = dynamicRoute.match(pathname) || undefined
if (params) {
page = dynamicRoute.page
pageFound = true
break
}
}
}

// map the route to the actual bundle name
const getOriginalAppPath = (appPath: string) => {
const getOriginalappPath = (appPath: string) => {
if (this.nextConfig.experimental.appDir) {
const originalAppPath = this.appPathRoutes?.[appPath]
const originalappPath = this.appPathRoutes?.[appPath]

if (!originalAppPath) {
if (!originalappPath) {
return null
}

return originalAppPath
return originalappPath
}
return null
}
const appPath = getOriginalAppPath(page)
if (typeof appPath === 'string') {
page = appPath
}

try {
result = await this.renderPage(ctx, params, page, bubbleNoFallback)
if (result) return result
// Ensure a request to the URL /accounts/[id] will be treated as a dynamic
// route correctly and not loaded immediately without parsing params.
if (!isDynamicRoute(pathname)) {
const appPath = getOriginalappPath(pathname)

if (typeof appPath === 'string') {
page = appPath
}
const result = await this.findPageComponents(page, query)
if (result) {
try {
return await this.renderToResponseWithComponents(ctx, result)
} catch (err) {
const isNoFallbackError = err instanceof NoFallbackError

if (!isNoFallbackError || (isNoFallbackError && bubbleNoFallback)) {
throw err
}
}
}
}

if (this.dynamicRoutes) {
for (const dynamicRoute of this.dynamicRoutes) {
const params = dynamicRoute.match(pathname)
if (!params) {
continue
}
page = dynamicRoute.page
const appPath = getOriginalappPath(page)

if (typeof appPath === 'string') {
page = appPath
}

const dynamicRouteResult = await this.findPageComponents(
page,
query,
params
)
if (dynamicRouteResult) {
try {
return await this.renderToResponseWithComponents(
{
...ctx,
pathname: page,
renderOpts: {
...ctx.renderOpts,
params,
},
},
dynamicRouteResult
)
} catch (err) {
const isNoFallbackError = err instanceof NoFallbackError

if (
!isNoFallbackError ||
(isNoFallbackError && bubbleNoFallback)
) {
throw err
}
}
}
}
}
} catch (error) {
const err = getProperError(error)
if (err instanceof NoFallbackError && bubbleNoFallback) {
Expand Down Expand Up @@ -1828,7 +1873,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}

if (
this.router.catchAllMiddleware &&
this.router.catchAllMiddleware[1] &&
!!ctx.req.headers['x-nextjs-data'] &&
(!res.statusCode || res.statusCode === 200 || res.statusCode === 404)
) {
Expand All @@ -1853,7 +1898,26 @@ export default abstract class Server<ServerOptions extends Options = Options> {
page: string,
bubbleNoFallback: boolean
) {
// map the route to the actual bundle name
const getOriginalAppPath = (appPath: string) => {
if (this.nextConfig.experimental.appDir) {
const originalAppPath = this.appPathRoutes?.[appPath]

if (!originalAppPath) {
return null
}

return originalAppPath
}
return null
}

const { query } = ctx
const appPath = getOriginalAppPath(page)
if (typeof appPath === 'string') {
page = appPath
}

const result = await this.findPageComponents(page, query, params)
if (result) {
try {
Expand All @@ -1863,7 +1927,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
pathname: page,
renderOpts: {
...ctx.renderOpts,
params,
...(params && { params }),
},
},
result
Expand Down
13 changes: 6 additions & 7 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,9 +1290,9 @@ export default class NextNodeServer extends BaseServer {
const normalizedPathname = removeTrailingSlash(pathname || '')
let page = normalizedPathname
let params: Params | undefined = undefined
let pageFound = !isDynamicRoute(page) && (await this.hasPage(page))
let pageFound = !isDynamicRoute(page)

if (!pageFound && this.dynamicRoutes) {
if (this.dynamicRoutes) {
for (const dynamicRoute of this.dynamicRoutes) {
params = dynamicRoute.match(normalizedPathname) || undefined
if (params) {
Expand Down Expand Up @@ -1515,8 +1515,8 @@ export default class NextNodeServer extends BaseServer {
}

protected async runEdgeFunction(params: {
req: BaseNextRequest
res: BaseNextResponse
req: BaseNextRequest | NodeNextRequest
res: BaseNextResponse | NodeNextResponse
query: ParsedUrlQuery
params: Params | undefined
page: string
Expand Down Expand Up @@ -1577,14 +1577,13 @@ export default class NextNodeServer extends BaseServer {
params.res.appendHeader(key, value)
})

const nodeRes = params.res as NodeNextResponse
if (result.response.body) {
// TODO(gal): not sure that we always need to stream
bodyStreamToNodeStream(result.response.body).pipe(
nodeRes.originalResponse
(params.res as NodeNextResponse).originalResponse
)
} else {
nodeRes.originalResponse.end()
;(params.res as NodeNextResponse).originalResponse.end()
}

return result
Expand Down
4 changes: 2 additions & 2 deletions packages/next/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ export default class Router {
...(this.useFileSystemPublicRoutes && middlewareRoute
? [middlewareRoute]
: []),
...this.rewrites.beforeFiles,
...this.fsRoutes,
// edge functions ssr
...(edgeSSRRoute ? [edgeSSRRoute] : []),
...this.rewrites.beforeFiles,
...this.fsRoutes,
// We only check the catch-all route if public page routes hasn't been
// disabled
...(this.useFileSystemPublicRoutes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Index({ header }) {
<div>
<Head>
<meta name="rsc-title" content="index" />
<title>hello, {envVar}</title>
<title>{`hello, ${envVar}`}</title>
</Head>
<h1>{`component:index.server`}</h1>
<div>{'env:' + envVar}</div>
Expand Down

0 comments on commit 2061291

Please sign in to comment.