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

Allow preventing default actions of client.js #5763

Open
4 tasks done
Artur- opened this issue Nov 19, 2021 · 9 comments
Open
4 tasks done

Allow preventing default actions of client.js #5763

Artur- opened this issue Nov 19, 2021 · 9 comments

Comments

@Artur-
Copy link
Contributor

Artur- commented Nov 19, 2021

Clear and concise description of the problem

Some users have brought up that they want live reload temporarily disabled.

The use case is that they are modifying styles in the browser inspector instead of modifying the project files. Once they are done with styles modifications, they go through their changes and copy them to the project files. If you have live reload enabled when you do this then a full page reload can take place after the first changes are copied to a project file and that file is saved. When this happens, all remaining changes are lost.

Suggested solution

Allow listeners like vite:beforeFullReload to call payload.preventDefault() which will stop client.js from taking any actions.

Alternative

No response

Additional context

No response

Validations

@ygj6
Copy link
Member

ygj6 commented Nov 20, 2021

You can disable HMR by set server.hmr to false.

@Artur-
Copy link
Contributor Author

Artur- commented Nov 20, 2021

Ah, I forgot to mention that it would be preferrable to do it runtime through a UI control in our case

@ygj6
Copy link
Member

ygj6 commented Nov 20, 2021

The scene is a little too special. Maybe you can put a breakpoint on @vite/client to stop the page reload.

@Artur-
Copy link
Contributor Author

Artur- commented Nov 20, 2021

I can agree that it is quite a special case and might be solvable with the conf option.

There is another case also where this has come up which is:

  1. You are on page /
  2. You click to navigate to /hello
  3. The router starts loading a JS bundle needed by /hello and when finished, updates the URL and the view content
  4. Before the JS bundle is loaded, Vite does a page reload

Result: you are still on / after navigating to /hello which is quite unexpected

@jacksleight
Copy link

jacksleight commented Feb 13, 2024

Does anyone know if it's possible to get the method suggested by @cdauth in #6695 (comment) to work in Vite 5?

I've upgraded to 5.1.1 and despite throwing in vite:beforeFullReload the page still refreshes.

If I downgrade to 4.5.2 it works.

@obeobe
Copy link

obeobe commented Feb 17, 2024

I also encountered the same issue as @jacksleight .

I'm just trying Vite out and considering to switch from Webpack.
Browser page refresh is a blocker for me.

Refreshing the browser resets all state and is an incredibly intrusive action.

I get the concern about leaving the app in a supposedly inconsistent state with the code - but why take the paternalistic approach? I am not a newbie. I know what I'm doing and I want to be in control (without losing HMR altogether).

@obeobe
Copy link

obeobe commented Feb 17, 2024

@jacksleight -

I found two workarounds:

  1. Relatively clean but annoying: prompt before browser refresh. It's annoying because it will show an alert, but at least it will give you a chance to click on "Cancel" and avoid the refresh. Just add this code in your application initialization:
window.addEventListener('beforeunload', function(e) {
	e.preventDefault();
	e.returnValue = '';
});
  1. Dirty but not annoying: modify Vite code in node_modules/vite/dist/client/client.mjs and add a break after `case 'full-reload':
    image

Obviously this will need to be re-applied when updating Vite...

Note that you need to restart the server for this change to take effect.

@cdauth
Copy link

cdauth commented Mar 2, 2024

Here is a very hacky workaround that will definitely not work forever:

if (import.meta.hot) {
	// Prevent full reload, see https://github.com/vitejs/vite/issues/5763#issuecomment-1974235806
	import.meta.hot.on('vite:beforeFullReload', (payload) => {
		payload.path = "(WORKAROUND).html";
	});
}

@rmacklin
Copy link

rmacklin commented May 4, 2024

@patak-dev Is there any chance you'd reconsider #6695 independently from this issue?

That issue was closed in favor of this one, but I feel that one has a smaller scope than this issue and like @cdauth I would prefer to simply run my vite server with full reloads completely disabled. (Though unlike @cdauth, I don't care if this also disables hot reloads - I already run with server: { hmr: false }, but I still get the frustratingly disruptive full page reloads when switching between branches.)

Let me elaborate on why these full page reloads can be so painful: Imagine you are working on the current code and you notice some unexpected behavior. You want to keep your app loaded in this state so you can interact with it, but you also want to go check out an older revision and check the behavior there side-by-side. So you git checkout the old revision and--OOPS! Vite just reloaded the page you wanted it to leave alone; now you can't do your side-by-side comparison.

Hopefully that helps illustrate the pain. There are ways to work around this, but it would be ideal if we could just "set and forget" a vite option if we want to be in control of when our pages reload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants