Skip to content

Commit

Permalink
chore: restores RSC when running on the edge
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed May 12, 2022
1 parent f0b1dd8 commit 7881769
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function middlewareLoader(this: any) {
}

return `
import { adapter } from 'next/dist/server/web/adapter'
import { adapter, blockUnallowedResponse } from 'next/dist/server/web/adapter'
// The condition is true when the "process" module is provided
if (process !== global.process) {
Expand All @@ -32,11 +32,11 @@ export default function middlewareLoader(this: any) {
}
export default function (opts) {
return adapter({
return blockUnallowedResponse(adapter({
...opts,
page: ${JSON.stringify(page)},
handler,
})
}))
}
`
}
42 changes: 22 additions & 20 deletions packages/next/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import { DeprecationError } from './error'
import { fromNodeHeaders } from './utils'
import { NextFetchEvent } from './spec-extension/fetch-event'
import { NextRequest } from './spec-extension/request'
import {
NextResponse,
RedirectHeader,
RewriteHeader,
NextMiddlewareHeader,
} from './spec-extension/response'
import { NextResponse, RedirectHeader } from './spec-extension/response'
import { waitUntilSymbol } from './spec-compliant/fetch-event'

export async function adapter(params: {
Expand All @@ -39,25 +34,32 @@ export async function adapter(params: {
const event = new NextFetchEvent({ request, page: params.page })
const response = await params.handler(request, event)

const error = isAllowed(response)
? null
: new Response(
JSON.stringify({
message: `A middleware can not alter response's body. Learn more: https://nextjs.org/docs/messages/returning-response-body-in-_middleware`,
}),
{
status: 500,
statusText: 'Internal Server Error',
headers: { 'content-type': 'application/json' },
}
)

return {
response: error || response || NextResponse.next(),
response: response || NextResponse.next(),
waitUntil: Promise.all(event[waitUntilSymbol]),
}
}

export function blockUnallowedResponse(
promise: Promise<FetchEventResult>
): Promise<FetchEventResult> {
return promise.then((result) => ({
...result,
response: isAllowed(result.response)
? result.response
: new Response(
JSON.stringify({
message: `A middleware can not alter response's body. Learn more: https://nextjs.org/docs/messages/returning-response-body-in-_middleware`,
}),
{
status: 500,
statusText: 'Internal Server Error',
headers: { 'content-type': 'application/json' },
}
),
}))
}

function isAllowed(response: NextMiddlewareResult): boolean {
return (
!response?.body || !response.body || response.headers.has(RedirectHeader)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/web/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class DeprecationError extends Error {
super(`The middleware "${page}" accepts an async API directly with the form:
export function middleware(request, event) {
return new Response("Hello " + request.url)
return new NextResponse(null, { status: 403 })
}
Read more: https://nextjs.org/docs/messages/middleware-new-signature
Expand Down

0 comments on commit 7881769

Please sign in to comment.