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

Why doesn't the HTML Compressor combine CSS/JS into one file? #79

Open
3 of 6 tasks
raamdev opened this issue Jun 9, 2015 · 0 comments
Open
3 of 6 tasks

Why doesn't the HTML Compressor combine CSS/JS into one file? #79

raamdev opened this issue Jun 9, 2015 · 0 comments
Assignees

Comments

@raamdev
Copy link
Contributor

raamdev commented Jun 9, 2015


KB Article Creation Checklist
  • Write initial draft for this KB Article; label this issue draft and either questions or tutorials
  • Add required YAML configuration
  • Add Tags for this KB Article to the YAML config (see YAML Keys (Explained))
  • Edit and finalize draft for publishing (remove draft label, add draft-finalized label)
  • Assign Issue to yourself and create Markdown file (remove draft-finalized label, add pending)
  • Project Lead: Review and Publish KB Article (remove pending label, add published label)
Additional TODOs

Additional TODO list items go here.

title: Why doesn't the HTML Compressor combine CSS/JS into one file?
categories: questions
tags: html-compression
author: raamdev
github-issue: 

I see that the HTML Compressor is combining CSS and JS into multiple files instead of a single CSS file and a single JS file. Is there a particular technical reason for this?

When it comes to CSS, I originally made every effort to get all CSS into a single file; i.e., that's how the HTML Compressor was originally released. The problem was that some <link rel="stylesheet" media="all" /> tags contain different media specifications. wpsharks/html-compressor#16

The HTML Compressor needs to preserve the original @media queries. Since IE 9/10 won't allow us to nest media queries, this means that we need to create separate files.

As the HTML Compressor scans your HTML, if it finds things like this:

<link rel="stylesheet" media="all" />
<link rel="stylesheet" media="screen" />
<link rel="stylesheet" media="all" />

That ends up becoming three CSS files, only so that the HTMLC can break the media queries down properly. One way to optimize your site, is to change that to this.

<link rel="stylesheet" media="all" />
<link rel="stylesheet" media="all" />
<link rel="stylesheet" media="all" />

Now it will all become one CSS file. i.e., so long as each media query is the same, we can combine the files.

It is non-obvious at first, because you would think that this could become two CSS files:

<link rel="stylesheet" media="all" />
<link rel="stylesheet" media="screen" />
<link rel="stylesheet" media="all" />

However, in order to preserve the cascading effect in the stylesheets, it needs to become three. Otherwise, we would be pulling the last line up above the second line when they are combined. For that reason, any change in the media query leads to more files.


JS files are a different beast. These we can combine without needing to consider anything like a media query. However, we do need to consider their location in the document. All JS files in the head should become one, assuming there were no exclusions between them. The same for any in the footer.

So what can cause more than one JS file in the head or footer?

<head>
<script src="/a.js"></script>
<script src="/b.js"></script>
<script src="/c.js"></script>
</head>

This will almost always become one JS file. However, if /b.js is excluded, it becomes three files. The HTMLC leaves the original intact, which means that it can only compress /a.js and /c.js (together with anything after /c.js).

Again, it is non-obvious with JS too. You would think that if /b.js was excluded, that this could become just two JS files.

<head>
<script src="/a.js"></script>
<script src="/b.js"></script>
<script src="/c.js"></script>
</head>

It becomes three, because /b.js needs to remain in the same exact loading order. Which means that we compress /a.js (with anything above it), we leave /b.js as-is (it is excluded), and then we continue by compressing /c.js (along with anything below it).


a secret tip that can be used to optimize the impact that our HTML Compressor will have on your site. The tip being, to look at your theme and other plugins, and try to bring them all into the same media query. Often times, one theme will choose to use all, while another chooses to use screen or a specific query for mobile devices. It's better to write your CSS <link> tags with the same media query, which results in just one CSS file after the HTML Compressor runs. If you have CSS that only applies to mobile devices, or only applies to printers, you can use @media{} queries in the CSS for this. That's fine, so long as you don't nest @media{} queries, which is what the HTML Compressor would need to do (it can't, due to compat. issues).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant