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

fix(nextjs): Skip NextResponse normalization in mergeResponses if possible #2244

Conversation

LekoArts
Copy link
Member

@LekoArts LekoArts commented Dec 1, 2023

Description

In the mergeResponses helper inside nextjs we normalize the incoming responses (which can be NextResponse or Response) by passing through the res to a new NextResponse().

In that process somehow the cookies are getting removed (except for the first one). This PR addresses this issue by just skipping this normalization if the incoming reponse is already a NextResponse.

Additionally, while updating some unit tests I also saw that we didn't pass along the options for cookies.set().

Fixes #1897

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Packages affected

  • @clerk/backend
  • @clerk/chrome-extension
  • @clerk/clerk-js
  • @clerk/clerk-expo
  • @clerk/fastify
  • gatsby-plugin-clerk
  • @clerk/localizations
  • @clerk/nextjs
  • @clerk/clerk-react
  • @clerk/remix
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/themes
  • @clerk/types
  • build/tooling/chore

Copy link

changeset-bot bot commented Dec 1, 2023

🦋 Changeset detected

Latest commit: 18ae827

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/nextjs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +46 to +60
.addFile(
'src/app/provider.tsx',
() => `'use client'
import { ClerkProvider } from "@clerk/nextjs"
export function Provider({ children }: { children: any }) {
return (
<ClerkProvider>
{children}
</ClerkProvider>
)
}`,
)
.addFile(
'src/app/layout.tsx',
Copy link
Member Author

Choose a reason for hiding this comment

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

The test did not run without these additions. The next-build.test.ts file has the same additions.

Why is the base template not just including these?

Comment on lines +44 to +55
response2.cookies.set('foo', '1');
response2.cookies.set({
name: 'second',
value: '2',
path: '/',
sameSite: 'none',
secure: true,
});
response2.cookies.set('bar', '1', {
sameSite: 'none',
secure: true,
});
Copy link
Member Author

Choose a reason for hiding this comment

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

The user reproduction used this format and while adding it the tests failed because sameSite and secure weren't passed through

@LekoArts LekoArts marked this pull request as ready for review December 4, 2023 09:09
Copy link
Member

@tmilewski tmilewski left a comment

Choose a reason for hiding this comment

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

@LekoArts I can't speak much to your question around the integration tests, but having paired with you on the initial issue, this all looks good to me!

Just one small suggestion around removing the console.log. 👍

packages/nextjs/src/utils/response.test.ts Outdated Show resolved Hide resolved
Co-authored-by: Tom Milewski <me@tm.codes>
@LekoArts LekoArts added this pull request to the merge queue Dec 5, 2023
Merged via the queue into main with commit b892ac6 Dec 5, 2023
7 of 8 checks passed
@LekoArts LekoArts deleted the lekoarts/sdk-996-authmiddleware-corrupts-response-cookies-only-emits-first branch December 5, 2023 07:42
LekoArts added a commit that referenced this pull request Dec 5, 2023
…sible (#2244)

* fix(nextjs): If res is NextResponse just return it

* fix(nextjs): Support options when setting cookies

* chore(repo): Add initial next middleware test

* chore(repo): Make test work

* chore(repo): Add changeset

* Update packages/nextjs/src/utils/response.test.ts

Co-authored-by: Tom Milewski <me@tm.codes>

---------

Co-authored-by: Tom Milewski <me@tm.codes>
(cherry picked from commit b892ac6)
github-merge-queue bot pushed a commit that referenced this pull request Dec 5, 2023
…sible (#2244) (#2260)

* fix(nextjs): If res is NextResponse just return it

* fix(nextjs): Support options when setting cookies

* chore(repo): Add initial next middleware test

* chore(repo): Make test work

* chore(repo): Add changeset

* Update packages/nextjs/src/utils/response.test.ts

Co-authored-by: Tom Milewski <me@tm.codes>

---------

Co-authored-by: Tom Milewski <me@tm.codes>
(cherry picked from commit b892ac6)

Co-authored-by: Lennart <lekoarts@gmail.com>
tmilewski added a commit that referenced this pull request Dec 6, 2023
…sible (#2244)

* fix(nextjs): If res is NextResponse just return it

* fix(nextjs): Support options when setting cookies

* chore(repo): Add initial next middleware test

* chore(repo): Make test work

* chore(repo): Add changeset

* Update packages/nextjs/src/utils/response.test.ts

Co-authored-by: Tom Milewski <me@tm.codes>

---------

Co-authored-by: Tom Milewski <me@tm.codes>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

authMiddleware() corrupts response cookies, only emits first one in local dev
3 participants