Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Long-Term Caching, Compression & File Combining

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

It's time to celebrate! We're on production, and it was really just as simple as making sure Tailwind was being built and running the asset-map:compile command.

Now that we're here, there are a few things we need to check on, to make sure our site is fast.

1) HTTP/2: You Definitely Need It

The first is to check that your web server is using HTTP/2. We talked about that earlier, so hopefully you already got that rocking.

2) Combining Files

The second thing is... well.. not really a thing at all. I just want to point out that nothing in AssetMapper ever combined our files to reduce the number of HTTP requests. We're going to talk more about this in a few minutes, but thanks to HTTP/2, you almost definitely do not need to combine your files together. So if you were thinking that this was missing, it's not! It's by design. That's good! One less thing.

3) File Compression / Minification

But what about minifying our files? It's true: right now, our files are being served without minification, and that is a problem. We do want our CSS and JavaScript files to be minified... or at least compressed. And this is thing number three to think about.

But... this is something that can be done by our web server. Yup, if you ask kindly, you should be able to convince your web server to compress your files so they're smaller when being sent across the network. This is something that all web servers support, and it's done automatically by Platform.sh.

Here's how we can see it. Go to "Network" tools and select JavaScript. Select one of the files then go to "Headers". I'll make this a bit bigger. Okay, see this "Content-Encoding" response header? That's compression. This "br" stands for something called Brotli, which is more delicious than it sounds. Brotli is an advanced compression format. The other common value is gzip. So all of our static files are already being compressed! We get that for free with Platform.sh, so we can check that item off our list. Check your deploy system or web server docs for details on how to do this in your situation.

But wait, are minifying and compressing the same thing or different? Actually, they're a bit different. Both minifying and compressing greatly reduce the size of a file. We're using compression. Minification can result in slightly smaller files - in part because it will remove code comments - but it's not significant. Web servers themselves don't support minification, but if you use Cloudflare, there is a way to enable auto-minification. It probably won't make a huge difference, but you can try it.

4) Long-Term File Caching

The fourth and final thing that we need to do is make sure that all of our static files are set up for long-term expiration. Because we have these nice versioned file names, when a user visits our site, we want them to download this file one time and never, ever download it again. We want them to cache it forever. Because, if we change anything inside of this file, the whole filename will change! And the user's browser will naturally download the new version the next time they visit the site.

Over here, under "Headers", we do see an "Expires" response header. Out of the box, Platform.sh adds an "Expires" header for static assets, set to one hour. We can do much better than that.

Over in .platform.app.yaml, under locations... there we go... we see... expires: 1h. That's fine as a default for other static assets that we may have on our site. So I'm going to leave that alone. But add another rule to be more specific. For the regex, if the URL matches /assets/ anything, then set the expires header to 365d. Yes, 1 year - that's forever in Internet time!

... lines 1 - 30
web:
locations:
"/":
... lines 34 - 36
rules:
'^\/assets\/.*':
expires: 365d
... lines 40 - 64

Cool! Lets commit that... and deploy, deploy!

symfony deploy

When that finishes, refresh. We won't see anything obvious... but let's check out one of our JavaScript files, like app.js. We're looking for "Expires". If you don't see it - or it's still short - do a force refresh. This is weird case where the file didn't change, but we might still have the cached version from a minute ago with the old header. And if you do see this, congratulations! That's the goal.

So, our assets are being compressed, and they have long-term "Expires" headers. We've checked all of our boxes for a performant site!

Next: We're going to prove the site is fast by using Lighthouse to profile the site's performance. We'll learn about how files are downloaded, how pages are built, and we'll make our frontend even more efficient.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^4.0", // v4.2.0
        "doctrine/doctrine-bundle": "^2.7", // 2.10.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.4
        "doctrine/orm": "^2.12", // 2.15.2
        "knplabs/knp-time-bundle": "^1.18", // v1.20.0
        "pagerfanta/doctrine-orm-adapter": "^4.0", // v4.1.0
        "pagerfanta/twig": "^4.0", // v4.1.0
        "stof/doctrine-extensions-bundle": "^1.7", // v1.7.1
        "symfony/asset": "6.3.*", // v6.3.0
        "symfony/asset-mapper": "6.3.*", // v6.3.0
        "symfony/console": "6.3.*", // v6.3.0
        "symfony/dotenv": "6.3.*", // v6.3.0
        "symfony/flex": "^2", // v2.3.1
        "symfony/framework-bundle": "6.3.*", // v6.3.0
        "symfony/http-client": "6.3.*", // v6.3.0
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/proxy-manager-bridge": "6.3.*", // v6.3.0
        "symfony/runtime": "6.3.*", // v6.3.0
        "symfony/stimulus-bundle": "^2.9", // v2.9.1
        "symfony/twig-bundle": "6.3.*", // v6.3.0
        "symfony/ux-turbo": "^2.9", // v2.9.1
        "symfony/web-link": "6.3.*", // v6.3.0
        "symfony/yaml": "6.3.*", // v6.3.0
        "twig/extra-bundle": "^2.12|^3.0", // v3.6.1
        "twig/twig": "^2.12|^3.0" // v3.6.1
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.4
        "symfony/debug-bundle": "6.3.*", // v6.3.0
        "symfony/maker-bundle": "^1.41", // v1.49.0
        "symfony/stopwatch": "6.3.*", // v6.3.0
        "symfony/web-profiler-bundle": "6.3.*", // v6.3.0
        "zenstruck/foundry": "^1.21" // v1.33.0
    }
}
userVoice