Skip to content

fix: prevent route guard from blocking pages that share names with root directories#15754

Merged
ematipico merged 5 commits intowithastro:mainfrom
rururux:fix-page-name-conflict
Mar 12, 2026
Merged

fix: prevent route guard from blocking pages that share names with root directories#15754
ematipico merged 5 commits intowithastro:mainfrom
rururux:fix-page-name-conflict

Conversation

@rururux
Copy link
Contributor

@rururux rururux commented Mar 4, 2026

fixes: #15655
Picked up from Houston AI suggestion.

Changes

Houston:

The root cause is in the dev server's route guard middleware (packages/astro/src/vite-plugin-astro-server/route-guard.ts). When a request comes in for a path like /test, the guard checks whether a file or directory exists at the project root with that name using fs.existsSync(). Since existsSync returns true for both files and directories, a directory named test/ at the project root causes the guard to return a 404, even though src/pages/test.astro is a valid page route. This only affects the dev server and only when a root-level directory name collides with a page route name.

The fix changes the route guard to use fs.statSync() instead of fs.existsSync(), and only blocks the request when the path resolves to an actual file (not a directory). Directories at the project root are allowed through so that page routes with matching names can be served normally.

Testing

In addition to Houston's test for when both a directory and a matching route exist,
I added a test to verify that a 404 is correctly returned when a directory exists at the project root but no matching route exists.

Docs

N/A, bug fix

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: af92a1d

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 Mar 4, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 4, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing rururux:fix-page-name-conflict (af92a1d) with main (ca76ff1)1

Open in CodSpeed

Footnotes

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

@ematipico ematipico merged commit 58f1d63 into withastro:main Mar 12, 2026
25 of 26 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Mar 12, 2026
@rururux
Copy link
Contributor Author

rururux commented Mar 13, 2026

Thank you! 🥳

@rururux rururux deleted the fix-page-name-conflict branch March 13, 2026 04:27
dadezzz pushed a commit to dadezzz/ice-notes that referenced this pull request Mar 17, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`6.0.3` → `6.0.4`](https://renovatebot.com/diffs/npm/astro/6.0.3/6.0.4) | ![age](https://developer.mend.io/api/mc/badges/age/npm/astro/6.0.4?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/astro/6.0.3/6.0.4?slim=true) |

---

### Release Notes

<details>
<summary>withastro/astro (astro)</summary>

### [`v6.0.4`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#604)

[Compare Source](https://github.com/withastro/astro/compare/astro@6.0.3...astro@6.0.4)

##### Patch Changes

- [#&#8203;15870](withastro/astro#15870) [`920f10b`](withastro/astro@920f10b) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Prebundle `astro/toolbar` in dev when custom dev toolbar apps are registered, preventing re-optimization reloads that can hide or break the toolbar.

- [#&#8203;15876](withastro/astro#15876) [`f47ac53`](withastro/astro@f47ac53) Thanks [@&#8203;ematipico](https://github.com/ematipico)! - Fixes `redirectToDefaultLocale` producing a protocol-relative URL (`//locale`) instead of an absolute path (`/locale`) when `base` is `'/'`.

- [#&#8203;15767](withastro/astro#15767) [`e0042f7`](withastro/astro@e0042f7) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Fixes server islands (`server:defer`) not working when only used in prerendered pages with `output: 'server'`.

- [#&#8203;15873](withastro/astro#15873) [`35841ed`](withastro/astro@35841ed) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Fix a dev server bug where newly created pages could miss layout-imported CSS until restart.

- [#&#8203;15874](withastro/astro#15874) [`ce0669d`](withastro/astro@ce0669d) Thanks [@&#8203;ematipico](https://github.com/ematipico)! - Fixes a warning when using `prefetchAll`

- [#&#8203;15754](withastro/astro#15754) [`58f1d63`](withastro/astro@58f1d63) Thanks [@&#8203;rururux](https://github.com/rururux)! - Fixes a bug where a directory at the project root sharing the same name as a page route would cause the dev server to return a 404 instead of serving the page.

- [#&#8203;15869](withastro/astro#15869) [`76b3a5e`](withastro/astro@76b3a5e) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Update the unknown file extension error hint to recommend `vite.resolve.noExternal`, which is the correct Vite 7 config key.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My41OS41IiwidXBkYXRlZEluVmVyIjoiNDMuNTkuNSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: Renovate Bot <renovate@zarantonello.dev>
Co-committed-by: Renovate Bot <renovate@zarantonello.dev>
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.

Page names conflict with folder names in the project root

3 participants