Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The CSS and JavaScript that we write is often filled with spaces, line breaks, comments, and long variable names. The web browser doesn't need any of these things to parse properly, and yet they still are being downloaded when a site visitor loads up the page. Minifying assets removes these extra bits and reduces overall page weight.
Concepts
- Minify Assets - Minification reduces the file size of JavaScripts and CSS by removing spaces, line breaks, comments, long variable names, and via other optimizations.
- Combine Assets - Combining CSS into one file reduces HTTP requests.
Resources
-
0:00
The CSS and JavaScript that we write is often filled with spaces, line breaks,
-
0:05
comments, and long variable names.
-
0:07
The web browser doesn't need any of these things to parse properly, and yet
-
0:12
they still are being downloaded when a site visitor loads up the page.
-
0:17
We could try removing these ourselves, but
-
0:19
that would be very tedious and time consuming.
-
0:21
Instead, we can use a process called minification.
-
0:25
When we minify a CSS or JavaScript file, we keep a copy of the original,
-
0:31
but the one that we actually reference in our HTML is the minified version.
-
0:37
There are many different sites, programs, and
-
0:39
web services that can help you minify your code.
-
0:42
However, we're going to use Google's closure compiler for our JavaScript and
-
0:47
CSS minifier for our CSS.
-
0:50
We could also minifcation from the command prompt then create an asset pipeline to do
-
0:55
this automatically every time we make changes, but
-
0:58
that's a little beyond the scope of the basics.
-
1:00
Instead, we're going to simply copy our code into the minifiers and
-
1:04
then copy the output into new minified versions on our site.
-
1:09
So let's start with the JavaScript.
-
1:12
We only have one JavaScript file that's not minified.
-
1:17
You can see that jquery.magnific-popup.min is already minified,
-
1:23
and then, we're not even using this jQuery file.
-
1:26
We're using the Google hosted library.
-
1:29
We just have app.js.
-
1:31
The file is already pretty small but
-
1:35
if we had a lot more JavaScript, this step becomes even more important.
-
1:40
We're going to go to the closure compiler service,
-
1:48
so closure-compiler.appspot.com.
-
1:54
And you can find a link to this in the notes associated with this video.
-
1:58
And here is where we will minify our JavaScript.
-
2:01
So I'm going to copy everything that's here.
-
2:05
And then I'm going to paste it by removing everything there.
-
2:11
And I'll paste it in, and I'll click compile.
-
2:15
[BLANK_AUDIO]
-
2:17
So, it went ahead and compiled the little bit of JavaScript that we have.
-
2:22
But, our JavaScript is so small that it's actually better to save an HTTP
-
2:26
request and simply paste it directly in line into the HTML.
-
2:31
The HTTP request would take longer than it would to just
-
2:35
download these characters and the HTML file.
-
2:38
So let's open up index.html.
-
2:42
And i'm going to scroll down to where were including app JS.
-
2:46
And i'm just going to remove the src attribute and it's value.
-
2:52
And between the two script tags,
-
2:54
I'm going to paste what we got from the closure compiler.
-
2:59
Next, let's do the same for our CSS.
-
3:02
For this, we're going to use another hosted service called CSS Minifier.
-
3:06
There's a link to CSS Minifier in the notes for this video.
-
3:10
We're also going to put all of our CSS into a single minified file.
-
3:16
This will combine all the HTTP requests of separate CSS files into one request.
-
3:24
So, I'm going to create a new file here.
-
3:27
[SOUND].
-
3:30
And I'll call it all.min.css.
-
3:37
And, this is going to include all of our CSS, and we are even going to include
-
3:44
some stuff from the jQuery plugin, even though we are not using it on other pages.
-
3:50
Just because if we can grab all this in one request, it will be cached and
-
3:54
we can use it on other pages.
-
3:57
So it's not, not big of a deal.
-
3:59
It's also a very small amount of CSS, so in this case, it's fine.
-
4:05
So I'm going to open CSS Minifier.
-
4:07
It's at cssminifier.com.
-
4:11
Again, this is in the notes.
-
4:14
And first, I'm going to go back to my workspace.
-
4:18
And we need to make sure that we get these in the proper order.
-
4:23
So, if we go to index and scroll up, we'll see that we have normalize,
-
4:28
and then we have a font from Google, and then we have main,
-
4:32
responsive, and then, magnific dash popup.
-
4:36
And we need all of these, but
-
4:39
we can't actually just put this font in there because it's hosted elsewhere.
-
4:44
So, I'm going to actually move normalize so it's just below the font.
-
4:50
And that really should not effect the look of the site or any of its functionality.
-
4:55
So it should be fine.
-
4:57
We'll put that all.min.css file right there.
-
5:03
So, normalize is first, so let's grab that.
-
5:07
I'm just going to copy the entire file, and
-
5:11
then I'll go to CSS Minifier and paste it in.
-
5:15
And, I can make a ton of space there just so I know what's going on,
-
5:19
because CSS Minifier is just going to remove it.
-
5:21
So that's fine.
-
5:23
So let's look at index again, after normalize is main.
-
5:27
So let's get that.
-
5:28
[NOISE] And next,
-
5:32
we need responsive.CSS.
-
5:38
So, let's grab that file.
-
5:40
[NOISE] And then finally,
-
5:44
we need our jquery.css.
-
5:48
So let's grab that and paste it there.
-
5:52
And then we will click minify, and
-
5:55
it's put it all into giant file with no spaces or comments.
-
6:00
So we'll just copy that.
-
6:04
And then let's go to all.min.css.
-
6:08
And paste it in.
-
6:09
And we'll save that file and
-
6:11
close it, because we don't want to edit that directly.
-
6:15
So let's close all these other files here.
-
6:17
And then in index, we just need one include here.
-
6:24
So, let's remove normalize and all those others.
-
6:27
And, inside of our CSS folder, we'll reference all.min.css,
-
6:33
and let's just go to our homepage here to make sure that's working properly.
-
6:38
Yup, everything looks good.
-
6:40
Our jQuery plugin still works.
-
6:42
So, let's do that on the other pages as well.
-
6:45
So we'll copy that and we'll go to about.
-
6:49
And we'll remove the CSS there.
-
6:52
And we'll remove from normalize, being careful to keep the Google font there.
-
6:59
And then same thing for contact, remove normalize and replace it.
-
7:06
There we go.
-
7:08
So let's just spot check our other pages here.
-
7:12
About still looks good, contact still looks good.
-
7:17
So, one thing to keep in mind with this method is that, any time you make a change
-
7:22
to your regular JavaScript in CSS files, you'll need to minify them again so
-
7:26
that the changes are reflected in the assets that you're actually using.
-
7:31
As I mentioned previously, there are techniques that can work to automate this,
-
7:36
but that gets into more advanced material than these lessons.
-
7:40
Now that we've minified our CSS and JavaScript, let's do one final performance
-
7:44
check and measure the difference between where we started and where we ended up.
-
7:51
So I'm going to open the chrome dev tools here and refresh on the network tab.
-
7:58
And if you remember, we started out with about 20 requests and
-
8:02
we had something like 800 kilobytes being transferred.
-
8:06
Now, by combining our CSS and SVGs and
-
8:09
other files that we don't need, we're down to 13 requests.
-
8:13
So that's pretty good.
-
8:15
We're transferring 152 kilobytes.
-
8:18
And most important of all, we are at a pretty fast response time now.
-
8:22
So let me just refresh that a few more times.
-
8:24
Looks like there is some variance in the network,
-
8:29
but generally it's around 200 milliseconds.
-
8:33
So that is a lot better than where we started.
You need to sign up for Treehouse in order to download course files.
Sign up