Skip to content

Conversation

@leonace924
Copy link
Contributor

@leonace924 leonace924 commented Jan 7, 2026

Fixes #15095

Changes

  • What does this change?

Updated baseMiddleware to ensure that after stripping the base path, the resulting URL always starts with / when it's not empty. This preserves query parameters and ensures proper URL parsing downstream.

After:

let newUrl = url.replace(devRoot, devRootReplacement);
// Ensure the URL is a valid path (e.g., /base?foo=bar -> /?foo=bar, not ?foo=bar)
if (newUrl !== '' && !newUrl.startsWith('/')) {
    newUrl = '/' + newUrl;
}
req.url = newUrl;
  • packages/astro/src/vite-plugin-astro-server/base.ts: Added logic to prepend / when the stripped URL doesn't start with / and isn't empty
  • packages/astro/src/vite-plugin-astro-server/request.ts: Already had normalization logic that handles the empty string pathname case (no changes needed)
  • packages/astro/test/units/routing/trailing-slash.test.js: Test already exists for this scenario (issue Query params trigger 404 error in dev #15095)

Testing

The fix ensures that:

  1. /base?query=test → strips to /?query=test → normalizes to empty string pathname for route matching ✅
  2. /base → strips to empty string → normalizes correctly ✅
  3. /base/ → strips to / → normalizes correctly ✅
  • tested with minimal example
{01D2EAEA-075C-46BD-A5E4-D6BE4694A946} {BEBBE330-54BE-4976-950F-B6ECB59B73BC} {2D884BBA-C1FE-490C-BCC6-467CE5C35C21}

Docs

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=42954461

@changeset-bot
Copy link

changeset-bot bot commented Jan 7, 2026

🦋 Changeset detected

Latest commit: 9926b6b

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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jan 7, 2026
@leonace924
Copy link
Contributor Author

@florian-lefebvre can you review my PR? please give me feedback
Thank you for your time

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 7, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing leonace924:fix/trailing-slash (9926b6b) with main (4eb6fc9)1

Summary

✅ 6 untouched benchmarks

Footnotes

  1. No successful run was found on main (8695285) during the generation of this report, so 4eb6fc9 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@florian-lefebvre
Copy link
Member

Hi, some tests are not passing. Can you fix them first?

@leonace924 leonace924 force-pushed the fix/trailing-slash branch 2 times, most recently from df5043d to 9aa7899 Compare January 7, 2026 22:48
@leonace924
Copy link
Contributor Author

leonace924 commented Jan 8, 2026

@florian-lefebvre I’ve added the test screenshot; please review it again.
All tests have passed.
Thank you for your time.

leonace924 and others added 6 commits January 8, 2026 08:51
Fixes issue where requests to /base?query=test would return 404 when
trailingSlash is set to 'never' or 'always'. The baseMiddleware now
ensures URLs always start with '/' after stripping the base path,
preserving query parameters for proper route matching.

Fixes withastro#15095
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
leonace924 and others added 2 commits January 8, 2026 08:52
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
@leonace924
Copy link
Contributor Author

Hi @florian-lefebvre would you review once more?
I noticed this test error is not related to my changes...
{B1A9B721-ED47-4D65-9B52-09632B8A2D2D}

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

These tests are flaky, nothing to worry about. Thank you!

@florian-lefebvre florian-lefebvre merged commit 81db3c0 into withastro:main Jan 8, 2026
24 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query params trigger 404 error in dev

2 participants