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

properly remove script tags #9

Merged
merged 1 commit into from
Jun 6, 2024
Merged

properly remove script tags #9

merged 1 commit into from
Jun 6, 2024

Conversation

ryanbethel
Copy link
Contributor

No description provided.

Copy link
Contributor

@macdonst macdonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check that the component is SSR'd first. Can we add tests for dynamically added components?

@@ -35,7 +35,7 @@ const CustomElementMixin = (superclass) => class extends superclass {
// Removes script tags as they are already appended to the body by SSR
// TODO: If only added dynamically in the browser we need to insert the script tag after running the script transform on it. As well as handle deduplication.
el.querySelectorAll('script')
.forEach((tag) => { el.content.removeChild(tag) })
.forEach((tag) => { el.removeChild(tag) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to guard this code with a check to see if the component is SSR'd or not. If they component is created dynamically this could remove a script tag the component needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong, but I think the fact that it is running inside the custom element mixin render function itself means that the code to define the custom element has already run. I think that we might need to add a check so that if you want to add the script tag for some other reason it does not get removed. Like in the case of analytics etc. But then it needs to be deduplicated etc. so for now I think it is safest to remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add some tests to illustrate what I was talking about but there is still a failure as the enhanced attribute is not reset when modified.

https://github.com/enhance-dev/enhance-custom-element-mixin/blob/dynamic-add-element-tests/test/script-test.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think my suggestion needs to be implemented in this PR so I'm going to approve this PR>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most recent update of this mixin should not not tamper with the template anymore.
There is a todo in the code about injecting and deduplicating any script tags present in the template.

@kristoferjoseph kristoferjoseph merged commit 7ef0e20 into main Jun 6, 2024
7 checks passed
@kristoferjoseph kristoferjoseph deleted the script-tag-bug branch June 6, 2024 21:44
@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants