I recently optimized the performance of neon.tech (by @neondatabase) homepage and cut the TTFB from ~400ms to ~90ms:
📊 (TTFB numbers via @SpeedVitals)
Now, let's dive into magical changes below one by one:
- 1. Moved Real-Time Logic to Middleware
- 2. Removed Page Revalidation
- 3. Added Shared Cache Headers on Vercel
1. Moved Real-Time Logic to Middleware
Previously, every request was SSR'd just to check if a user should be redirected to console.neon.tech
.
Now, this logic runs in a lightweight middleware using Next.js middleware, which performs runtime checks much more efficiently.
2. Removed Page Revalidation
Disabled revalidation logic on the homepage and its alias /home
:
- export const revalidate = 60;
+ export const revalidate = false;
3. Added Shared Cache Headers on Vercel
Configured the cache headers in vercel.json
to enable shared cache responses:
{
"source": "/",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=0, s-maxage=31536000"
}
]
},
{
"source": "/home",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=0, s-maxage=31536000"
}
]
}
🌍 Global Performance Results
And that did the trick! Here's how the latency now looks globally (via @OpenStatusHQ):
Summary
With just a few key changes:
- Middleware instead of SSR
- No revalidation
- Smart caching headers
…we got the homepage blazing fast!