Skip to content

T-137: fix(redirect): merge query string with the one already in the url - #160

Open
fleet-panda[bot] wants to merge 2 commits into
9.xfrom
task/T-137
Open

fleet-panda[bot] wants to merge 2 commits into
9.xfrom
task/T-137

Conversation

@fleet-panda

@fleet-panda fleet-panda Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Builds on #158 by @bardrop

Fixes #157

Goal

A redirect to a URL that already has a query string adds a second ?. toIntended() of @adonisjs/session passes such a URL to toPath(), because the session saves request.url(true) as the intended URL. For example, withQs({ welcome: 1 }).toIntended('/dashboard') after a login from /dashboard?tab=billing redirects to /dashboard?tab=billing?welcome=1.

This pull request keeps the commit of #158, and adds one commit on top of it. #sendResponse now parses the query string of the URL with the configured qs parser, as back() does, and assigns the new values on it. A value of withQs(), or of the forwarded query string, replaces the same key of the URL. The fragment stays at the end.

Deployment requirements

  • Release @adonisjs/http-server as a patch after the merge.

Acceptance criteria

  • withQs({ success: 1 }).toPath('/users?page=2') redirects to /users?page=2&success=1. Test: append query string to a url that already has one. Passes.
  • withQs({ success: 1 }).toPath('/users?page=2#list') redirects to /users?page=2&success=1#list, and withQs({ success: 1 }).toPath('/users#list') redirects to /users?success=1#list. Tests: add query string before the url fragment, add query string before the fragment of a url without one. Pass.
  • withQs({ page: 3 }).toPath('/users?page=2') redirects to /users?page=3. Tests: replace the keys of the url query string with the custom query string, replace the keys of the url query string with the forwarded query string. Pass. Both fail on the commit of fix(redirect): merge query string with the one already in the url #158.
  • withQs({ success: 1 }).toPath('https://example.com/users?page=2') gives https://example.com/users?page=2&success=1. Test: keep the host of an absolute url when merging the query string. Passes.
  • When the redirect adds no query string, the URL stays as it is. Test: keep the url as it is when there is no query string to add (/users?page=2&page=3#list stays the same). Passes.

npm run lint, npm run typecheck and npm run quick:test (680 tests) pass.

Context changes

None.

Discovered work

None.

bardrop and others added 2 commits November 26, 2026 12:42
sendResponse always built "${url}?${qs}", so withQs() or forwarded
query strings gave "/users?page=2?success=1" when the target already had
a query string, and "/users#list?success=1" when it had a fragment.

Join with "&" when the url already has a "?", and keep the fragment at
the end.

Fixes #157
Parse the query string of the url and assign the new values on it, as
back() does, so withQs({ page: 3 }).toPath('/users?page=2') redirects
to /users?page=3 and not to /users?page=2&page=3.

This branch has not been deployed

No deployments
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.

redirect() adds a second ? when the target url already has a query string

2 participants