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

URLSearchParams - getAll() is not returning proper array in edge runtime. #340

Open
robertotcestari opened this issue May 10, 2023 · 5 comments
Labels
question Further information is requested

Comments

@robertotcestari
Copy link

robertotcestari commented May 10, 2023

Bug Report

Current behavior
Using NextJS 13.4, using API routes (Pages Folder) in the following folder structure:

pages/api/[...name].jsx

When i try to use the method getAll() on the route BASE_URL/api/first-segment/second-segment:

  const { searchParams } = new URL(req.url);
  const allSegments = searchParams.getAll('name')

the expected return from getAll() was to be an array like:

[
  'first-segment',
  'second-segment'
]

This works correct in localhost and in chrome js console.
However, when pushing to production (edge runtime), it gives the following code:

[
  'first-segment/second-segment',
]

... which is different from the Web API implementation.

Expected behavior/code

From the example above, searchParams.getAll(param) was expected to return an array with two items. It returned one array with only one item.

@robertotcestari
Copy link
Author

I ran the code now with edgeVM, and works as expected.
The only place where is not working is in production code, deployed at vercel edge.

if you want to check the bug in production:
https://og.codante.io/api/test/first-segment/second-segment

@Kikobeats
Copy link
Member

Kikobeats commented May 10, 2023

Hello, I'm not seeing how .getAll('name') is going to return ['first-segment','second-segment'] to you.

Using Node.js repl:

> new URL('https://og.codante.io/api/test/first-segment/second-segment').searchParams.getAll('name')
[]

They should be query parameters rather than part of the URL, like in this way:

new URL('https://og.codante.io/api/test?name=first-segment&name=second-segment').searchParams.getAll('name')
[ 'first-segment', 'second-segment' ]

Please correct me if I'm wrong; happy to re-open the issue if needed.

@Kikobeats Kikobeats added the question Further information is requested label May 10, 2023
@robertotcestari
Copy link
Author

robertotcestari commented May 10, 2023

Hello @Kikobeats,

When i have a catch all route, NextJS injects the segments into req.url as query params when using edge runtime, so we can access it on API routes.

Sorry if i wasn't clear, but I am sure that this problem is a bug. Local and edge production environment are getting different results with the exact same code.
Please take a look at the code and the production result of both cases (as per the documentation example)

Edge Runtime (bug): https://og.codante.io/api/edge/first/second -
Node: https://og.codante.io/api/node/first/second

Below is the code that is running in production (link here):

export const config = {
  runtime: 'edge',
};

export default function handler(req) {
  const { searchParams } = new URL(req.url);
  const allSegments = searchParams.getAll('name');

  return new Response(JSON.stringify({ allSegments }), { status: 200 });

}

@robertotcestari
Copy link
Author

robertotcestari commented May 22, 2023

Hello @Kikobeats any chance reopening this?

When we run this in node:
new URL('https://og.codante.io/api/test?name=first-segment&name=second-segment').searchParams.getAll('name')
we get the expected output.

But when we run the same code in edge-runtime (production nextjs deployment @vercel ), the result is not the expected.

Please check this live:
Edge Runtime (bug): og.codante.io/api/edge/first/second -
Node: og.codante.io/api/node/first/second

@Kikobeats Kikobeats reopened this May 22, 2023
@Kikobeats
Copy link
Member

Hey @robertotcestari, I reopened the issue; definitively we have to take a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants