Share: How to Fix Service Worker Precache Causing Incorrect Locale Cookie Setting with next-intl #2224
bolechen
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Context:
The Problem
When using next-intl with localePrefix: "as-needed" mode alongside Serwist Service Worker:
/en/all, /en/chat)
Root Cause
The issue occurs because:
The Solution
We implemented a two-layer fix:
1. Service Worker Layer (src/sw/index.ts)
Exclude locale-prefixed routes from precaching:
Why this works:
2. Middleware Layer (src/proxy.ts)
Explicitly set cookies for default locale, skip prefetch requests:
Why this works:
Key Takeaways
precache
This fix ensures that Service Worker precaching doesn't interfere with user language preferences, providing a consistent i18n
experience across all locales.
Beta Was this translation helpful? Give feedback.
All reactions