arnoson / vite-plugin-live-reload

A simple live reloading plugin for vite.
86 stars 3 forks source link

feat: Allow reloading the page regardless of the modified file path #5

Closed ElMassimo closed 3 years ago

ElMassimo commented 3 years ago

Description 📖

This pull request closes #4 by adding an always configuration option that forces reload to happen regardless of the modified file paths.

Usage 🚀

import LiveReload from 'vite-plugin-live-reload'

export default defineConfig({
  plugins: [
    LiveReload(['config/routes.rb', 'app/views/**/*'], { always: true, log: true }),
arnoson commented 3 years ago

Thanks for the PR! Isn't the plugin forcing a full page reload already, regardless of the path? However, I am not familiar with how the Vite client reacts to different paths

ElMassimo commented 3 years ago

Normally it would, but it doesn't seem to work for these use cases, probably because these paths are outside of the root.

I'll read the client to see if I can figure out what's going on. Hold on the merge for now 😃

ElMassimo commented 3 years ago

It seems that when the modified file is an .html file, Vite's client only reloads the page if the browser is currently in that HTML page.

Unfortunately, that logic does not work when the HTML file is outside the root, because:

const payloadPath = base + payload.path.slice(1)

will never match the URL.

This is why forcing * is necessary for some use cases.

Vite Client ```js if (payload.path && payload.path.endsWith('.html')) { // if html file is edited, only reload the page if the browser is // currently on that page. const pagePath = location.pathname const payloadPath = base + payload.path.slice(1) if ( pagePath === payloadPath || (pagePath.endsWith('/') && pagePath + 'index.html' === payloadPath) ) { location.reload() } ```
arnoson commented 3 years ago

Okay, thanks for researching it further! Would you mind changing the name of the option to something more descriptive? For example alwaysReload or forceReload

ElMassimo commented 3 years ago

Sounds good, I like alwaysReload.

At the moment I'm traveling, but I'll update the PR later today. Thanks!

Edit: Submitted the change.

arnoson commented 3 years ago

Thank you! Just published the new version

ElMassimo commented 3 years ago

Excellent, thanks!! 😃