Setting Things Up
Caching and Compressing Assets for Optimal Web Performance
In this final lesson, we focus on optimizing server performance by caching and compressing assets. These techniques are essential for ensuring your web application delivers content efficiently, reduces load times, and enhances the user experience.
Key Techniques
-
Gzip Compression
- Use gzip compression to reduce the size of files sent to the client.
- In your server setup, integrate Express middleware with the
compressionlibrary to compress all outgoing assets.
Example:
const compression = require('compression'); app.use(compression()); -
Caching Assets
- Configure cache lifetimes for different asset types to optimize browser caching.
- Use the
Cache-Controlheader to define the max age for assets:- CSS: 1 month
- Images: 1 year
- JavaScript: 1 day (to ensure updates propagate quickly).
Example:
const oneDay = 86400000; // In milliseconds app.use('/static', express.static('public', { maxAge: oneDay })); -
Dynamic Cache Control
- Use logic to set cache lifetimes based on environment or asset types.
- Split directories in your public folder to manage caching rules for different types of assets.
-
Production Environment Setup
- Declare your production environment to optimize server behavior.
- Example:
process.env.NODE_ENV = 'production';
Best Practices
- Tailored Cache Rules: Assign specific caching rules for different types of assets to balance performance and updates.
- Compression for All Assets: Apply gzip or Brotli compression to minimize file sizes and speed up delivery.
- Regular Audits: Periodically audit your server setup to ensure caching and compression settings remain optimal.
- Integration with Modern Frameworks: Frameworks like Next.js or Remix include many of these optimizations out of the box, making them an excellent choice for new projects.
With this setup, your application is now ready to serve users efficiently, ensuring optimal performance across devices and browsers. Thank you for completing this course, and we hope to see you in future titles!
Video Transcript
00:00Welcome to Mastering React Data Visualization.
00:03My name is Ben Fala,
00:05and this is a packed publishing production.
00:08In this title, we're gonna be doing a lot, but
00:10before we explore the main topics, let's focus on
00:14who you are and who this title is for really.
00:17So you're a JavaScript developer, not necessarily up to date
00:21with their latest technologies
00:22because we're gonna update you,
00:24but assuming that you're already a JavaScript developer,
00:28secondary, you're comfortable with terminal,
00:31you have basic comfort with it, which means you know how
00:33to install applications on terminal
00:35and how to work with basics of it.
00:38We're not gonna explore the basics of terminal,
00:41and we assume that as you start this title, you know
00:44what it is, you know how to work with it,
00:46and you're a JavaScript developer.
00:50So what are we gonna explore in this title?
00:52And our learning goals are very diverse,
00:55starting from data itself.
00:56And we're gonna look at how to work with a lot
00:58of different types of data.
01:00We're gonna see how to work with JSON Web Services, CSV,
01:04and even MongoDB.
01:06Those are the types of data.
01:08So we're gonna see a no SQL type of database.
01:10We're gonna look at files. We're gonna look at converting
01:13data to fit our needs.
01:17And the secondary learning goal is seeing how do you take
01:19that data and make it visual.
01:21And to do that, we're gonna have an interactive map.
01:23We're gonna have a grid, we're gonna have a chart
01:26or a graph, and a few different types of charts
01:28and graphs in the process.
01:31Now, our secondary learning goal will be o also
01:34to explore react js, JSX and ESX to get you up to speed
01:39and learn how to work with the with them.
01:42With that said, we'll also explore other technologies such
01:45as no JS, express JS and MongoDB,
01:48but they're not gonna be the main focus
01:50of throughout the title.
01:53So let's jump right into our first chapter
01:55because our first chapter is dedicated to JSON,
01:58where we're gonna learn about the JSON foundations,
02:01and we're gonna learn how
02:02to import J SSON files into our project,
02:04and also how to build ES six react components
02:08that integrate JSON into them.
02:11So let's jump right into this chapter, starting
02:14with the first lecture that we'll use to set things up
02:18and show you our development environment.
02:20Through it, we're gonna explore babble
02:23and show you that, well it show you
02:25that you could use already today ESX and JSX
02:27because it's gonna automatically be converted with the help
02:31of Babel and Webpac and our webpac dev server.
02:34So we could have a running server live as we're working,
02:38that is automatically sending to our browser
02:41a downgraded version of JavaScript,
02:43even though we're working with the latest cutting edge
02:46JSX and ES six.
02:48Last but not least, oa, we're gonna use a webpac dev server
02:51to create hot refreshes, which will basically cut down the,
02:55a lot of our refreshes
02:56'cause we don't have to click on refresh anymore. It
02:58Will will automatically re-comp, compile,
03:00and refresh our code every time we make a change.
03:03So let's see it in action.
03:06Now, to be able to take full advantage of this course,
03:10you really need to have NPM
03:13and no JS installed on your system.
03:15If you're not sure, if you have no JS
03:18or NPM, go into your terminal and type node minus V.
03:23If you type node minus V
03:25and you see a version number,
03:26that means you have no JS installed
03:28and you're already in really good shape.
03:31We're gonna be running on version 6.3 for this title,
03:35but make sure you grab the latest version, whatever it is.
03:39Last but not least, make sure that you have NPM installed
03:42as well by typing NPM minus V.
03:45If any of the two doesn't install or doesn't come through,
03:49or if at any stage when we're doing something you're getting
03:52an error, uh, permissions error, make sure
03:55that you go into the document that is
03:57provided, the link that is
03:59provided with more information on how to install no JS
04:03and NPM onto your system.
04:05From here on, we're assuming that you have both
04:08of them set up and configured,
04:09and it's time for us to talk about the source files in the
04:13download folder that you're gonna have to.
04:15With this course, you're gonna find many folders
04:17with source files among them.
04:19You'll have the zero zero starter files
04:22where you'll find the starting files that are appropriate
04:25for most of the chapters.
04:27If a certain chapter needs a different starter file,
04:30you'll find it appended with the actual chapter number,
04:33with the word starter file.
04:35In the case of, for example, the first chapter,
04:38which we're just about to get into,
04:40we're gonna be starting from that starter file.
04:43So when you looking into this file, these are the outcomes
04:45of the work that we've done starting from the files
04:48that were within the starter file.
04:50With that said, let's take a look at the basic structure
04:53of the files that you're gonna expect inside
04:55of that starter file.
04:57And first of all, you're gonna find a public folder
04:59where in it you'll have a very empty HTML file
05:03with literally almost nothing inside of it, besides a diviv
05:07and an ID for our React application in our source,
05:12in our root folder, you're gonna find a package js o
05:16and a web pack, do config js.
05:19In the source file, you're gonna find our client js
05:22that currently has a very basic basic JavaScript just
05:27for testing purposes.
05:29Now I wanna talk a little bit about package JSO.
05:33And although this title is not gonna go deeply into this
05:35topic, and if you wanna learn more about how NPM works
05:39and how no JS works in general, I urge you
05:42to check my title, full stack React,
05:46just search within the platform you're on for
05:49for Full Stack React,
05:50where you'll get a very good bird's view over React
05:53and a lot of other technologies that are very critical
05:56for working in React.
05:58Now I want to talk about the things that are inside of here,
06:01even though we're not gonna manually install them.
06:04First of all, the, our application has a few dependencies,
06:07and among them, the most important one is react
06:10and react dumb, which are used
06:11for every single React application that we're building.
06:16Babbled Polyfill is gonna make sure that browsers
06:19that don't fully support ES five will have
06:22that extended fills those shims
06:25or polyfills that will make sure that older browsers
06:28that don't fully support ES five will have support as well
06:32in our dev dependencies.
06:34These are dependencies that we're gonna be used
06:36to build up our final files, which will then be hosted.
06:40So these are co-hosting files
06:41and co-hosting elements that will help us in our work.
06:45Among them, we'll have a few Babel packages
06:48that will enable us to,
06:49to start coding already now in E ES six
06:52and with JSX in React.
06:55While our actual outcome application is going
06:58to actually run an ES five,
07:00even though we're developing an E ES six, webpac
07:04and Web Pack dev server are gonna help us streamline
07:07and speed up the process of preparing an outputting our file
07:11to create a hot fix that will enable us
07:14to basically not need to recompile things,
07:17but every time we make an update, our browser will refresh
07:20with that content that we've just updated.
07:23Last but not least, our webpac dot config js file has all
07:27the description and the information
07:30and the settings, both for our development server
07:33and also for how is it going to use Babel, how,
07:36what is it gonna compile.
07:37So in this case, it's searching
07:39for our JavaScript file in our source file called Client js,
07:43and it's gonna output that file as index
07:46js in our root path.
07:49In other words, our output will be our local host server
07:52and Port 3000.
07:54And if we want to gain access to the JavaScript
07:56that is being compiled, we're gonna want to add
07:59that into our HTML file so it'll have access to it.
08:03Last but not least, the files as you're getting them,
08:06does not contain actually the no JS files that are needed
08:10for application to run
08:12and every single source file that you're gonna be working
08:15with before you could actually start working with it,
08:18make sure that you type inside of that folder NPM install.
08:22When you do that, it's gonna go through the process of going
08:25through the documentations that are in in this JSON package,
08:29and it's gonna go through and install the dependencies
08:32and the dev dependencies.
08:35In the next lecture, we're gonna be under the assumption
08:37that you've installed all those packages
08:39and you're ready to start developing
08:41and working in our environment.
08:44In this lecture, we saw our setup
08:46that we'll be using throughout our title.
08:49In the next lesson, we'll start moving into coding itself
08:52as we explore and take an overview of JSON Basics.
