Skip to content
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

Hot Reload shows in terminal but do not updates browser #7839

Closed
7 tasks done
daniloribeiro00 opened this issue Apr 21, 2022 · 63 comments
Closed
7 tasks done

Hot Reload shows in terminal but do not updates browser #7839

daniloribeiro00 opened this issue Apr 21, 2022 · 63 comments
Labels
bug: upstream Bug in a dependency of Vite feat: hmr

Comments

@daniloribeiro00
Copy link

daniloribeiro00 commented Apr 21, 2022

Describe the bug

I have a Vue.js 3 + Vite + Tailwind CSS 3 project that uses a third party components library (also built with Vue.js 3 and Tailwind CSS 3) from my company's local NPM.

The problem is when I change the class or any prop that affects the classes of the imported component in my project, the VSCode terminal and the browser terminal both show
hmr update /src/views/.../fileName.vue
hmr update /src/tailwind.css

But the browser does not update and I always have to manually refresh the page with F5 to see the changes.

It works correctly if I change the class of any element or component from my own project, the issue is ONLY with the third party library.

This is my vite.config.js

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
const path = require('path');

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  server: {
    port: 8080,
  },
});

Does anyone know how to make the browser reflects the changes?

------- edit: added reproduction link from rtek

Reproduction

https://stackblitz.com/edit/vite-auuvqt?file=src%2Fcomponents%2FWrapper.vue

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz   
    Memory: 6.53 GB / 15.78 GB
  Binaries:
    Node: 16.14.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.5.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (100.0.1185.44)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @vitejs/plugin-vue: ^2.0.0 => 2.3.1
    vite: ^2.9.5 => 2.9.5

Used Package Manager

pnpm

Logs

vite:load 1.18ms [fs] /src/views/Monitoring/MonitoringShow.vue +6s
  vite:hmr [self-accepts] src\views\Monitoring\MonitoringShow.vue +57ms
  vite:import-analysis 13.99ms [8 imports rewritten] src\views\Monitoring\MonitoringShow.vue +6s   
  vite:cache [memory] /node_modules/.vite/deps/vue.js?v=59cbbbfb +6s
  vite:cache [memory] /node_modules/.vite/deps/date-fns.js?v=59cbbbfb +0ms
  vite:cache [memory] /node_modules/.vite/deps/date-fns_locale_pt-BR.js?v=59cbbbfb +0ms
  vite:cache [memory] /node_modules/.vite/deps/@fortawesome_free-solid-svg-icons.js?v=59cbbbfb +0ms
  vite:cache [memory] /node_modules/.vite/deps/@design-system_ui.js?v=59cbbbfb +0ms
  vite:cache [memory] /src/components/Dashboard.vue +1ms
  vite:cache [memory] /src/services/api.ts +0ms
  vite:cache [memory] plugin-vue:export-helper +4ms
  vite:transform 40.79ms /src/views/Monitoring/MonitoringShow.vue +6s
  vite:time 47.56ms /src/views/Monitoring/MonitoringShow.vue?import +6s
  vite:load 1.90ms [fs] /src/tailwind.css +51ms
  vite:hmr [self-accepts] src\tailwind.css +125ms
  vite:import-analysis 0.68ms [0 imports rewritten] src\tailwind.css +122ms
  vite:transform 103.92ms /src/tailwind.css +113ms
  vite:time 110.01ms /src/tailwind.css?import +111ms
  vite:cache [memory] /src/views/Monitoring/MonitoringShow.vue +119ms      
  vite:time 2.19ms /src/views/Monitoring/MonitoringShow.vue?import +5ms    
  vite:cache [memory] /src/tailwind.css +8ms
  vite:time 2.48ms /src/tailwind.css?import +8ms

Validations

@rtek
Copy link

rtek commented Apr 21, 2022

Jumping in on this since I found this same behavior while evaluating vue and distilled it down to this contrived example.

https://stackblitz.com/edit/vite-auuvqt?file=src%2Fcomponents%2FWrapper.vue

There is something about looping over useSlots().default() that's breaking the HMR. Again - just evaluating here so I don't understand what's really going on - but I've had all sorts of HMR issues with many popular components and they seem to use this slot looping pattern.

@0773
Copy link

0773 commented Apr 21, 2022

I have the same problem, I use element-plus
2

@sapphi-red
Copy link
Member

Looks like it's an upstream issue since it happens not only with Vite.
vuejs/core#5767 (comment)

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite and removed pending triage labels Apr 21, 2022
@adrianocr
Copy link

Interestingly I'm having the same HMR issue but with react.

If I downgrade to vite@2.6.14 it works as expected. But in the new versions I see the terminal notice but nothing happens in the browser. Replaced it with preact (just to test) and the same thing so I believe the issue might be with vite's HMR or the @preact/preset-vite and/or @vitejs/plugin-react-refresh packages.

@3lang3
Copy link

3lang3 commented Apr 24, 2022

Interestingly I'm having the same HMR issue but with react.

If I downgrade to vite@2.6.14 it works as expected. But in the new versions I see the terminal notice but nothing happens in the browser. Replaced it with preact (just to test) and the same thing so I believe the issue might be with vite's HMR or the @preact/preset-vite and/or @vitejs/plugin-react-refresh packages.

I'm having json hmr issue with react

@daniloribeiro00
Copy link
Author

Interestingly I'm having the same HMR issue but with react.

If I downgrade to vite@2.6.14 it works as expected. But in the new versions I see the terminal notice but nothing happens in the browser. Replaced it with preact (just to test) and the same thing so I believe the issue might be with vite's HMR or the @preact/preset-vite and/or @vitejs/plugin-react-refresh packages.

I tried to downgrade to 2.6.14 but it still doesn’t work as expected. 🤦🏼‍♂️

@dangenendt
Copy link

dangenendt commented Apr 26, 2022

Also exact same issue here with React.
browser console shows me "[vite] hot updated: /src/components/FooBar.tsx" but the component is not getting rerendered (?).
After hard page reload -> component change is visible 🧐

Vite 2.9.5
Vite Plugin-React: 1.3.1

@BigKamil5
Copy link

BigKamil5 commented Apr 29, 2022

Same issue here. For me it happens sometimes, when copying nested components from one project/folder to another, when hmr is running. It seems like vite has some weird problem with new component files with deps
The weirdest part is that nothing really helps with it. Restarting dev server does nothing. Clear '.vite' or whole 'node_modules' does nothig. Clearing browser cache, still nothing. Clearing npm cache, nope
One things that help is to move component, which hmr 'forgot' to another folder. For some reason this works like a charm for me... but it's so frustrating, pls help :')

@chemeng
Copy link

chemeng commented Jun 22, 2022

any updates on this one? for me it only happens with .vue files, .js files appear to be causing a full reload

@tannineo
Copy link

@adrianocr @3lang3 @daniloribeiro00

TL;DR: You might need to change the component export to export default.

I found the HMR issue with template preact-ts and find a discussion thread might be related:

#4577 (comment)

I tested on the vite template preact-ts, when template inits, HMR is not working,
but when I change to export default it works well.

@daniloribeiro00
Copy link
Author

@tannineo

The components I'm using are from a external lib, so there's no way to change the exports

@0773
Copy link

0773 commented Jul 20, 2022

I have the same problem, I use element-plus
2

bug fixed in vite 3.0

@daniloribeiro00
Copy link
Author

daniloribeiro00 commented Jul 20, 2022

I've tried again with Vite 3.0.2. It kinda works for some props but not all of them. For example, Tailwind CSS classes do not update and I have to reload the entire page. It used to work for everything.

@dbomfim
Copy link

dbomfim commented Aug 13, 2022

Hey folks! Yesterday I experienced something related, but really weird. When saving the file, browser was updated, but duplicating the component on screen. Today, it isn't updating at all... It's brand new project, created using vue3 initiator.
I will make some tests creating using vite. For now, looks like the bug still persists.

@tintin10q
Copy link

tintin10q commented Aug 18, 2022

This just started happening to me as well. The updates only come through when I restart my dev server. I tried updating to vite 3.0.8 but that did nothing.

@kresli
Copy link

kresli commented Aug 22, 2022

It started happening to me as well. Disabled browser cache but still receiving old component. Terminal log page reload successfully and browser auto refresh works as expected. The issue must be with serving those files via server.

EDIT:
To fix this issue you need to rename file to something else and then rename it back. I'm not sure why but this fixed the issue.

@artisin
Copy link

artisin commented Aug 25, 2022

Ran into the same issue and resolved it by ensuring the proper file permissions. I’m using gulp to preprocess my styles and the output file permission was 755 (-rwxr-xr-x) but by changing the output file permission to 644 (-rw-r--r--) fixed my issues.

EDIT:
Meh, I spoke too soon. Unfortunately I lack the time to debug the issue so for now a good old-fashioned link tag will have to do.

@Manuelbaun
Copy link

I had the same issue, up till vite 3.0.8. The bug seems to be fixed in the 3.0.9 version, which just came out a couple of days ago.

@dqm07
Copy link

dqm07 commented Aug 29, 2022

in my project, the React/ReactDOM are all from external, so when i use change my local code the update msg is shown at the terminal but no update in the browser.

i got the reason is ReactDOM from external is production mode and not support refresh features, what can i do cuz i have to use external for the historical reasons?

@daniloribeiro00
Copy link
Author

I had the same issue, up till vite 3.0.8. The bug seems to be fixed in the 3.0.9 version, which just came out a couple of days ago.

For me v3.0.9 fixed the issue. I'm closing this thread.

@zachbryant
Copy link

zachbryant commented Sep 6, 2022

Still experiencing this in v3.1.0. Just pinned to 3.0.9 and also not working for me.

@patak-dev
Copy link
Member

@zachbryant could you open a new bug report with a minimal repro against 3.1?

@quroom
Copy link

quroom commented Nov 1, 2022

Hey did anybody find the solution?

Have you ever tried this?
I solved problem with this way below.

solution for me
Remove that folder includes the code is not working.
And then create all codes with new folder.
It's working to me.

Remove router folder and all files in that folder.
Remove views folder and all files in that folder.
Create those files.
Not It seems to work perfectly.
I guess some code location hashes, to find path fastly, are broken

@steffenstolze
Copy link

We were facing the same issue with our views folder within a very minimal app.

This doesn't work (changes are not reflected)

image

It works if you save the files inside views as lowercase:

image

Changes to files inside components and layouts are reflected, even though they are uppercase:

image

@ldgmaia
Copy link

ldgmaia commented Nov 3, 2022

Why this issue is closed since is not working yet? Using version 3.2.2 with React

@daniloribeiro00
Copy link
Author

@ldgmaia open again, even though somehow it works for me now

@dxlliv
Copy link

dxlliv commented Nov 3, 2022

Remove everything from App.vue and main.ts, then recompose them gradually.
My issue was with vue3-simple-icons global components registration

@sapphi-red
Copy link
Member

The original repro is fixed by vuejs/core#4077.

For other people having issues should create a new issue with reproduction as it's a different issue.

Also for react ones, I guess those are duplicate of vitejs/vite-plugin-react#20, vitejs/vite-plugin-react#21, vitejs/vite-plugin-react#6.

@btonasse
Copy link

Does anybody know how to fix this? This bug is clearly not fixed

@ldgmaia
Copy link

ldgmaia commented Nov 19, 2022

Does anybody know how to fix this? This bug is clearly not fixed

I got it fixed. Actually it was my mistake. Maybe I can help. What is your stack?

@btonasse
Copy link

Does anybody know how to fix this? This bug is clearly not fixed

I got it fixed. Actually it was my mistake. Maybe I can help. What is your stack?

Thanks, Diego. I have just created a typescriptreact app and changed nothing in the default config. The only dependency added was styled-components.

Whenever I make a change in main.tsx or App.tsx, hot reload works as expected (and the terminal outputs [vite] hmr update /src/main.tsx). However, if the change happens in one of App's children, the terminal will read [vite] hmr update /src/UpdatedChild.tsx but the browser will not be updated. Only a manual refresh (or a change to main.tsx/App.tsx) will force it to update.

@ldgmaia
Copy link

ldgmaia commented Nov 19, 2022

If I could see your code, would help, but let's try. In the file where you are importing this child component, can you check if the import is correct, even capital letters?

@btonasse
Copy link

btonasse commented Nov 19, 2022

If I could see your code, would help, but let's try. In the file where you are importing this child component, can you check if the import is correct, even capital letters?

You're right, I should have provided some code. In any case, I managed to isolate the issue down to styled-components. See snippets below:

// src/components/Comptest.tsx
// Changing the props of ChildButton does NOT trigger a reload

import React from "react";
import styled from "styled-components";

interface Props {
    text: string;
}

const StyledButton = styled.button`
    color: red;
`;

class ChildButton extends React.Component<Props> {
    render() {
        return <StyledButton>{this.props.text}</StyledButton>;
    }
}

export default class ParentDiv extends React.Component {
    render() {
        return (
            <div>
                <ChildButton text="Btn1" />
                <ChildButton text="Btn2" />
                <ChildButton text="Btn3" />
            </div>
        );
    }
}
// src/components/Comptest.tsx
// Changing the props of ChildButton DOES trigger a reload

import React from "react";

interface Props {
    text: string;
}

class ChildButton extends React.Component<Props> {
    render() {
        return <button>{this.props.text}</button>;
    }
}

export default class ParentDiv extends React.Component {
    render() {
        return (
            <div>
                <ChildButton text="Btn1" />
                <ChildButton text="Btn2" />
                <ChildButton text="Btn3" />
            </div>
        );
    }
}

Do I need any extra configuration to make Vite work with styled-components ?

@aihrig
Copy link

aihrig commented Nov 25, 2022

I’m also having the same problem with plain JS app. Vite shows a change to the child component but does not cause a re-render to parents.

I’m using:

  • Vite/3.2.3
  • Linux-x64 (Ubuntu on wsl2 - files on the wsl storage, not going through windows mapping)
  • node-v18.8.0
  • React v18.2.0
  • @vitejs/plugin-react v2.2.0
  • postcss v8.4.19

I’ve tried moving the files and updating imports, and renaming files to lower case - as mentioned in some comments above. Casing matches files and imports. I’ve removed node_modules and reinstalled (many times).

No luck with any of that.

If there’s anything further I can add to help track this issue down I’d be happy to post.

@ldgmaia
Copy link

ldgmaia commented Nov 25, 2022

btonasse

Hi, sorry I couldn't reply earlier. Health issues happened.
I took your code and applied the way I would do it and it worked correctly. You can check it at https://github.com/ldgmaia/btonasse-test

I usually don't use classes like you, but I hope this helps you.

@lehni
Copy link

lehni commented Dec 6, 2022

I am experiencing the same problem and after a lot of digging in the internals I noticed that the path inside the deps that acceptedPath is being matched against is missing the version query parameter. So I tried this simple hack out that patches the incoming updates and 🎉, it works for me!

Now why the stored deps are missing this part of the URL, I cannot say…

if (import.meta.hot) {
  // Workaround for a strange Vite hot-reloading bug, see:
  // https://github.com/vitejs/vite/issues/7839#issuecomment-1340109679
  import.meta.hot.on('vite:beforeUpdate', onBeforeViteUpdate)
}

function onBeforeViteUpdate(event) {
  if (event.type === 'update') {
    // Patch `event.updates` to remove the version query parameter from path,
    // so that the update gets picked up.
    const updates = []
    for (const update of event.updates) {
      updates.push(update) // Keep the original update.
      const acceptedPath = update.acceptedPath.replace(/\?v=[0-9a-f]+&/i, '?')
      if (acceptedPath !== update.acceptedPath) {
        updates.push({ ...update, acceptedPath })
      }
    }
    event.updates = updates
  }
}

@dajpes
Copy link

dajpes commented Dec 7, 2022

@lehni

Where did you apply that change?

@AlbinCederblad
Copy link

AlbinCederblad commented Dec 7, 2022

Experiencing the same thing here. I have to manually refresh browser to see changes... 😢

"vite": "^3.1.4"

@AlbinCederblad
Copy link

Experiencing the same thing here. I have to manually refresh browser to see changes... 😢

"vite": "^3.1.4"

Ah my issue was that for some reason my code was trying to use a variable before it was initialized, shuffled some stuff around and now it's working correctly again..
Very very strange bug. I initialized a new project with react-ts, copied over my old code and there it's working fine.

@Developifyer
Copy link

Developifyer commented Dec 8, 2022

For me, Vite won't hot reload imported Class components but does fine with imported functional components. It will reload a Class component if I save the main.jsx (importing file) but not the module I'm currently working on. I'm assuming it has something to do with Class components using the render() function instead of relegating that task to the importing module? Any ideas?

Edit: Is there a way to set it up so that hmr always updates main.jsx as well as any module I'm currently saving?

@lpona
Copy link

lpona commented Dec 8, 2022

Good. This is someone who has solved the problem? What version do I need to downgrade from vite to have it refresh the browser window itself?
Or should I do something else?

@lehni
Copy link

lehni commented Dec 9, 2022

Where did you apply that change?

@dajpes at the root level of your code. Ideally wrapped in an if () statement since production code will set import.meta.hot to false:

if (import.meta.hot) {
  // Workaround for a strange Vite hot-reloading bug, see:
  // https://github.com/vitejs/vite/issues/7839#issuecomment-1340109679
  import.meta.hot.on('vite:beforeUpdate', onBeforeViteUpdate)
}

function onBeforeViteUpdate(event) {
  if (event.type === 'update') {
    // Patch `event.updates` to remove the version query parameter from path,
    // so that the update gets picked up.
    // Why the stored `deps` are missing this part of the URL, I cannot say…
    const updates = []
    for (const update of event.updates) {
      updates.push(update, {
        ...update,
        acceptedPath: update.acceptedPath.replace(/\?v=[0-9a-f]+&/i, '?')
      })
    }
    event.updates = updates
  }
}

@zachbellay
Copy link

zachbellay commented Dec 12, 2022

@lehni When you say root level, do you mean to add this script inside of the index.html file?

For example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + React + TS</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>

    <script type="module"> 
      if (import.meta.hot) {
        // Workaround for a strange Vite hot-reloading bug, see:
        // https://github.com/vitejs/vite/issues/7839#issuecomment-1340109679
        import.meta.hot.on('vite:beforeUpdate', onBeforeViteUpdate)
      }

      function onBeforeViteUpdate(event) {
        if (event.type === 'update') {
          // Patch `event.updates` to remove the version query parameter from path,
          // so that the update gets picked up.
          // Why the stored `deps` are missing this part of the URL, I cannot say…
          const updates = []
          for (const update of event.updates) {
            updates.push(update, {
              ...update,
              acceptedPath: update.acceptedPath.replace(/\?v=[0-9a-f]+&/i, '?')
            })
          }
          event.updates = updates
        }
      }
    </script>

  </body>
</html>

@zachbellay
Copy link

May not be related to this specific issue, but what fixed it for me was a stupid mistake. I am using django-vite and in my index.html file I added this script when in development:

<script type="module">
    import RefreshRuntime from 'http://localhost:3000/@react-refresh'

    RefreshRuntime.injectIntoGlobalHook(window)
    window.$RefreshReg$ = () => {
    }
    window.$RefreshSig$ = () => (type) => type
    window.__vite_plugin_react_preamble_installed__ = true
</script>

As it turned out, I was serving all of my static assets from /static/ so changing import RefreshRuntime from 'http://localhost:3000/@react-refresh' to import RefreshRuntime from 'http://localhost:3000/static/@react-refresh' fixed my issue.

@lehni
Copy link

lehni commented Dec 13, 2022

@zachbellay yes this should work. Did you test it? Does it not resolve the issue?

@mcometa
Copy link

mcometa commented Dec 14, 2022

May not be related to this specific issue, but what fixed it for me was a stupid mistake. I am using django-vite and in my index.html file I added this script when in development:

<script type="module">
    import RefreshRuntime from 'http://localhost:3000/@react-refresh'

    RefreshRuntime.injectIntoGlobalHook(window)
    window.$RefreshReg$ = () => {
    }
    window.$RefreshSig$ = () => (type) => type
    window.__vite_plugin_react_preamble_installed__ = true
</script>

As it turned out, I was serving all of my static assets from /static/ so changing import RefreshRuntime from 'http://localhost:3000/@react-refresh' to import RefreshRuntime from 'http://localhost:3000/static/@react-refresh' fixed my issue.

We have a similar setup and I can confirm that this is working for me using vite@3.0.7.

Thank you!

@lpona
Copy link

lpona commented Dec 15, 2022

a nice solution is add plugin vite-plugin-live-reload
add
import liveReload from 'vite-plugin-live-reload'in vite.config
also use in plugin liveReload('.path'),

it works :)

@Jebble
Copy link

Jebble commented Dec 23, 2022

Started to notice this issue after upgrading to Vite 4, changes to reflected even though the console shows the file being updated through HMR.

Upgrading Vue from 3.1.0 to 3.2.45 solved it for me.

@majo44
Copy link

majo44 commented Jan 4, 2023

I'm facing same issue. In my case module is loaded to browser, and accepted, but hot reload do not cause react rerendering. If I will force rerender of components tree eg by resize window, new component version will be rendered properly.
Vite 4.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite feat: hmr
Projects
None yet
Development

No branches or pull requests