Skip to content

fix: support setting cookies from MiddlewareResponse #2027

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

Merged
merged 22 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
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
6 changes: 6 additions & 0 deletions packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ export const buildResponse = async ({
if (request.method === 'HEAD' || request.method === 'OPTIONS') {
return response.originResponse
}

// NextResponse doesn't set cookies onto the originResponse, so we need to copy them over
if (response.headers.has('set-cookie')) {
response.originResponse.headers.set('set-cookie', response.headers.get('set-cookie')!)
}

// If it's JSON we don't need to use the rewriter, we can just parse it
if (response.originResponse.headers.get('content-type')?.includes('application/json')) {
const props = await response.originResponse.json()
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/modified-tests/middleware-general/app/middleware.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files are the tests from the Next.js repo, so shouldn't have new ones added. New ones should go in either Cypress or Jest tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true (and let me know if you want me to move them @Skn0tt because the folder structure can be a bit difficult to navigate)
however, i've wondered for a while whether we should use the Next.js repo pattern for our own e2e/integration tests - i.e. spinning up small, specific Next.js apps rather than testing against the demo sites, which include a mixed bag of functionality - it seems easier to reason around

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. They should be a new suite of tests though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and let me know if you want me to move them @Skn0tt because the folder structure can be a bit difficult to navigate

that'd be lovely, yeah!

i'm not sure wether mix fix actually fixes the problem, but having tests in the right directory would definitely help :D

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global globalThis */
import { NextRequest, NextResponse, URLPattern } from 'next/server'
import { MiddlewareRequest } from '@netlify/next'
import magicValue from 'shared-package'

export const config = { regions: 'auto' }
Expand Down Expand Up @@ -71,6 +72,13 @@ export async function middleware(request) {
return NextResponse.rewrite(url)
}

if (url.pathname === '/cookie-repro') {
const mwRequest = new MiddlewareRequest(request)
const mwResponse = await mwRequest.next()
mwResponse.cookies.set('foo', 'bar')
return mwResponse
}

if (url.pathname.startsWith('/fetch-user-agent-default')) {
try {
const apiRoute = new URL(url)
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/modified-tests/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('Middleware Runtime', () => {
start: 'next start',
},
},
dependencies: {
'@netlify/next': 'file:' + join(__dirname, '../../../../../packages/next'),
},
startCommand: (global as any).isNextDev ? 'yarn dev' : 'yarn start',
buildCommand: 'yarn build',
env: {
Expand Down Expand Up @@ -465,6 +468,12 @@ describe('Middleware Runtime', () => {
expect(readMiddlewareError(response)).toContain(urlsError)
})

// https://github.com/netlify/pillar-support/issues/350
it('supports setting cookies', async () => {
const response = await fetchViaHTTP(next.url, `/cookie-repro`)
expect(response.headers.get('set-cookie')).toContain('foo')
})

if (!(global as any).isNextDeploy) {
// these errors differ on Vercel
it('should throw when using Request with a relative URL', async () => {
Expand Down