BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

ยฉ 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Blog/cdn
Intermediate3 min read

CDN Explained: What It Is, How It Works, and How to Use It in Next.js

A CDN speeds up your app by serving assets from servers close to your users. Here's how it works and how Next.js takes advantage of it.

Mohsen Fallahnejad

Mohsen Fallahnejad

Jun 20, 20261 readsDiscuss

A CDN (Content Delivery Network) is a global network of servers that cache and serve your static assets โ€” JS, CSS, images, fonts โ€” from a location close to the user.

Without a CDN, every user hits your origin server (wherever it's hosted).
With a CDN, they hit the nearest edge node instead.


๐ŸŒ How It Works#

  1. 1

    User (Tokyo)

  2. 2

    CDN Edge (Tokyo)

then it depends
Cache HIT โœ“
  1. โ†ณInstant Response
Cache MISS
  1. โ†ณOrigin Server
  2. โ†’Cache
  3. โ†’Respond
  1. User requests /logo.png
  2. CDN checks its edge cache
  3. Cache hit โ†’ served immediately from edge
  4. Cache miss โ†’ CDN fetches from your origin, caches it, serves it

Next request from any user near that edge? Instant.


๐Ÿ“ฆ What Gets Cached?#

Asset typeCached by CDN?
JS / CSS bundlesโœ…
Images / fontsโœ…
HTML (static)โœ…
API responsesโš ๏ธ optional
Dynamic SSR pagesโš ๏ธ optional

โšก Why It Matters for Frontend#

  • Lower latency โ€” assets served from 50ms away, not 300ms
  • Less load on your server โ€” CDN absorbs most traffic
  • Better Core Web Vitals โ€” faster LCP, less layout shift
  • Global scale โ€” same performance in Tokyo as in London

๐Ÿ”ง CDN in Next.js#

Next.js is CDN-ready out of the box. Here's what gets cached automatically:

Static assets in /public#

Served with long-lived cache headers automatically.

next/image#

Images are optimized and served via CDN edge with proper Cache-Control.

Static pages (SSG)#

Pages generated at build time are cached at the CDN edge.


โš™๏ธ Configuration#

1. Custom CDN with assetPrefix#

If you're using your own CDN (Cloudflare, CloudFront, etc.):

js
// next.config.js
module.exports = {
  assetPrefix: 'https://cdn.yourdomain.com',
}

Next.js will now load all /_next/static/ assets from your CDN URL.


2. Cache-Control headers#

js
// next.config.js
module.exports = {
  async headers() {
    return [
      {
        source: '/static/(.*)',
        headers: [
          {
            key: 'Cache-Control',
            value: 'public, max-age=31536000, immutable',
          },
        ],
      },
    ]
  },
}
  • max-age=31536000 โ†’ cache for 1 year
  • immutable โ†’ browser won't revalidate even on hard refresh

3. Per-fetch CDN caching (App Router)#

js
// cached for 1 hour at the CDN edge
const data = await fetch('https://api.example.com/posts', {
  next: { revalidate: 3600 },
})

4. Page-level revalidation (ISR)#

js
// app/blog/page.tsx
export const revalidate = 60 // revalidate every 60 seconds

CDN serves the cached page, Next.js regenerates it in the background when stale.


5. Force static caching#

js
export const dynamic = 'force-static'

Tells Next.js (and the CDN) to never recompute โ€” serve the static version always.


๐Ÿง  Cache-Control Cheat Sheet#

Header valueMeaning
public, max-age=31536000, immutableCache forever (versioned assets)
public, s-maxage=3600CDN cache for 1 hour
no-storeNever cache
stale-while-revalidate=60Serve stale, refresh in bg

โœจ Final Takeaway#

A CDN puts your assets next to your users. Next.js handles most of it automatically โ€” configure assetPrefix for custom CDNs, and use revalidate to control how long pages stay cached at the edge.

Filed under#cdn#nextjs#performance#frontend#caching

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X โ†—GitHub โ†—LinkedIn โ†—
โ† OlderqueueMicrotask Explained: The Smallest Async Queue in JavaScriptNewer โ†’React memo vs useMemo vs useCallback โ€” When to Use Which

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

One Line in My Root Layout Turned 158 Static Pages Into 0

Reading a cookie with cookies() in the Next.js root layout opts every route out of static rendering. What it cost this blog, measured, and how to fix a theme flash without it.

Nov 1, 20267 min read2

Next.js App Router Caching โ€” Cheat Sheet

A compressed reference of Next.js caching layers and controls (App Router).

Sep 20, 20252 min read

I Optimized a Button From O(n) to O(1). The Speed Didn't Matter.

A bug report, a route map, and three rounds of making it faster โ€” a story about what Big O really measures, why constants win at small n, and why the best algorithm is sometimes deleting the question.

Oct 31, 202610 min read5

Get new posts in your inbox

No spam. Unsubscribe any time.

3 min left
Discuss๐Ÿ‘ React
๐Ÿ‘