You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This might be something that could be a lack of education on my part, so hoping either @zachleat or any others knowledgeable about how the WebC plugin works can help me find the right way to handle this.
On pages where there isn't any bundled content returned from getBundle(), the output files end up with empty <script></script> and <style></style> tags when using the example markup from the WebC Plugin Documentation like so:
I've tried using webc:if to conditionally render the style and script tags if getBundle() returns falsey, however, this doesn't appear to have any effect, even if I check to see if the bundle returns falsey:
As a last ditch effort, I've tried adding some logic to the bundler plugin to return early if there's empty content and combine that with webc:if, but that doesn't have any impact either:
What's the preferred way to drop a <script> or <style> tag from rendering if a bundle returns empty? Is this something that's better to handle by post-processing the HTML output?
The text was updated successfully, but these errors were encountered:
noelforte
changed the title
Handling empty <script> and <style> tags when bundles are empty
Handling empty <script>, <style> and <link> tags when bundles are empty
May 27, 2023
Stumbled across this issue (11ty/eleventy-plugin-bundle#8) while trying to dig deeper into this. Looks like handling empty bundles isn't quite supported yet. I'll leave this issue open in case anyone knows different but for now will probably add some global data to hide the script / style / link tags until there's official support.
Have a workaround for this now: You can use a transform to post-process Eleventy's output to match and strip empty tags generated by @11ty/eleventy-plugin-bundle. If there's an official solution to this issue I'd love to hear it, but until then the following transform works great:
eleventyConfig.addTransform('formatHTML',asyncfunction(content){if(this.page.outputPath&&this.page.outputPath.endsWith('.html')){constfiltered=content// Remove all invalid script link and style tags.replaceAll(/<link.+href=["']{2}.*\/?>/g,'').replaceAll(/<script((?!src=".+").)*><\/script>/g,'').replaceAll(/<style.*>\s*<\/style>/g,'')// Clean up multi line breaks.replaceAll(/\s{2,}/g,'\n');returnfiltered;}});
EDIT (02-01-2024): Note that this transform needs to be added AFTER the call to .addPlugin(webcPlugin) otherwise the transform happens too early and the content is still populated with the placeholder bundle comments.
This might be something that could be a lack of education on my part, so hoping either @zachleat or any others knowledgeable about how the WebC plugin works can help me find the right way to handle this.
On pages where there isn't any bundled content returned from
getBundle()
, the output files end up with empty<script></script>
and<style></style>
tags when using the example markup from the WebC Plugin Documentation like so:I've tried using
webc:if
to conditionally render thestyle
andscript
tags ifgetBundle()
returns falsey, however, this doesn't appear to have any effect, even if I check to see if the bundle returns falsey:As a last ditch effort, I've tried adding some logic to the bundler plugin to return early if there's empty content and combine that with
webc:if
, but that doesn't have any impact either:eleventy.config.js
index.webc
Also of note, when writing to an external file, if no bundle can be produced, WebC will produce invalid markup:
What's the preferred way to drop a
<script>
or<style>
tag from rendering if a bundle returns empty? Is this something that's better to handle by post-processing the HTML output?I've uploaded my reduced test case to https://github.com/noelforte/eleventy-webc-bundle-rtc in case anyone wants to tinker with how to handle this.
The text was updated successfully, but these errors were encountered: