Skip to content

Commit bb7670f

Browse files
committed
fix: enhance service worker registration and deduplication for React dependencies
1 parent d6d4fdc commit bb7670f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

apps/web/src/main.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ const queryClient = new QueryClient({
2020
// Vercel → "/" (stripped to ""), Local → "/console/" (stripped to "/console")
2121
const basename = import.meta.env.BASE_URL.replace(/\/+$/, '');
2222

23-
// Register service worker for PWA offline support
24-
registerServiceWorker({
25-
onSuccess: () => console.log('[SW] Content cached for offline use'),
26-
onUpdate: () => console.log('[SW] New content available; please refresh'),
27-
});
23+
// Register service worker for PWA offline support (production only).
24+
// In dev mode the SW's cache-first strategy serves stale Vite pre-bundled
25+
// chunks, causing React hook mismatches and HMR WebSocket failures.
26+
if (import.meta.env.PROD) {
27+
registerServiceWorker({
28+
onSuccess: () => console.log('[SW] Content cached for offline use'),
29+
onUpdate: () => console.log('[SW] New content available; please refresh'),
30+
});
31+
}
2832

2933
createRoot(document.getElementById('root')!).render(
3034
<StrictMode>

apps/web/vite.config.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,33 @@ export default defineConfig({
1919
plugins: [react(), tailwindcss(), htmlBaseUrl()],
2020
base,
2121
resolve: {
22-
dedupe: ['react', 'react-dom'],
22+
// Ensure every dependency resolves to exactly ONE copy of React, its DOM
23+
// renderer, and the router — prevents "Invalid hook call" errors.
24+
dedupe: ['react', 'react-dom', 'react-router', 'react-router-dom'],
2325
alias: {
2426
'@': resolve(__dirname, 'src'),
2527
},
2628
},
29+
optimizeDeps: {
30+
// Force these into the Vite pre-bundle so they share a single React chunk.
31+
include: [
32+
'react',
33+
'react-dom',
34+
'react-dom/client',
35+
'react-router-dom',
36+
'better-auth/react',
37+
'better-auth/client/plugins',
38+
'@tanstack/react-query',
39+
],
40+
},
2741
server: {
2842
port: 5321,
43+
hmr: {
44+
// With base: '/console/' the default WS path becomes /console/ which
45+
// can fail behind proxies or when a stale Service Worker intercepts the
46+
// upgrade request. Use a dedicated path so HMR always connects.
47+
path: 'hmr',
48+
},
2949
proxy: {
3050
'/api/v1': {
3151
target: 'http://localhost:5320',

0 commit comments

Comments
 (0)