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

HMR not working #11

Closed
n8sabes opened this issue Feb 20, 2023 · 5 comments · Fixed by #26
Closed

HMR not working #11

n8sabes opened this issue Feb 20, 2023 · 5 comments · Fixed by #26

Comments

@n8sabes
Copy link
Contributor

n8sabes commented Feb 20, 2023

Live edits to styled components are not being rendered by HMR. You must to stop and restart dev mode between edits which is quite inefficient.

It has not worked for some time. I am using the main branch.

    "@builder.io/qwik": "github:builderio/qwik-build",
    "@builder.io/qwik-city": "github:builderio/qwik-city-build",
@wmertens
Copy link
Owner

wmertens commented Jun 8, 2023

I think there's nothing else I can do beyond what I did in 0.5.5-0, this seems to be a qwik issue, see QwikDev/qwik#1901

@franck-co
Copy link
Contributor

Faced the same issue with version 0.5.12
But I don't have to restart the qwik dev server when I stick to version 0.5.4

@wmertens
Copy link
Owner

wmertens commented Dec 9, 2023

Weird, thanks for finding a working version.

Unfortunately I don't use this module myself any more so if anybody could help find the problem that'd be greatly appreciated.

@franck-co
Copy link
Contributor

@wmertens
In 5785cc8 the handleHotUpdate hook was removed.

// Re-parse .css.ts files when they change
async handleHotUpdate({file, modules}) {
	if (!cssFileFilter.test(file)) return
	try {
		const virtuals: any[] = []
		const invalidate = (type: string) => {
			const found = server.moduleGraph.getModulesByFile(`${file}${type}`)
			found?.forEach(m => {
				virtuals.push(m)
				return server.moduleGraph.invalidateModule(m)
			})
		}
		invalidate(virtualExtCss)
		invalidate(virtualExtJs)
		// load new CSS
		await server.ssrLoadModule(file)
		return [...modules, ...virtuals]
	} catch (e) {
		// eslint-disable-next-line no-console
		console.error(e)
		throw e
	}
},

By adding it back, I get the expected behaviour.
It's not technically HMR because it triggers a full page reload but at least, there is no need to restart the dev server each time.

My understanding is that the example.css.ts.vanilla.css needs to be invalidated too.
It was done by the handleHotUpdate hook, among other things.

Because there was certainly a good reason to remove the handleHotUpdate hook., maybe it's better to reintroduce only the invalidation of .css files.

This could be done in the transform hook, where the .css.ts files are already invalidated :

const {moduleGraph} = server
const modules = Array.from(
	moduleGraph.getModulesByFile(absoluteIdNoExt) || []
)
+const virtualCSSModules = Array.from(
+	moduleGraph.getModulesByFile(
+		`${absoluteIdNoExt}${virtualExtCss}`
+	) || []
+)
-for (const module of modules) {
+for (const module of [...modules, ...virtualCSSModules]) {
	if (module) {
		moduleGraph.invalidateModule(module)

		// Vite uses this timestamp to add `?t=` query string automatically for HMR.
		module.lastHMRTimestamp =
			(module as any).lastInvalidationTimestamp || Date.now()
	}
}

This way the page reloads without any handleHotUpdate

I've made a PR with thoses changes #26

@JaxCavalera
Copy link

JaxCavalera commented Jun 3, 2024

Did the fix from that PR get released yet? it seems this is still an issue and happening on the latest available version 0.5.12

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

Successfully merging a pull request may close this issue.

4 participants