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

Proxy server hot-reloading does not play nice with htmx #607

Closed
llamux opened this issue Mar 10, 2024 · 4 comments · Fixed by #614
Closed

Proxy server hot-reloading does not play nice with htmx #607

llamux opened this issue Mar 10, 2024 · 4 comments · Fixed by #614

Comments

@llamux
Copy link

llamux commented Mar 10, 2024

I'm currently using the templ proxy server for development of an application that uses htmx. Everything works as expected except for one slightly annoying console error. Whenever htmx loads a new page from the templ proxy server it re-executes the reload event listener script. This causes a javascript error because of the redeclared identifier (let src).

This is the script I'm talking about:

let src = new EventSource("/_templ/reload/events");
src.onmessage = (event) => {
	if (event && event.data === "reload") {
		window.location.reload();
	}
};

I'm currently just hiding error messages from this script using a filter.

-url:http://127.0.0.1:3001/_templ/reload/script.js

Is this a known issue and is there a workaround? Or is there potentially something amiss with my development setup? Would it make sense to use var src in the script instead to avoid this error?

@llamux llamux closed this as completed Mar 10, 2024
@a-h
Copy link
Owner

a-h commented Mar 10, 2024

Any reason you closed this one?

If it is a problem, it would be pretty easy to rename the variable to templ_src or something, it would just be an update to https://github.com/a-h/templ/blob/main/cmd/templ/generatecmd/proxy/script.js

@llamux
Copy link
Author

llamux commented Mar 11, 2024

Sorry, I didn't want to waste anyone's time and it's not too much trouble to suppress the error messages.

The issue isn't the chosen variable name, but the script re-executing after htmx page changes, causing a re-declaration error. The issue is specific to servers using htmx or htmx-like libraries (like turbo/turbolinks/pjax). I'm not sure if setups like this are common enough among templ users to warrant fixing the error. Unless there is some dead-simple fix that is low-overhead and doesn't break any other use cases, it's probably not worth it.

@a-h
Copy link
Owner

a-h commented Mar 14, 2024

I think this could be a wider issue. I ran into myself while testing something. I've put together a little PR, what do you think?

@a-h a-h reopened this Mar 14, 2024
@a-h a-h closed this as completed in #614 Mar 14, 2024
@llamux
Copy link
Author

llamux commented Mar 15, 2024

Thank you for this! It works great for me and looks like a clean solution without adding too much complexity.

And thank you for the rest of your work here. Templ has been a complete pleasure to use. 👍

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.

2 participants