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

Middleware redirect does not work if the page file doesn't exist #7752

Closed
1 task done
raulfdm opened this issue Jul 21, 2023 · 7 comments · Fixed by #7841
Closed
1 task done

Middleware redirect does not work if the page file doesn't exist #7752

raulfdm opened this issue Jul 21, 2023 · 7 comments · Fixed by #7841
Assignees
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority)

Comments

@raulfdm
Copy link
Contributor

raulfdm commented Jul 21, 2023

What version of astro are you using?

2.9.0

Are you using an SSR adapter? If so, which one?

Not relevant

What package manager are you using?

pnpm

What operating system are you using?

Stackblitz

What browser are you using?

Not relevant

Describe the Bug

I have a project where I always want to redirect the user from the entry point (/) to a locale page (/en).

If the src/pages/index.astro doesn't exist, this redirect won't happen if we do this via middleware.

Is this correct? I searched in the documentation but I haven't found anything.

CleanShot.2023-07-21.at.19.12.41.mp4

What's the expected result?

No need to have an empty index to I can redirect.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-rfraau?file=src%2Fmiddleware.ts

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 Jul 21, 2023
@bmenant
Copy link
Contributor

bmenant commented Jul 21, 2023

Similar issue occurs when trying to enforce trailing slash from a middleware, e.g. to redirect /about to /about/ (or vice versa).
If Astro is configured with trailingSlash: 'always' then the redirect never happens and a (blank) 404 page is returned.
If Astro is configured with trailingSlash: 'ignore' then the redirect works fine.

@Princesseuh Princesseuh removed the needs triage Issue needs to be triaged label Jul 26, 2023
@fprl
Copy link
Contributor

fprl commented Jul 27, 2023

I agree that the middleware must be executed before Astro returns /404.

In this case, you can use redirects:

// astro.config.mjs
export default defineConfig({
  // rest of your config
 redirects: {
    '/': '/about,
  }
})

@raulfdm
Copy link
Contributor Author

raulfdm commented Jul 27, 2023

In this case, you can use redirects:

For that particular case it would be fine, but in my real word case I use middleware to determine based on the user accept-language if I should redirect from / to /pt or /en (localization)

@fprl
Copy link
Contributor

fprl commented Jul 27, 2023

For that particular case it would be fine, but in my real word case I use middleware to determine based on the user accept-language if I should redirect from / to /pt or /en (localization)

Yeah, I have something similar to that. Hopefully its possible to create a PR for this.

@ematipico
Copy link
Member

This seems to be a regression.

@ematipico ematipico added the - P4: important Violate documented behavior or significantly impacts performance (priority) label Jul 27, 2023
@ematipico ematipico self-assigned this Jul 27, 2023
@syahzuan
Copy link

Can I confirm that this was fixed? I see that this was patched in 2.9.5 and 2.9.6, but I am using v2.10.14 and still have the same issue.
Trying to redirect / (which does not exist) to the localized content (/en or /zh etc), but Astro returns 404 instead of running the middleware.
Trailing slash issue mentioned above by @bmenant also still persists.

Rule-out diagnosis:

  1. I tested this with other existing pages and my middleware works fine.
  2. I am using Cloudflare adapter, but issue persists when even when turned off.
  3. Tested both "server" or "hybrid", issue persists.

@ematipico
Copy link
Member

ematipico commented Aug 25, 2023

@syahzuan I'd suggest to open a new issue. It's also possible that your environment is different.

In fact, the issue was fixed in development. Your case is different because you're using the Cloudflare adapter.

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)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants