-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(vercel): edge middleware next() drops HTTP method and body #16170
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@astrojs/vercel': patch | ||
| --- | ||
|
|
||
| Fixes edge middleware `next()` dropping the HTTP method and body when forwarding requests to the serverless function, which caused non-GET API routes (POST, PUT, PATCH, DELETE) to return 404 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,12 +127,15 @@ export default async function middleware(request, context) { | |
| const next = async () => { | ||
| const { vercel, ...locals } = ctx.locals; | ||
| const response = await fetch(new URL('/${NODE_PATH}', request.url), { | ||
| method: request.method, | ||
| headers: { | ||
| ...Object.fromEntries(request.headers.entries()), | ||
| '${ASTRO_MIDDLEWARE_SECRET_HEADER}': '${middlewareSecret}', | ||
| '${ASTRO_PATH_HEADER}': request.url.replace(origin, ''), | ||
| '${ASTRO_LOCALS_HEADER}': trySerializeLocals(locals) | ||
| } | ||
| }, | ||
| body: request.body, | ||
| duplex: 'half', | ||
| }); | ||
|
Comment on lines
129
to
138
|
||
| return new Response(response.body, { | ||
| status: response.status, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,18 @@ describe('Vercel edge middleware', () => { | |||||||||||||||||||||||||||||||||||
| assert.ok((await response.text()).length, 'Body is included'); | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it('edge middleware forwards HTTP method and body', async () => { | ||||||||||||||||||||||||||||||||||||
| const contents = await build.readFile( | ||||||||||||||||||||||||||||||||||||
| '../.vercel/output/functions/_middleware.func/middleware.mjs', | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| assert.ok(contents.includes('method: request.method'), 'forwards the HTTP method'); | ||||||||||||||||||||||||||||||||||||
| assert.ok(contents.includes('body: request.body'), 'forwards the request body'); | ||||||||||||||||||||||||||||||||||||
| assert.ok( | ||||||||||||||||||||||||||||||||||||
| contents.includes("duplex: 'half'") || contents.includes('duplex: "half"'), | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| assert.ok(contents.includes('method: request.method'), 'forwards the HTTP method'); | |
| assert.ok(contents.includes('body: request.body'), 'forwards the request body'); | |
| assert.ok( | |
| contents.includes("duplex: 'half'") || contents.includes('duplex: "half"'), | |
| assert.match( | |
| contents, | |
| /fetch\([\s\S]*?{[\s\S]*?method\s*:\s*request\.method[\s\S]*?}/, | |
| 'forwards the HTTP method', | |
| ); | |
| assert.match( | |
| contents, | |
| /fetch\([\s\S]*?{[\s\S]*?body\s*:\s*request\.body[\s\S]*?}/, | |
| 'forwards the request body', | |
| ); | |
| assert.match( | |
| contents, | |
| /fetch\([\s\S]*?{[\s\S]*?duplex\s*:\s*['"]half['"][\s\S]*?}/, |
Uh oh!
There was an error while loading. Please reload this page.