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

«Rewrite mechanism» transforms array parameters of query of destination link into strings #26609

Closed
lsndr opened this issue Jun 25, 2021 · 1 comment · Fixed by #26619
Closed
Labels
bug Issue was opened via the bug report template.

Comments

@lsndr
Copy link
Contributor

lsndr commented Jun 25, 2021

What version of Next.js are you using?

10.2.2

What version of Node.js are you using?

14.17.1

What browser are you using?

Safari

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

I'm trying to use Next.js' Rewrite feature. But when I put an array in a query part of a destination link string (in next.config.js), a context, that I receive in getServerSideProps, contains only first parameter of the array.

Here is my next.config.js

module.exports = {
...
  async rewrites() {
    return [
      {
        source: '/projects',
        destination: '/?tags=project&tags=проект',
      },
    ]
  },
...
}

When I open /projects in a browser, only first value of tags parameter is passed into context.query of getServerSideProps:

{
    tags: 'project'
}

Expected Behavior

context.query in getServerSideProps must contain all query values.

{
    tags: ['project', 'проект']
}

To Reproduce

  • Create a new next.js project with the following next.config.js:
module.exports = {
...
  async rewrites() {
    return [
      {
        source: '/projects',
        destination: '/?tags=project&tags=проект',
      },
    ]
  },
...
}
  • Create a page 'index.tsx' with getServerSideProps:
export const getServerSideProps: GetServerSideProps<any> = async (context) => {
    ....
    console.log(context.query);
    ....
};
  • Open /projects page in a browser and check the server-side terminal

Actually, I've figured out why this happens and i'm ready to create PR. But before, I need to make sure that I understood the problem.

Here is the code, that causes the bug (packages/next/next-server/lib/router/utils/prepare-destination.ts):

export default function prepareDestination(
  destination: string,
  params: Params,
  query: ParsedUrlQuery,
  appendParamsToQuery: boolean
) {
...
  // update any params in query values
  for (const [key, strOrArray] of Object.entries(destQuery)) {
    let value = Array.isArray(strOrArray) ? strOrArray[0] : strOrArray
    if (value) {
      // the value needs to start with a forward-slash to be compiled
      // correctly
      value = compileNonPath(value, params)
    }
    destQuery[key] = value
  }
...

As you can see, it takes only a first item of the query array. I believe we must use.maphere

@lsndr lsndr added the bug Issue was opened via the bug report template. label Jun 25, 2021
@lsndr lsndr changed the title Rewrite trasnforms array query parameters of destination link into strings Rewrite transforms array parameters of query of destination link into strings Jun 25, 2021
@lsndr lsndr changed the title Rewrite transforms array parameters of query of destination link into strings "Rewrite" transforms array parameters of query of destination link into strings Jun 25, 2021
@lsndr lsndr changed the title "Rewrite" transforms array parameters of query of destination link into strings «Rewrite mechanism» transforms array parameters of query of destination link into strings Jun 25, 2021
@kodiakhq kodiakhq bot closed this as completed in #26619 Jul 6, 2021
kodiakhq bot pushed a commit that referenced this issue Jul 6, 2021
fixes #26609

## Bug

- [x]  Related issues linked using fixes #number
- [x]   Integration tests added

## Documentation / Examples

- [x]  Make sure the linting passes
flybayer pushed a commit to blitz-js/next.js that referenced this issue Aug 19, 2021
fixes vercel#26609

## Bug

- [x]  Related issues linked using fixes #number
- [x]   Integration tests added

## Documentation / Examples

- [x]  Make sure the linting passes
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants