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

Head tags duplicating on body's children elements after swap events #2018

Closed
fercomunello opened this issue Nov 21, 2023 · 2 comments
Closed

Comments

@fercomunello
Copy link
Contributor

fercomunello commented Nov 21, 2023

How to reproduce?
1) Include the template fragment parsing config via a meta tag or script:

<meta name="htmx-config" content='{"useTemplateFragments": true}' hx-preserve="true">
<script hx-preserve="true">
    htmx.config.useTemplateFragments = true;
</script>

2) Make sure the page title changes with the new content when clicking on the links and if the title appears once on <head> or if it's duplicated on the <body>.

3) Now comment the config or set it to false and see that the issue does not happen!

  • The same behaviour happens with other head tags such as scripts. And for changing something like the page's title I could do the following too:
    <script async>
        document.title = '${serverPage.title}';
    </script>

Set-up environment
In order to simulate the XHR requests I just used IntelliJ's open in browser feature (which uses websockets) but the same behaviour occurs on a normal web server.

Download code
https://gist.github.com/fercomunello/701c36db4acf47580e324e6e2202c655

Demo
image

@fercomunello
Copy link
Contributor Author

This solution seems ok... or it's just a temp fix? 🤔

var startTag = getStartTag(resp);
if (htmx.config.useTemplateFragments && partialResponse) {
    if (startTag === 'head') {
        resp = resp.replace(/<head>[\s\S]*?<\/head>/, '');
    }
    ...
} else {
    ...
}

fercomunello added a commit to fercomunello/htmx that referenced this issue Nov 21, 2023
fercomunello added a commit to fercomunello/htmx that referenced this issue Nov 21, 2023
fercomunello added a commit to fercomunello/htmx that referenced this issue Nov 22, 2023
@fercomunello
Copy link
Contributor Author

@1cg Hey, thanks for the merge!

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

No branches or pull requests

1 participant