Article

Wire Up a CDN With Vite for Faster Site Loads

Picture this: you ship a fresh Vite build, hit reload, and watch a waterfall of tiny JS chunks crawl across the network tab. Multiply that by every visitor, and you've got a real bottleneck. The fix is hooking your Vite output up to a CDN—and with AWS CloudFront as the example, the whole setup takes under an hour.

Here's the core trade-off: CDNs serve your static files from edge locations close to each visitor, cutting latency on repeat requests. But they also upgrade delivery to HTTP/2 and offload traffic from your origin. The cost is configuration time and CloudFront's request-based pricing, which for most personal to mid-size projects stays modest.

How the pieces fit together

A CDN is essentially a fleet of servers scattered across the globe that holds copies of your static files. The first visitor in a region pulls the file from your origin; everyone after that gets it from the nearest edge. CloudFront also upgrades delivery to HTTP/2 out of the box—something a plain S3 bucket does not give you.

The setup itself is not deep magic. In the CloudFront console, you point a new distribution at the domain hosting your assets, pick the "CORS-With-Preflight" response headers policy, and click create. That is the whole distribution.

Wiring Vite to the CDN

Once the distribution exists, the trick is teaching Vite to actually use it. Vite already knows the full graph of imported JS, CSS, and font files for your project, so the only thing you have to do is swap the base URL during production builds. In your vite.config.ts file, check whether NODE_ENV is set to production, and if it is, point base at an environment variable like REACT_CDN, which holds your CloudFront URL. In development, base stays empty so hot module reloading behaves normally.

One gotcha worth flagging: if you use the VitePWA plugin, it does not automatically inherit the new base. Without an override, the web manifest will point at the wrong place and the service worker will complain. Set its base explicitly to your live site URL to keep everything aligned.

Who should care about this setup

Use case Impact Effort
Content-heavy SPA with many chunks High – each saved round trip compounds Low – one config change
Docs site with repeat readers globally Medium to high – regional edge caching pays off Low
Small personal portfolio Low to medium – gains are real but modest Low
S3-hosted image library High – CloudFront adds HTTP/2 and edge caching S3 lacks Medium – second distribution needed

When to skip it:** If your site is truly static, has under a few hundred visits per month, and serves a single region, the gains may not justify the added complexity. Core Web Vitals are already green? Move on.

When this is a stepping stone:** Serving only assets from the CDN is the first step. The next logical move is serving your entire site from the edge—HTML included—which is where CloudFront Functions, Lambda@Edge, or a static-site host like Netlify or Vercel starts to look attractive.

What to watch next

  • If you serve images from an S3 bucket, the same trick applies; CloudFront has S3-specific options that make the integration painless.
  • HTTP/2 from CloudFront also opens the door to keep-alive connections and header compression on your static files.
  • When you push a CSS change, Vite's built-in filename hashing turns home.css into home-9f3a.css, so the CDN treats it as a brand-new file and the old one quietly retires. No manual cache purging, no stale styles.

Bottom line: for a Vite project, adding a CDN is one of the lowest-effort, highest-impact performance upgrades you can ship. It takes roughly an hour including the console clicking, and it pays off every time someone loads your site.

Key takeaways

  • Set up a CloudFront distribution with the CORS-With-Preflight policy and point it at your asset domain.
  • In vite.config.ts, switch the base setting to your CloudFront URL only when NODE_ENV is production.
  • Override the VitePWA base so your service worker and PWA manifest stay valid.
  • Lean on Vite's filename hashing for cache busting instead of manually purging CloudFront.
  • Consider a second distribution for S3-hosted images to get edge caching and HTTP/2 on media too.

Frequently asked questions

Do I need a CloudFront distribution for a small Vite site?

Not strictly, but even small sites benefit from edge caching for repeat visitors and from the HTTP/2 upgrade that CloudFront adds on top of S3.

Does Vite support CDN URLs without any extra plugins?

Yes. Switching the base option in vite.config.ts to a CDN URL when NODE_ENV is production is enough for Vite to rewrite asset paths.

What happens to my cache when I deploy a new build?

Vite adds hash codes to built filenames, so a changed file becomes a new URL and the CDN treats it as fresh. You usually do not need to manually invalidate anything.

Will this work with other bundlers like webpack or Rollup?

The general approach is the same, since they all support a public path or asset base, though the exact config key differs.

Why use CORS-With-Preflight for the response headers policy?

It tells CloudFront to send the CORS headers browsers expect, including the ones needed for preflight requests, so your assets can be requested cross-origin without extra configuration.

Related