Skip to content

fix(node): keep the full query string on trailing slash redirects - #17961

Closed
Chy-Zaber-Bin-Zahid wants to merge 1 commit into
withastro:mainfrom
Chy-Zaber-Bin-Zahid:fix/node-trailing-slash-query
Closed

Chy-Zaber-Bin-Zahid wants to merge 1 commit into
withastro:mainfrom
Chy-Zaber-Bin-Zahid:fix/node-trailing-slash-query

Conversation

@Chy-Zaber-Bin-Zahid

@Chy-Zaber-Bin-Zahid Chy-Zaber-Bin-Zahid commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #17962

Changes

@astrojs/node's static handler splits the request URL with fullUrl.split('?') and keeps only the first two parts, so a query string that itself contains a ? is cut off when the trailing-slash redirect rebuilds the Location header.

  • GET /one/?redirect=/x?y=1&z=2 with trailingSlash: 'never' → 301 Location: /one?redirect=/x (expected /one?redirect=/x?y=1&z=2)
  • GET /one?redirect=/x?y=1&z=2 with trailingSlash: 'always' → 301 Location: /one/?redirect=/x

This affects any return URL, OAuth state, or search URL carried in the query. The fix splits on the first ? only, which is what RFC 3986 allows inside the query component.

Changeset: '@astrojs/node': patch.

Testing

Added Keeps a query string that contains a second ? on redirect to all four existing redirect suites in test/trailing-slash.test.ts (always/never, with and without base). All four fail before the change:

actual:   '/one?redirect=/x'
expected: '/one?redirect=/x?y=1&z=2'

and pass after it (38/38 in the file).

Docs

No docs change: this restores the documented behavior of the trailing-slash redirect, which is only meant to normalize the slash.

The static file handler split the request URL on every `?`, so a query
string containing a second `?` (for example `?redirect=/page?id=1`) was
truncated in the 301 `Location` header. Split on the first `?` only.

Claude-Session: https://claude.ai/code/session_01UVwZZjkMttKwiVX4Zhvkmi
@changeset-bot

changeset-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1262c29

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

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

// Only split on the first `?` so a query string that itself contains `?` is kept intact.
const queryIndex = fullUrl.indexOf('?');
const urlPath = queryIndex === -1 ? fullUrl : fullUrl.slice(0, queryIndex);
const urlQuery = queryIndex === -1 ? undefined : fullUrl.slice(queryIndex + 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes urlQuery undefined, which is a bug, because before this change, it was an empty string

@ematipico ematipico closed this Sep 10, 2026
@ematipico

Copy link
Copy Markdown
Member

The bug has been fixed. Thank you though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@astrojs/node: trailing-slash redirect drops part of a query string that contains a second ?

2 participants