Bug: Minified CSS files in require_css() get truncated at {#element-id-selector

When a minified CSS file is included via require_css(), CSS ID selector rules at the beginning of an opening bracket seem to get treated as a hubl comment, which truncates the rest of the file after HubSpot regenerates the CSS file. Is there a way to prevent HubSpot from parsing a minified css file for hubl? If not, it seems like this is a bug which could cause quite a few headaches while people chase down why their styles look broken.

Hey @aviolin,

Welcome back!! :tada:

I couldn’t find a documented setting that turns off HubL parsing for CSS files in the design manager. However it may be helpful to review these docs that may outline some workarounds:

I’m tagging in a few folks from the community to see if they’ve run into this before!

Hey @Alex_Boissonneault, @GRajput, @danielsinewe – do you have any suggestions for @aviolin on this?

Thank you!!

Sam, Community Manager

You’re running into HubL’s comment syntax. {# opens a HubL comment, and CSS files in the design manager get run through HubL before they’re served. Once a minifier strips the space in something like @media (max-width:768px){#header{display:none}}, HubL reads {# as the start of a comment and drops everything after it until it finds a closing #}. That’s why the file comes out cut off.

A few ways around it, in the order I’d try them:

  1. Wrap the whole file in a raw block. Put {% raw %} on the first line and {% endraw %} on the last. HubL leaves everything between them alone. You lose HubL variables inside that file, but a pre-built minified file usually doesn’t use any.
  2. Add one step to your build that puts a space back after the brace, for example sed -i 's/{#/{ #/g' dist/main.min.css. That costs one byte per match.
  3. Skip your own minify step for files that live in the design manager. HubSpot already minifies the CSS it serves from there, so doing it twice gains you very little.
  4. Upload the minified file to the File Manager and pass its full URL to require_css(). File Manager files are served as-is and aren’t parsed as HubL.

The raw block is the fastest fix if you need the styles back today.

Hey @stassey thanks for looping me in, i think i could help here as i’ve run into this one too.

@aviolin, HubSpot isn’t truncating the file at random. Any CSS file in the Design Manager goes through the HubL renderer, and in HubL {# opens a comment. Minified CSS produces that pair all the time, for example @media(max-width:768px){#header{...}}. HubL then treats everything after it as a comment until it finds a closing #}, and if there isn’t one, the rest of the file disappears.

As far as i know there’s no setting to turn HubL parsing off for a single file, but there are a few clean ways around it:

Wrap the file in raw tags. Put {% raw %} on the first line and {% endraw %} on the last. HubL then outputs everything in between as-is. It’s the quickest fix, and you can add it as a build step so it survives every re-minify.

Host it in the File Manager instead. Files there aren’t rendered as HubL, so you can upload the minified file there and point require_css() at its URL. The trade-off is that you lose Design Manager versioning for that file.

Don’t pre-minify at all. HubSpot already minifies CSS when it serves it through the CDN. You can upload the readable source and let HubSpot do the minifying, which avoids the problem completely.

If you’re moving toward CMS React / JS-rendered modules, CSS you import there is bundled at build time and never goes through HubL, so this issue doesn’t come up.

I agree this should be documented, because it’s a frustrating one to track down. Wrapping in {% raw %} should get your styles back right away.

Hey all, thanks for the helpful information. {% raw %} does seem like a pretty good solution here. We ended up adding a rule in our optimizer which always includes a space between { and #, %, and { in css files, which fixed it for us. Also, if HubSpot is already minifying it anyways, it may not even be necessary!