Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enable hot reload for vite react project instead of page reload

I am new to vite and I just started a new react application. My project had hmr (hot module replacement) enabled and it was ok. I just added some changes but when I start it now the hmr is disabled and when adding new change the browser is reloading (not updating fast) and in the terminal it logs: 12:37:54 PM [vite] page reload src/App.tsx I created a new test application and it has hmr enabled and when I add any change it logs: 12:35:23 PM [vite] hmr update /src/App.tsx (x2) Can any you tell me how to enable hmr instead of page reload?

Here is my vite.config.ts for project that logs page reload

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()]
})

and also tsconfig.json for project that logs page reload


{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["./src"]
}
like image 350
Mahdi-Jafaree Avatar asked Sep 12 '25 12:09

Mahdi-Jafaree


1 Answers

To enable the hot reload, you need to put this configuration in your vite.config.ts

export default defineConfig({
  plugins: [react()],
  server: {
    watch: {
      usePolling: true
    }
  }
})
like image 188
alvescleiton Avatar answered Sep 14 '25 02:09

alvescleiton