Caching and Compressing Assets for Optimal Web Performance
Getting Webpack JavaScript to Be Production Ready
JavaScript optimization is critical for ensuring your application performs well in production. In this session, we focus on preparing JavaScript for production using Webpack, covering key steps such as stripping out development-specific code, minifying files, and configuring Webpack for optimized builds.
Key Steps Covered:
-
Removing Development-Specific Code
- Use the
DefinePluginto strip out debugging and testing code. - Set
process.env.NODE_ENVto"production", which signals libraries like React to run in production mode.
- Use the
-
Optimizing the Build
- Use
TerserPluginor similar tools to minify JavaScript. - Configure source maps with
cheap-module-source-mapfor production to balance build speed and debugging capability.
- Use
-
Splitting Configurations
- Maintain separate Webpack configurations for development and production to simplify maintenance and ensure tailored optimizations.
- Include
devtoolsettings likeevalfor faster development builds.
-
Minifying Code
- Reduce file size using
TerserPluginorOptimizeCSSAssetsPluginfor bundled JavaScript. - Ensure you exclude unnecessary warnings by adjusting compression settings.
- Reduce file size using
Results Achieved:
- Reduced JavaScript file size from 979 KB in development to 223 KB in production.
- Improved application loading time and ensured better performance across browsers.
Best Practices for 2024:
- Frameworks like Next.js handle many of these optimizations automatically. If starting a new project, consider leveraging a modern framework for simplicity and built-in production features.
- Use ESBuild or Vite for faster builds and smaller output files when working outside traditional Webpack environments.
With these optimizations in place, your application is ready for a high-performing production deployment.
Video Transcript
00:01We reached our finest lecture, final lecture in this title
00:04where we're gonna learn how to cache
00:06and compress our files, starting
00:08by compressing our assets in G ip,
00:11defining a cache and oto.
00:13By the way, as a side note,
00:15although we're not doing this in this lecture,
00:16you'll find it in the lecture note.
00:18You'll see how could we cache Otto templates?
00:20You'll find that in the links provided to the section.
00:23But let's go ahead and define a cache
00:25and set up a GIP to make sure
00:26that our server is gonna render out our items in the most
00:30effective way when interacting with browsers.
00:34Alright, so there's one more thing that we need to do
00:36before we wrap things up,
00:37and that is to go to our server itself, in this case,
00:40our production server, and also optimize node
00:42itself for production.
00:44Now, technically it's very easy to do this
00:46and I'm gonna do it in our production
00:48script, not the other one.
00:49And even before I start up the server,
00:51I'm gonna call the process environment node environment.
00:55Just like we set up with our Webpack only this time
00:58around without Webpack,
00:59I'm just gonna say this is production.
01:01So I'm literally declaring that our,
01:03there's a few different ways of doing it, by the way,
01:05and there'll be a link for you
01:06to look at other ways of setting it up.
01:08But what I've done here is once we start up our server
01:10production only, then our server will be in
01:13the production mode.
01:15Now let's continue and I'm gonna go ahead
01:16and create a cons to get our, our server
01:19because I wanna also add a few configurations to our server.
01:23So I'm gonna go ahead and require,
01:25and then once we required our server, we will build it up.
01:29But even before building it out, how about we also make sure
01:32that any public items
01:33or any items that are being sent
01:35to the user will be compressed.
01:37Now it is really easy to do that,
01:39but really, generally speaking, or I'm, I'm gonna go ahead
01:42and just do it directly inside of our, I'm gonna go ahead
01:45and do it directly inside of our um, server itself, not
01:48because this would probably be the ideal situation.
01:50I'm gonna leave to you to figure out how to split it out.
01:52But just to wrap things up, what I wanna do,
01:55I'm gonna show you what step in the application do we wanna
01:57do each step, and then you can figure out
01:59and create hooks to be able to add and integrate them.
02:02So for one, even before we do anything, I want to go
02:05to my app and I want to set it to use our
02:10express oopsie Compress.
02:14Now what this is gonna do is gonna compress all files
02:17and all assets that are being sent to the clients.
02:19So we're not sending larger files than they need to do to,
02:22to be using J-I-G-Z-I.
02:25Ah, whatever. It's using a zipping kind of, I,
02:27it ran outta my head, I think it's G.
02:29Okay. The next thing I wanna do is I also want
02:32to set up Otto, some sort of a timeout
02:34or an a maximum age for our code here.
02:37And by the way, one thing that I could do is I could use,
02:40now that variable
02:41that I've added into our production area here,
02:44if we could find it, where was I production,
02:48I could take this variable and just use it Octo.
02:50So in this case, just just for this case of ex of sampling,
02:53something I could say here, Octo,
02:55if we're in the process environment,
02:59Equal, equal, equal production,
03:03then we're gonna do one thing.
03:05So for that, for that matter, if we're in production,
03:07we're gonna set it up this way.
03:10And if not, we'll go ahead
03:12and just leave it the way it was,
03:16which had no expires.
03:17Okay? But in this case, we want to set up
03:19and say that the maximum age of every file will be one day,
03:24which has been pre-calculated already
03:25to be
03:268 6, 4 0, 0 0, 0 in seconds.
03:31Or technically speaking, if we just take a calculator 60,
03:3860 times, 60 times 24
03:42and we will get our beautiful value of 86,000 seconds.
03:46But then I'm gonna duplicate that by a thousand milliseconds
03:49to reach that beautiful value.
03:52So that's milliseconds of the maximum age. So there you go.
03:56And what I've just done here, I basically checked
03:58to see if we're in the production.
04:01And if we're in production, then every file
04:03that is being served is only going to live for one day.
04:06Although you definitely would probably wanna
04:08put there a little bit longer lifetime for every single
04:12file file.
04:14Now, I just wanted to make a small note in a situation,
04:17for example, maybe you want your CSS to cash for a month,
04:20your images to cash for a year
04:22and your JavaScript to cache for just the day.
04:25Maybe 'cause you're very active on updating your JavaScript
04:27and you wanna make sure that it,
04:28the users always have the latest version.
04:31So the way you would do that is instead of serving up all
04:34of your assets directly in one directory,
04:38what you could do is basically split out your content
04:41and inside of your server itself,
04:43when you're setting up your public,
04:45you could set a different directory for each item.
04:47So public will go in the route directory,
04:49but for example, your images you would put here in your
04:52first parameter, your path to your image, second parameter,
04:55what folder it is, third parameter,
04:57the age that you want to give it.
04:59And then you could break it down
05:00and give it a variety of based on it, what it needs.
05:04And that's it. But you could look more about
05:05that in the documentation
05:07provided in the lecture files of this chapter.
05:10And with that is it, we're concluding this title.
05:15That's it. We're done with this title.
05:17Thank you so much for taking it.
05:19I hope you had a really wonderful time.
05:21And by the way, you could find a lot more of my things
05:23by searching my name up, Ben Fala, which is FH a LA,
05:28and you'll find many more
05:29of my titles in the exact same platform
05:31that you're in right now.
05:32If you have any question, concerns, ideas,
05:34or titles you think it would be great for me to create,
05:36you're more than invited
05:38to send me personal feedback at my email,
05:40which is on your screen right now, ben@zerotogeek.com.
05:44If you have positive feedback
05:45besides sending me an email, which is always nice,
05:47please do post it on social media
05:50and use the hashtag zero to geek and write a review, comment
05:54or share in the current platform you're in
05:56or in any other place.
05:58Thank you so much for joining me in another title
06:00and hope to see you in many other titles in the future.
06:03Until next time, I was Ben Fowler
06:06and this was a packed publishing production.
