Unverified Commit a623653e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Forward response to edge runtime for response modification (#49145)

For edge runtime, we're modifying `res.statusCode` in actions handler, but we didn't forward the `res` object to it, previously we're using a fake `res` `{}`. This PR fixes it so that the propery status code will be returned to the client

[slack-thread](https://vercel.slack.com/archives/C052S77L05C/p1682988777740609)
parent c5468474
...@@ -99,7 +99,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -99,7 +99,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
import {IncrementalCache} from 'next/dist/esm/server/lib/incremental-cache' import {IncrementalCache} from 'next/dist/esm/server/lib/incremental-cache'
enhanceGlobals() enhanceGlobals()
const pageType = ${JSON.stringify(pagesType)} const pageType = ${JSON.stringify(pagesType)}
${ ${
isAppDir isAppDir
...@@ -126,7 +126,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -126,7 +126,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
const appRenderToHTML = null const appRenderToHTML = null
` `
} }
const incrementalCacheHandler = ${ const incrementalCacheHandler = ${
incrementalCacheHandlerPath incrementalCacheHandlerPath
? `require("${incrementalCacheHandlerPath}")` ? `require("${incrementalCacheHandlerPath}")`
......
...@@ -920,12 +920,12 @@ export async function renderToHTMLOrFlight( ...@@ -920,12 +920,12 @@ export async function renderToHTMLOrFlight(
<Component {...props} /> <Component {...props} />
)} )}
{/* This null is currently critical. The wrapped Component can render null and if there was not fragment {/* This null is currently critical. The wrapped Component can render null and if there was not fragment
surrounding it this would look like a pending tree data state on the client which will cause an errror surrounding it this would look like a pending tree data state on the client which will cause an errror
and break the app. Long-term we need to move away from using null as a partial tree identifier since it and break the app. Long-term we need to move away from using null as a partial tree identifier since it
is a valid return type for the components we wrap. Once we make this change we can safely remove the is a valid return type for the components we wrap. Once we make this change we can safely remove the
fragment. The reason the extra null here is required is that fragments which only have 1 child are elided. fragment. The reason the extra null here is required is that fragments which only have 1 child are elided.
If the Component above renders null the actual treedata will look like `[null, null]`. If we remove the extra If the Component above renders null the actual treedata will look like `[null, null]`. If we remove the extra
null it will look like `null` (the array is elided) and this is what confuses the client router. null it will look like `null` (the array is elided) and this is what confuses the client router.
TODO-APP update router to use a Symbol for partial tree detection */} TODO-APP update router to use a Symbol for partial tree detection */}
{null} {null}
</> </>
......
...@@ -361,7 +361,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -361,7 +361,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
protected async renderHTML( protected async renderHTML(
req: WebNextRequest, req: WebNextRequest,
_res: WebNextResponse, res: WebNextResponse,
pathname: string, pathname: string,
query: NextParsedUrlQuery, query: NextParsedUrlQuery,
renderOpts: RenderOpts renderOpts: RenderOpts
...@@ -379,7 +379,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -379,7 +379,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
headers: req.headers, headers: req.headers,
body: req.body, body: req.body,
} as any, } as any,
{} as any, res as any,
pathname, pathname,
query, query,
Object.assign(renderOpts, { Object.assign(renderOpts, {
......
...@@ -199,6 +199,18 @@ createNextDescribe( ...@@ -199,6 +199,18 @@ createNextDescribe(
await browser.elementByCss('#dec').click() await browser.elementByCss('#dec').click()
await check(() => browser.elementByCss('h1').text(), '3') await check(() => browser.elementByCss('h1').text(), '3')
}) })
it('should return error response for hoc auth wrappers in edge runtime', async () => {
const browser = await next.browser('/header/edge')
await await browser.eval(`document.cookie = 'auth=0'`)
await browser.elementByCss('#authed').click()
await check(async () => {
const text = await browser.elementByCss('h1').text()
console.log('text', text)
return text && text.length > 0 ? text : 'failed'
}, /Multipart form data is not supported/)
})
}) })
} }
) )
import { getCookie, getHeader, setCookie } from '../actions'
import UI from '../ui'
import { validator } from '../validator'
export default function Page() {
const prefix = 'Prefix:'
return (
<UI
getCookie={getCookie}
getHeader={getHeader}
setCookie={setCookie}
getAuthedUppercase={validator(async (str) => {
'use server'
return prefix + ' ' + str.toUpperCase()
})}
/>
)
}
export const runtime = 'edge'
import UI from './ui'
import { getCookie, getHeader, setCookie } from './actions' import { getCookie, getHeader, setCookie } from './actions'
import UI from './ui'
import { validator } from './validator' import { validator } from './validator'
export default function Page() { export default function Page() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment