Skip to content

trailingSlash breaks static output #16185

Closed

Description

@tyler71

Astro Info

Astro                    v6.1.3
Vite                     v7.3.1
Node                     v22.13.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Astro >6 breaks static output with trailing slash.
Issue does not occur in serve, only when building.

$ cat src/pages/demo.ts
import type { APIRoute } from "astro";

export const GET: APIRoute = async () => {
    const response = new Response("hello");
    response.headers.set("Content-Type", "text/plain");
    return response;
};

Disabled:

export default defineConfig({
//  trailingSlash: "always",
});
$ cat dist/demo
hello

Enabled:

export default defineConfig({
  trailingSlash: "always",
});
$ cat dist/demo
<!doctype html><title>Redirecting to: /demo/</title><meta http-equiv="refresh" content="0;url=/demo/"><meta name="robots" content="noindex"><link rel="canonical" href="/demo/"><body>	<a href="/demo/">Redirecting from <code>/demo</code> to <code>/demo/</code></a></body>%

What's the expected result?

demo should render with its output of "hello" instead of redirecting to a non-existent page.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-dghg4t8k

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. added
    triage: needs triage
    on Apr 2, 2026
  2. astrobot-houston commented on Apr 2, 2026

    @astrobot-houston
    Contributor

    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.

    Full Triage Report

    Triage Report: Issue #16185 — trailingSlash breaks static output

    Issue Details

    Original Issue Description

    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/.

    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 returns new Response("hello") with Content-Type: text/plain
    • astro.config.mjs with trailingSlash: "always"

    Reproduction Steps

    1. Created triage project from StackBlitz reproduction
    2. Set astro.config.mjs to trailingSlash: "always"
    3. Ran astro build
    4. Checked dist/demo file contents

    Result with bug: dist/demo contains redirect HTML:

    <!doctype html><title>Redirecting to: /demo/</title><meta http-equiv="refresh" content="0;url=/demo/">...

    Baseline (without trailingSlash): dist/demo correctly contains hello.

    Diagnosis

    Root Cause

    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.

  3. added
    - P4: important
    pkg: astro
    and removed
    triage: needs triage
    on Apr 2, 2026
  4. dataCenter430 commented on Apr 2, 2026

    @dataCenter430
    Contributor

    Hi, @matthewp
    I'd love to work on this.

  5. matthewp commented on Apr 2, 2026

    @matthewp
    Contributor

    @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.

  6. self-assigned this
    on Apr 2, 2026
  7. added
    triage: needs triage
    on Apr 2, 2026
  8. matthewp commented on Apr 2, 2026

    @matthewp
    Contributor

    @astrobot-houston answer the above 👆

  9. tyler71 commented on Apr 2, 2026

    @tyler71
    Author

    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

    $ tree dist
    dist
    ├── demo
    │   └── index.html
    $ curl localhost:8000/demo/
    hello
  10. astrobot-houston commented on Apr 2, 2026

    @astrobot-houston
    Contributor

    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.

    Full Triage Report

    Triage Report: Issue #16185 — trailingSlash breaks static output

    Issue Details

    Original Issue Description

    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/.

    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 returning new Response("hello") with Content-Type: text/plain
    • astro.config.mjs with trailingSlash: "always"

    Relevant Comments from the Issue

    Maintainer @matthewp commented:

    "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

    1. Created triage project at triage/gh-16185 by cloning the StackBlitz reproduction
    2. Updated package.json to use astro: "workspace:*" for the monorepo version
    3. Confirmed astro.config.mjs has trailingSlash: "always" set
    4. Confirmed src/pages/demo.ts is an API endpoint returning new Response("hello")
    5. Ran astro build
    6. Checked contents of dist/demo

    Actual Result (Bug Present)

    With trailingSlash: "always", dist/demo contains redirect HTML:

    <!doctype html><title>Redirecting to: /demo/</title><meta http-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:

    } else if (routeType === 'endpoint') {
        const buildPathRelative = removeLeadingForwardSlash(pathname);
        buildPathname = joinPaths(base, buildPathRelative);
    }

    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.

    Affected File

    • Primary: packages/astro/src/core/build/generate.ts:618-621 — getUrlForPath() endpoint branch doesn't apply trailing slash config

    Verification

    Verdict: Bug

    1. Documentation says non-extension endpoints respect trailingSlash config
    2. The route manifest correctly applies trailingSlash to non-extension endpoints
    3. The URL construction has a gap — no developer chose to skip trailing slash for endpoints
    4. Maintainer @matthewp confirmed this is a bug
    5. The failure is silent and destructive — no build warning, complete data loss

    Fix

    Updated getUrlForPath in generate.ts to 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.

  11. removed
    triage: needs triage
    on Apr 2, 2026
  12. matthewp commented on Apr 2, 2026

    @matthewp
    Contributor

    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.

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

Metadata

Metadata

Assignees

Labels

- P4: importantpkg: astro

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions