withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.62k stars 2.39k forks source link

When using the development server versus distributing static build artifacts, the behavior of redirects differs. #10576

Open taiga533 opened 5 months ago

taiga533 commented 5 months ago

Astro Info

Astro                    v4.5.10
Node                     v20.11.1
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

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

No response

Describe the Bug

"When using the development server versus distributing static build artifacts, the behavior of redirects differs.

With the following configuration, on the development server, the contents of old-page.astro (old-page/index.html) are displayed, but when a static build is performed and distributed on any server (or previewed using astro preview), old-page/index.html is displayed and then redirected to new-page.astro using the <meta http-equiv=\"refresh\"> tag.

# file-system
pages
├ old-page
└ new-page
// config
export default defineConfig({
  output: \"static\",
  redirects: {'/old-page': '/new-page'},
});

What's the expected result?

I believe that even on the development server, old-page.astro should be displayed and then redirected to new-page.astro using the <meta http-equiv=\"refresh\"> tag.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-yogmzg?file=astro.config.mjs

Participation

matthewp commented 5 months ago

Sounds reasonable, but I would expect a status code redirect still if output: 'server'. Would you be willing to implement this?

Lissy93 commented 4 months ago

I was facing something similar. No redirects working, unless the output is set to server (example CodeSandbox).

import { defineConfig } from "astro/config";

const output = "hybrid";  // Doesn't work, if set to hybrid or static.
// const output = "server"; // But works as expected if set to server.

const redirects = {
  "/old": "/new",
};

export default defineConfig({
  output,
  redirects,
});

Or if this is the expected behavior, maybe it could be mentioned in the redirect docs

Unless I'm just being silly and missing something really obvious(?)

I'd be willing to take a look at submitting a PR, if someone could point me in the right direction.