Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookies and headers for request using in RSC #31623

Merged
merged 6 commits into from
Nov 22, 2021
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { NextRequest } from '../../../../server/web/spec-extension/request'
import { renderToHTML } from '../../../../server/web/render'
import RenderResult from '../../../../server/render-result'

const createHeaders = (args?: any) => ({
...args,
'x-middleware-ssr': '1',
})
huozhi marked this conversation as resolved.
Show resolved Hide resolved

export function getRender({
App,
Document,
Expand All @@ -24,15 +29,15 @@ export function getRender({
restRenderOpts: any
}) {
return async function render(request: NextRequest) {
const url = request.nextUrl
const { nextUrl: url, cookies, headers } = request
const { pathname, searchParams } = url

const query = Object.fromEntries(searchParams)

// Preflight request
if (request.method === 'HEAD') {
return new Response('OK.', {
headers: { 'x-middleware-ssr': '1' },
return new Response(null, {
headers: createHeaders(),
})
}

Expand All @@ -41,7 +46,11 @@ export function getRender({
: false
delete query.__flight__

const req = { url: pathname }
const req = {
url: pathname,
cookies,
headers,
}
const renderOpts = {
...restRenderOpts,
// Locales are not supported yet.
Expand Down Expand Up @@ -103,7 +112,7 @@ export function getRender({
).toString(),
{
status: 500,
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
}
)
}
Expand All @@ -114,7 +123,7 @@ export function getRender({
'An error occurred while rendering ' + pathname + '.',
{
status: 500,
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
}
)
}
Expand All @@ -126,7 +135,7 @@ export function getRender({
} as any)

return new Response(transformStream.readable, {
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
status: 200,
})
}
Expand Down