Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Astro.rewrite does not respect the base path in configuration #13033

Open
1 task
fa0311 opened this issue Jan 21, 2025 · 3 comments
Open
1 task

Astro.rewrite does not respect the base path in configuration #13033

fa0311 opened this issue Jan 21, 2025 · 3 comments
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: routing Related to Astro routing (scope)

Comments

@fa0311
Copy link

fa0311 commented Jan 21, 2025

Astro Info

Astro                    v5.1.8
Node                     v23.1.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             none

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

Describe the Bug

When using Astro.rewrite, the configured base path is not considered. Below is a minimal example demonstrating the issue:

astro.config.mjs

import { defineConfig } from 'astro/config';
export default defineConfig({
    base: "/base",
});

src/pages/aaa.astro

---
const url = Astro.url;
---
{url}

src/pages/bbb.astro

---
return Astro.rewrite('/aaa');
---

Steps to Reproduce

  1. Run the Astro application with the above configuration.
  2. Access http://localhost:4322/base/bbb.

Expected Behavior

The page should output:

http://localhost:4322/base/aaa

Actual Behavior

The page outputs:

http://localhost:4322/aaa

It appears that Astro.rewrite does not respect the base path specified in the configuration.

Would appreciate any clarification or fixes for this behavior.

What's the expected result?

  1. Run the Astro application with the above configuration.
  2. Access http://localhost:4322/base/bbb.

The page should output:

http://localhost:4322/base/aaa

Link to Minimal Reproducible Example

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

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 21, 2025
@ematipico
Copy link
Member

You're right, Astro.url, during a rewrite, should contain base

@ematipico ematipico added - P4: important Violate documented behavior or significantly impacts performance (priority) feat: routing Related to Astro routing (scope) labels Jan 21, 2025
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Jan 21, 2025
@fa0311
Copy link
Author

fa0311 commented Jan 22, 2025

Workaround

By explicitly including the base path in the rewrite, the issue can be resolved:

Updated src/pages/bbb.astro

---
+ return Astro.rewrite('/base/aaa');
- return Astro.rewrite('/aaa');
---

@fa0311
Copy link
Author

fa0311 commented Jan 22, 2025

Issue with trailingSlash: "always"

If trailingSlash is set to "always" in the configuration, the above workaround does not work properly:

Updated astro.config.mjs

import { defineConfig } from 'astro/config';
export default defineConfig({
    base: "/base",
+     trailingSlash: "always",
});

Updated src/pages/bbb.astro

---
+ return Astro.rewrite('/base/aaa/');
- return Astro.rewrite('/base/aaa');
---

When accessing http://localhost:4322/base/bbb/, the following problems occur:

  1. A 404: Not Found error is returned.
  2. The following strange warning is logged:
15:40:28 [WARN] `Astro.request.headers` was used when rendering the route `src/pages/bbb.astro`. `Astro.request.headers` is not available on prerendered pages. If you need access to request headers, make sure that the page is server-rendered using `export const prerender = false;` or by setting `output` to `"server"` in your Astro config to make all your pages server-rendered by default.
15:40:28 [404] (rewrite) /bbb/ 5ms

This suggests further inconsistencies in how Astro.rewrite handles base paths and trailing slashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: routing Related to Astro routing (scope)
Projects
None yet
Development

No branches or pull requests

2 participants