Skip to content

Lack of true Edge Middleware support blocks prerender + dynamic behavior (e.g. i18n redirects) #13774

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

Open
Papsanly opened this issue May 8, 2025 · 3 comments

Comments

@Papsanly
Copy link

Papsanly commented May 8, 2025

Describe the problem

Summary:

SvelteKit currently does not support a true equivalent of Next.js middleware.ts — a middleware layer that runs before a static asset is served (e.g. prerendered .html). This severely limits the ability to implement common use cases like internationalization redirects, A/B testing, geolocation-based content, and cookie inspection without giving up SSG.

Problem:

In Next.js, middleware.ts runs before a static page is served from the CDN. This allows developers to:

  • Read headers and cookies

  • Dynamically redirect (/ → /en)

  • And still serve prerendered content

In SvelteKit:

  • hooks.server.ts only runs on SSR or Edge runtime

  • But does not run at all for prerendered routes

So, to implement dynamic logic (e.g. locale detection), we must disable prerendering, which:

  • Slows down response times

  • Breaks CDN-level caching

Describe the proposed solution

Possible solutions:

  • Add a middleware.ts-like system that runs before routes are resolved, even for static pages

  • Allow limited hooks to run during static asset delivery when deployed via Vercel/Cloudflare

  • Add official integration with Vercel's Edge Middleware layer that plays nicely with SvelteKit’s router

Alternatives considered

As a result, we either:

  • Accept client-side FOUC (e.g. for i18n redirects)

  • Or lose SSG/CDN benefits by turning off prerendering

Next.js has solved this very well. SvelteKit currently has no equivalent.

Importance

i cannot use SvelteKit without it

Additional Information

Can the SvelteKit team provide an official position on this limitation, and whether "true edge middleware before static delivery" is on the roadmap?

@dummdidumm
Copy link
Member

What is your use case?
You can place a middleware.js file at the root and it will be picked up by vercel, it doesn't get recognized at dev time though.
Related: #13477 / #13430

@Papsanly
Copy link
Author

Papsanly commented May 8, 2025

My use case is just being able to do basic user locale detection from headers and redirects with static prerendering.

Regarding the middleware.js file, as said in sveltekit framework docs on vercel:

"Due to SvelteKit's client-side rendering, you cannot use Vercel's Edge Middleware with SvelteKit."

So it seems to me that the middleware.js file won't work, however I haven't actually tried it. I'll let you know if this resolves the issue for my use case.

@Papsanly
Copy link
Author

Papsanly commented May 8, 2025

Thank you for directing me to #13477!

Since the PR state is draft, that means that for now edge middlewares on vercel don't work until PR is merged? If so, that's all I wanted to know and I can close the issue.

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

No branches or pull requests

2 participants