Visitar URL original
qs.stringify does not respect the addQueryPrefix param when the result of stringify is an empty string · Issue #418 · ljharb/qs · GitHub
Skip to content

qs.stringify does not respect the addQueryPrefix param when the result of stringify is an empty string #418

Closed

Description

@ehab-cl

Hey, thank you for the amazing library.

The following will return an empty string which i think is unexpected
qs.stringify({}, { addQueryPrefix: true })
i would expect the previous to return "?"

The use case is for many navigating methods including the native replaceState in window.history they discard an empty string while "?" means to empty the url query params.

I have noticed that this change was introduced in this commit 229b0e9#diff-0b54a0fe4e28dd7c969fecd6711539ca19dcd5cc1e19a175193d805468d863caL208, previously it would work as expected.

Activity

  1. ljharb commented on Aug 2, 2021

    @ljharb
    Owner

    Yes, imo it would be incorrect to produce a ? when there's nothing in the string; see #213. You can do qs.stringify({}, { addQueryPrefix: true }) || '?' pretty easily if that's something you want.

  2. ehab-cl commented on Aug 11, 2021

    @ehab-cl
    Author

    @ljharb Thanks for the reply. Yea what you suggested is what i already did in my code. But i still think it is worth adding in documentation that addQueryPrefix does not work when stringify produces empty string.

    Still i think the way the option is called does not give any indicator that query prefix won't be added when the result is an empty string , and one can do something like the following if not interested in getting just a question mark without anything.

    // lodash isEmpty and assuming that params has no undefined values 
    isEmpty(params) ? "" : qs.stringify(params, { addQueryPrefix: true })
    
  3. ljharb commented on Aug 11, 2021

    @ljharb
    Owner

    I'm fine adding it to the docs.

  4. added a commit that references this issue on Feb 11, 2026
    294db90
  5. added 9 commits that reference this issue on Feb 13, 2026
    625fa19
    8a1b294
    09044b3
    b68d61e
    3a5f714
    3d135d0
    e26a45b
    db6477e
    6e39e92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions