修正:dev 環境改善(vite 對網路磁碟啟用 polling、Service Worker 僅正式版註冊)

摘要:
- vite dev server 啟用 watch.usePolling,讓 W: 網路磁碟(RaiDrive)的檔案變更能觸發 HMR
- Service Worker 改為僅在正式版(import.meta.env.PROD)才註冊,dev 不再註冊

根本原因:
- 專案放在 RaiDrive 掛載的網路磁碟,不會發出 fs 事件,vite 預設的檔案監看抓不到變更,HMR 失效
- dev 也註冊 SW 會用快取卡住舊版 bundle,改了程式看不到效果

影響:
- vite.config.ts:server.watch 加上 usePolling 與 interval
- src/main.tsx:SW 註冊條件加上 import.meta.env.PROD

修法:
- 開發時靠 polling 監看檔案;SW 只在 build 後的正式環境啟用

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-15 17:42:22 +08:00
parent 9eb60baa37
commit 304bcaeedd
2 changed files with 6 additions and 1 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ createRoot(document.getElementById('root')!).render(
</StrictMode>,
)
if ('serviceWorker' in navigator) {
if (import.meta.env.PROD && 'serviceWorker' in navigator) {
window.addEventListener('load', () => {
let refreshing = false
+5
View File
@@ -7,6 +7,11 @@ export default defineConfig({
server: {
port: 3501,
strictPort: true,
// W: 是 RaiDrive 網路磁碟,不會發 fs 事件,需用 polling 才能觸發 HMR
watch: {
usePolling: true,
interval: 300,
},
proxy: {
'/api': 'http://127.0.0.1:8788',
},