You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was able to reproduce this issue. When trailingSlash: "always" is configured with static output, endpoint routes (API routes like src/pages/demo.ts) produce a redirect HTML page instead of their actual response content. The root cause is in packages/astro/src/core/app/base.ts — the render() method's trailing slash redirect logic applies to all route types indiscriminately, including endpoints. During static prerendering, this causes the redirect HTML to be written to disk in place of the endpoint's actual output, and the redirect target (/demo/) doesn't even exist in the build output.
I was able to fix this issue. The fix adds a check to skip trailing slash redirects for endpoint routes (routeData?.type !== 'endpoint'), since endpoints serve raw content (JSON, text, etc.) and should not be redirected. All 33 trailing-slash unit tests pass, including a new test for this case. View Suggested Fix
P4: important. This bug affects any user combining trailingSlash: "always" (or "never") with API/endpoint routes in static output mode. The endpoint's response is silently replaced with broken redirect HTML, and the redirect target doesn't exist — resulting in complete data loss for the affected routes with no obvious error message during build. This is a common configuration pattern and the silent failure makes it especially impactful.
When using trailingSlash: "always" in astro.config.mjs with static output mode, API routes (.ts endpoint files) produce a redirect HTML page instead of the actual API response content. The issue only occurs during astro build, not during astro dev.
Reporter's Environment
Astro v6.1.3
Vite v7.3.1
Node v22.13.0
Linux (x64)
pnpm
Output: static
Adapter: none
Integrations: none
Reporter's Expected Result
The demo endpoint should render with its output of "hello" instead of redirecting to a non-existent page /demo/.
The bug is in packages/astro/src/core/app/base.ts, in the render() method of BaseApp (line ~389-395).
When a request comes in for an endpoint route like /demo, the redirectTrailingSlash() method computes a redirect target of /demo/ (because trailingSlash: "always" is configured). The code then unconditionally generates a redirect HTML response — this applies to ALL route types, including endpoints. During static prerendering, this redirect HTML replaces the endpoint's actual output.
Affected File
packages/astro/src/core/app/base.ts — render() method of BaseApp
Confidence: High
Verification
Verdict: Bug
No code comments explain that endpoints should receive trailing slash redirects
The behavior contradicts the purpose of API routes
The redirect target doesn't exist in static output
No prior GitHub issues or PRs indicate this was intentional
Fix
Added routeData?.type !== 'endpoint' condition to skip trailing slash redirects for endpoint routes. Added unit test confirming endpoints are not redirected.
- if (redirect !== url.pathname) {+ // Skip trailing slash redirect for endpoints — they serve raw content (JSON, text, etc.)+ // and should not be redirected. This especially matters during static prerendering where+ // the redirect HTML would replace the endpoint's actual output.+ if (redirect !== url.pathname && routeData?.type !== 'endpoint') {
All 33 trailing-slash unit tests pass.
This report was made by an LLM. Mistakes happen, check important info.
@astrobot-houston is it documented that trailingSlash config doesn't apply to endpoints? I recall that it doesn't apply to endpoints with a file extension, but not otherwise. Assuming it does apply then this fix is wrong. The problem instead seems to be that we don't create a correct route for the endpoint, the route should have a trailing slash in it.
Astro v5 behavior is to create dist/demo with hello.
One option is to create dist/demo/index.html with hello, without the redirect (cannot have a file demo & directory demo)
Which does statically render correctly
I was able to reproduce this issue. When trailingSlash: "always" is configured with static output, endpoint routes without file extensions (e.g., src/pages/demo.ts) produce redirect HTML instead of their actual response content. The root cause is in packages/astro/src/core/build/generate.ts — the getUrlForPath() function's endpoint branch unconditionally skipped all trailing slash handling, so the prerender URL was /demo instead of /demo/. This caused BaseApp.render() to detect a mismatch and return a 301 redirect HTML response, which was written to disk in place of the endpoint's actual output. The redirect target /demo/ doesn't even exist in the build output, resulting in silent data loss.
I was able to fix this issue. The fix updates getUrlForPath in generate.ts to apply trailing slash logic to endpoint routes without file extensions, matching how trailingSlashForPath in create-manifest.ts already generates route patterns. This ensures the prerender URL (/demo/) matches the route pattern (^\/demo\/$), so no redirect is triggered. Endpoints with file extensions (e.g., /sitemap.xml) are unaffected. This approach follows @matthewp's guidance — instead of skipping redirects for endpoints, the route URL is generated correctly from the start. View Suggested Fix
P4: important. This bug affects any user combining trailingSlash: "always" (or "never") with API/endpoint routes in static output mode. The endpoint's response is silently replaced with broken redirect HTML — no error or warning is produced during build — and the redirect target doesn't exist, resulting in complete data loss for affected routes. This is a common configuration pattern and the silent failure makes it especially impactful.
When using trailingSlash: "always" in astro.config.mjs with static output mode, API endpoint routes (.ts files exporting GET/POST etc.) produce a redirect HTML page instead of the actual API response content. The issue only occurs during astro build, not during astro dev.
Reporter's Environment
Astro v6.1.3
Vite v7.3.1
Node v22.13.0
Linux (x64)
pnpm
Output: static
Adapter: none
Integrations: none
Reporter's Expected Result
The demo endpoint should render with its output of "hello" instead of redirecting to a non-existent page /demo/.
"is it documented that trailingSlash config doesn't apply to endpoints? I recall that it doesn't apply to endpoints with a file extension, but not otherwise. Assuming it does apply then this fix is wrong. The problem instead seems to be that we don't create a correct route for the endpoint, the route should have a trailing slash in it."
This confirms the correct fix is to ensure the endpoint URL is generated with the proper trailing slash during static prerendering, not to skip the redirect for endpoints.
Reproduction Result: REPRODUCED
Steps to Reproduce
Created triage project at triage/gh-16185 by cloning the StackBlitz reproduction
Updated package.json to use astro: "workspace:*" for the monorepo version
Confirmed astro.config.mjs has trailingSlash: "always" set
Confirmed src/pages/demo.ts is an API endpoint returning new Response("hello")
Ran astro build
Checked contents of dist/demo
Actual Result (Bug Present)
With trailingSlash: "always", dist/demo contains redirect HTML:
<!doctype html><title>Redirecting to: /demo/</title><metahttp-equiv="refresh" content="0;url=/demo/">...
The redirect target /demo/ does not exist in the build output.
Baseline Verification (Bug Absent)
With trailingSlash commented out, dist/demo correctly contains hello.
Diagnosis
Root Cause — Confidence: High
The bug is caused by a mismatch between how the route manifest and the static build URL generation handle trailing slashes for endpoint routes without file extensions.
In create-manifest.ts:441-446, trailingSlashForPath correctly returns config.trailingSlash for non-extension endpoints, generating a route pattern like ^\/demo\/$.
However, in generate.ts:618-621, getUrlForPath() unconditionally skips trailing slash handling for endpoints:
This produces URL http://localhost:4321/demo (no trailing slash). Then BaseApp.render() detects the mismatch, returns a 301 redirect to /demo/, and the redirect HTML is written to disk instead of the endpoint's actual response.
Have a fix for this. Note that API endpoints without an extension in static mode are kind of an odd thing to do. I'm not sure of a use-case, but nevertheless it will build without the redirect after #16193.
Activity
astrobot-houston commentedon Apr 2, 2026
I was able to reproduce this issue. When
trailingSlash: "always"is configured with static output, endpoint routes (API routes likesrc/pages/demo.ts) produce a redirect HTML page instead of their actual response content. The root cause is inpackages/astro/src/core/app/base.ts— therender()method's trailing slash redirect logic applies to all route types indiscriminately, including endpoints. During static prerendering, this causes the redirect HTML to be written to disk in place of the endpoint's actual output, and the redirect target (/demo/) doesn't even exist in the build output.I was able to fix this issue. The fix adds a check to skip trailing slash redirects for endpoint routes (
routeData?.type !== 'endpoint'), since endpoints serve raw content (JSON, text, etc.) and should not be redirected. All 33 trailing-slash unit tests pass, including a new test for this case. View Suggested FixP4: important. This bug affects any user combining
trailingSlash: "always"(or"never") with API/endpoint routes in static output mode. The endpoint's response is silently replaced with broken redirect HTML, and the redirect target doesn't exist — resulting in complete data loss for the affected routes with no obvious error message during build. This is a common configuration pattern and the silent failure makes it especially impactful.Full Triage Report
Triage Report: Issue #16185 — trailingSlash breaks static output
Issue Details
Original Issue Description
When using
trailingSlash: "always"inastro.config.mjswith static output mode, API routes (.tsendpoint files) produce a redirect HTML page instead of the actual API response content. The issue only occurs duringastro build, not duringastro dev.Reporter's Environment
Reporter's Expected Result
The
demoendpoint should render with its output of "hello" instead of redirecting to a non-existent page/demo/.Reporter's Reproduction
StackBlitz URL: https://stackblitz.com/edit/github-dghg4t8k
The reproduction consists of a minimal Astro project with:
src/pages/demo.ts— an API route that returnsnew Response("hello")withContent-Type: text/plainastro.config.mjswithtrailingSlash: "always"Reproduction Steps
astro.config.mjstotrailingSlash: "always"astro builddist/demofile contentsResult with bug:
dist/democontains redirect HTML:Baseline (without
trailingSlash):dist/democorrectly containshello.Diagnosis
Root Cause
The bug is in
packages/astro/src/core/app/base.ts, in therender()method ofBaseApp(line ~389-395).When a request comes in for an endpoint route like
/demo, theredirectTrailingSlash()method computes a redirect target of/demo/(becausetrailingSlash: "always"is configured). The code then unconditionally generates a redirect HTML response — this applies to ALL route types, including endpoints. During static prerendering, this redirect HTML replaces the endpoint's actual output.Affected File
packages/astro/src/core/app/base.ts—render()method ofBaseAppConfidence: High
Verification
Verdict: Bug
Fix
Added
routeData?.type !== 'endpoint'condition to skip trailing slash redirects for endpoint routes. Added unit test confirming endpoints are not redirected.All 33 trailing-slash unit tests pass.
This report was made by an LLM. Mistakes happen, check important info.
dataCenter430 commentedon Apr 2, 2026
Hi, @matthewp
I'd love to work on this.
matthewp commentedon Apr 2, 2026
@astrobot-houston is it documented that trailingSlash config doesn't apply to endpoints? I recall that it doesn't apply to endpoints with a file extension, but not otherwise. Assuming it does apply then this fix is wrong. The problem instead seems to be that we don't create a correct route for the endpoint, the route should have a trailing slash in it.
matthewp commentedon Apr 2, 2026
@astrobot-houston answer the above 👆
tyler71 commentedon Apr 2, 2026
Astro v5 behavior is to create
dist/demowithhello.One option is to create
dist/demo/index.htmlwithhello, without the redirect (cannot have a file demo & directory demo)Which does statically render correctly
astrobot-houston commentedon Apr 2, 2026
I was able to reproduce this issue. When
trailingSlash: "always"is configured with static output, endpoint routes without file extensions (e.g.,src/pages/demo.ts) produce redirect HTML instead of their actual response content. The root cause is inpackages/astro/src/core/build/generate.ts— thegetUrlForPath()function's endpoint branch unconditionally skipped all trailing slash handling, so the prerender URL was/demoinstead of/demo/. This causedBaseApp.render()to detect a mismatch and return a 301 redirect HTML response, which was written to disk in place of the endpoint's actual output. The redirect target/demo/doesn't even exist in the build output, resulting in silent data loss.I was able to fix this issue. The fix updates
getUrlForPathingenerate.tsto apply trailing slash logic to endpoint routes without file extensions, matching howtrailingSlashForPathincreate-manifest.tsalready generates route patterns. This ensures the prerender URL (/demo/) matches the route pattern (^\/demo\/$), so no redirect is triggered. Endpoints with file extensions (e.g.,/sitemap.xml) are unaffected. This approach follows @matthewp's guidance — instead of skipping redirects for endpoints, the route URL is generated correctly from the start. View Suggested FixP4: important. This bug affects any user combining
trailingSlash: "always"(or"never") with API/endpoint routes in static output mode. The endpoint's response is silently replaced with broken redirect HTML — no error or warning is produced during build — and the redirect target doesn't exist, resulting in complete data loss for affected routes. This is a common configuration pattern and the silent failure makes it especially impactful.Full Triage Report
Triage Report: Issue #16185 — trailingSlash breaks static output
Issue Details
Original Issue Description
When using
trailingSlash: "always"inastro.config.mjswith static output mode, API endpoint routes (.tsfiles exportingGET/POSTetc.) produce a redirect HTML page instead of the actual API response content. The issue only occurs duringastro build, not duringastro dev.Reporter's Environment
Reporter's Expected Result
The
demoendpoint should render with its output of "hello" instead of redirecting to a non-existent page/demo/.Reporter's Reproduction
StackBlitz URL: https://stackblitz.com/edit/github-dghg4t8k
The reproduction consists of a minimal Astro project with:
src/pages/demo.ts— an API route returningnew Response("hello")withContent-Type: text/plainastro.config.mjswithtrailingSlash: "always"Relevant Comments from the Issue
Maintainer @matthewp commented:
This confirms the correct fix is to ensure the endpoint URL is generated with the proper trailing slash during static prerendering, not to skip the redirect for endpoints.
Reproduction Result: REPRODUCED
Steps to Reproduce
triage/gh-16185by cloning the StackBlitz reproductionpackage.jsonto useastro: "workspace:*"for the monorepo versionastro.config.mjshastrailingSlash: "always"setsrc/pages/demo.tsis an API endpoint returningnew Response("hello")astro builddist/demoActual Result (Bug Present)
With
trailingSlash: "always",dist/democontains redirect HTML:The redirect target
/demo/does not exist in the build output.Baseline Verification (Bug Absent)
With
trailingSlashcommented out,dist/democorrectly containshello.Diagnosis
Root Cause — Confidence: High
The bug is caused by a mismatch between how the route manifest and the static build URL generation handle trailing slashes for endpoint routes without file extensions.
In
create-manifest.ts:441-446,trailingSlashForPathcorrectly returnsconfig.trailingSlashfor non-extension endpoints, generating a route pattern like^\/demo\/$.However, in
generate.ts:618-621,getUrlForPath()unconditionally skips trailing slash handling for endpoints:This produces URL
http://localhost:4321/demo(no trailing slash). ThenBaseApp.render()detects the mismatch, returns a 301 redirect to/demo/, and the redirect HTML is written to disk instead of the endpoint's actual response.Affected File
packages/astro/src/core/build/generate.ts:618-621—getUrlForPath()endpoint branch doesn't apply trailing slash configVerification
Verdict: Bug
trailingSlashconfigtrailingSlashto non-extension endpointsFix
Updated
getUrlForPathingenerate.tsto apply trailing slash logic for non-extension endpoints:} else if (routeType === 'endpoint') { const buildPathRelative = removeLeadingForwardSlash(pathname); - buildPathname = joinPaths(base, buildPathRelative); + let endpointPathname = joinPaths(base, buildPathRelative); + if (trailingSlash === 'always' && !hasFileExtension(pathname)) { + endpointPathname = appendForwardSlash(endpointPathname); + } else if (trailingSlash === 'never') { + endpointPathname = removeTrailingForwardSlash(endpointPathname); + } + buildPathname = endpointPathname; }All existing tests pass (trailing-slash, endpoint routing, generator, route-manifest, router-match, preview-routing, dynamic-endpoint-collision, static-build).
This report was made by an LLM. Mistakes happen, check important info.
matthewp commentedon Apr 2, 2026
Have a fix for this. Note that API endpoints without an extension in static mode are kind of an odd thing to do. I'm not sure of a use-case, but nevertheless it will build without the redirect after #16193.