Skip to content

Fix for searchParams #817

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 6 commits into from
Apr 10, 2025
Merged

Conversation

conico974
Copy link
Contributor

@conico974 conico974 commented Apr 7, 2025

Fix multivalue query handling in searchParams in Node.
Should fix opennextjs/opennextjs-cloudflare#533
Potentially fix this one as well opennextjs/opennextjs-cloudflare#537

Copy link

changeset-bot bot commented Apr 7, 2025

🦋 Changeset detected

Latest commit: ef1b5a7

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

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router 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

Copy link

pkg-pr-new bot commented Apr 7, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@817

commit: ef1b5a7

@conico974 conico974 marked this pull request as ready for review April 8, 2025 12:02
Object.entries(query).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((entry) => urlQuery.append(key, decodeURIComponent(entry)));
value.forEach((entry) => queryStrings.push(`${key}=${entry}`));

Choose a reason for hiding this comment

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

Why not use URLSearchParams here without the decodeURIComponent?

Encoding the search parameters manually seems error-prone

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ho right i'll revert this one, that was an attempt to fix it, but this function wasn't the only issue here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do remember now why i did that, convertToQueryString is only used for already properly encoded search parameters.
The query in these cases are provided by the converter (which in the case of cloudflare already use URLSearchParams)

If we revert this without the decodeURIComponent it will break the handleRewrite from next config (Specifically this test

const result = handleRedirects(event, [
{
source: "/foo",
destination: "/search?bar=hello+world&baz=new%2C+earth",
locale: false,
statusCode: 308,
regex: "^(?!/_next)/foo(?:/)?$",
},
]);
)

It should be safe enough to use it here as long as we don't use it in places where query could be not properly encoded.

@conico974 conico974 requested a review from vicb April 10, 2025 08:04
@@ -41,10 +41,18 @@ export function convertFromQueryString(query: string) {
return queryParts.reduce(
(acc, part) => {
const [key, value] = part.split("=");
acc[key] = value;
if (key in acc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe factor with getQueryFromSearchParams by passing [k,v][]?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Thanks for the fix 👍

@conico974 conico974 merged commit be3653a into opennextjs:main Apr 10, 2025
3 checks passed
@github-actions github-actions bot mentioned this pull request Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] URL in middleware has decoded parameters
3 participants