<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eamon Cusic, Jr.</title>
    <description>The latest articles on DEV Community by Eamon Cusic, Jr. (@ecusicjr).</description>
    <link>https://dev.to/ecusicjr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4261737%2F2013f99d-2c5c-44b9-88a7-5a0570656328.png</url>
      <title>DEV Community: Eamon Cusic, Jr.</title>
      <link>https://dev.to/ecusicjr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ecusicjr"/>
    <language>en</language>
    <item>
      <title>Fixing Broken Styles After Deploy: Next.js, Cloud Run, and a Custom GCS Cache Handler</title>
      <dc:creator>Eamon Cusic, Jr.</dc:creator>
      <pubDate>Thu, 26 Nov 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/cusicdigital/fixing-broken-styles-after-deploy-nextjs-cloud-run-and-a-custom-gcs-cache-handler-4am6</link>
      <guid>https://dev.to/cusicdigital/fixing-broken-styles-after-deploy-nextjs-cloud-run-and-a-custom-gcs-cache-handler-4am6</guid>
      <description>&lt;p&gt;You ship an update. The deploy succeeds. Then someone emails: &lt;em&gt;"The site looks broken — no styling, weird layout."&lt;/em&gt; You hard-refresh and everything looks fine on your machine. So what happened?&lt;/p&gt;

&lt;p&gt;This post explains a real production issue we fixed while building &lt;a href="https://greatlakesadditive.com" rel="noopener noreferrer"&gt;Great Lakes Additive&lt;/a&gt; — a Next.js storefront on Google Cloud Run. If you build or operate modern web apps, the pattern applies beyond this stack: &lt;strong&gt;cached pages can outlive the assets they reference&lt;/strong&gt;, and serverless hosting makes that mismatch show up fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; engineers self-hosting Next.js on GCP, and founders or product owners who want to understand &lt;em&gt;why&lt;/em&gt; a deploy can look fine in CI but broken for real users — without needing to read the source code.&lt;/p&gt;




&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If you are…&lt;/th&gt;
&lt;th&gt;Here's the headline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Business / product&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A deploy replaced the server, but some users still received an old copy of the page that pointed at CSS files that no longer exist. Hard refresh fixed it; our fix made that mismatch impossible at the infrastructure level.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Engineering&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Next.js ISR/full-route cache persisted HTML across Cloud Run deploys. Hashed &lt;code&gt;_next/static&lt;/code&gt; assets changed every build, but GCS-backed cache entries were not scoped by &lt;code&gt;BUILD_ID&lt;/code&gt;. We added a custom GCS &lt;code&gt;cacheHandler&lt;/code&gt;, build-ID prefixes, client-side deploy detection, and a post-deploy smoke check.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  The symptom
&lt;/h2&gt;

&lt;p&gt;After a new release, some users saw:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unstyled HTML (plain browser defaults)&lt;/li&gt;
&lt;li&gt;Missing fonts and layout&lt;/li&gt;
&lt;li&gt;A page that looked fine after &lt;strong&gt;Ctrl+Shift+R&lt;/strong&gt; (hard refresh)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The app wasn't down. API calls worked. Navigation worked. But &lt;strong&gt;cached HTML was pointing at CSS and JS files from a previous build&lt;/strong&gt; — files that no longer existed on the new container.&lt;/p&gt;

&lt;p&gt;If you've ever seen &lt;code&gt;_next/static/chunks/…css&lt;/code&gt; return 404 while the document returns 200, you've met this bug.&lt;/p&gt;

&lt;p&gt;For a business, this shows up as "the site is broken" support tickets right after a release — even when monitoring says everything is green.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Cloud Run makes this worse
&lt;/h2&gt;

&lt;p&gt;Cloud Run is &lt;strong&gt;stateless by design&lt;/strong&gt; — each container instance has no guarantee of a persistent local disk:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Containers are ephemeral&lt;/strong&gt; — each deploy replaces the filesystem. Old &lt;code&gt;_next/static&lt;/code&gt; hashed assets vanish with the old revision.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple instances&lt;/strong&gt; — without shared cache storage, each instance builds its own in-memory cache. That's inconsistent and wasteful.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cold starts&lt;/strong&gt; — a new instance has no warm cache; without external persistence you rely entirely on whatever was baked into the image.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next.js aggressively caches &lt;strong&gt;rendered pages&lt;/strong&gt; (static generation, incremental static regeneration or "ISR", fetch cache, image optimization) for performance. That's correct behavior — until the cached HTML outlives the static assets it references.&lt;/p&gt;

&lt;p&gt;On a long-lived Node server with a single deployment, this mismatch is rare. On Cloud Run, &lt;strong&gt;every deploy is a new filesystem&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Next.js is caching
&lt;/h2&gt;

&lt;p&gt;For a typical App Router site, Next.js can persist several kinds of cache (via a &lt;a href="https://nextjs.org/docs/app/guides/self-hosting#configuring-caching" rel="noopener noreferrer"&gt;custom cache handler&lt;/a&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Cache type&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Full route cache&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Pre-rendered HTML for pages&lt;/td&gt;
&lt;td&gt;Marketing pages, product listings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Fetch cache&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Responses from server-side &lt;code&gt;fetch()&lt;/code&gt; calls&lt;/td&gt;
&lt;td&gt;CMS content, API data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Image optimization&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Resized/compressed images&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;next/image&lt;/code&gt; → &lt;code&gt;/_next/image&lt;/code&gt; outputs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Route handlers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cached API-style responses&lt;/td&gt;
&lt;td&gt;RSS feeds, webhooks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By default, Next.js stores this on the local filesystem (&lt;code&gt;.next/cache&lt;/code&gt;). On Cloud Run, that cache dies with the container — unless you plug in a &lt;strong&gt;custom &lt;code&gt;cacheHandler&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The solution: GCS-backed cache handler
&lt;/h2&gt;

&lt;p&gt;We implemented &lt;code&gt;cache-handler.mjs&lt;/code&gt; — a custom handler that reads and writes cache entries to &lt;strong&gt;Google Cloud Storage&lt;/strong&gt;, using a bucket already in the project's GCP stack.&lt;/p&gt;

&lt;p&gt;High-level flow:&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
  A[Browser request] --&amp;gt; B[Cloud Run / Next.js]
  B --&amp;gt; C{Cache hit?}
  C --&amp;gt;|Yes| D[Return cached page from GCS]
  C --&amp;gt;|No| E[Render page]
  E --&amp;gt; F[Store in GCS]
  F --&amp;gt; G[Return to browser]&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Stored path shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gs://bucket/next-cache/{BUILD_ID}/entries/{hash}.value.bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why GCS?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shared across all Cloud Run instances&lt;/strong&gt; — consistent cache hits regardless of which container serves the request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Survives container restarts&lt;/strong&gt; — &lt;em&gt;when scoped correctly&lt;/em&gt; (more on that below).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Already in the stack&lt;/strong&gt; — no new infrastructure if you're on GCP.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why binary serialization?
&lt;/h3&gt;

&lt;p&gt;Early versions stored cache values as JSON. That breaks Next.js internal structures — &lt;code&gt;Map&lt;/code&gt; objects (e.g. &lt;code&gt;segmentData&lt;/code&gt;) deserialize as plain objects and silently corrupt cached pages.&lt;/p&gt;

&lt;p&gt;The handler uses Node's &lt;strong&gt;&lt;code&gt;v8.serialize&lt;/code&gt; / &lt;code&gt;v8.deserialize&lt;/code&gt;&lt;/strong&gt; for route cache entries, with validation to reject legacy JSON-shaped entries. Image cache entries store raw buffers separately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;next.config.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isProd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cacheHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./cache-handler.mjs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cacheMaxMemorySize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// force GCS, not in-memory fallback&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runtime env vars on Cloud Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GCS_BUCKET=your-bucket
NEXT_CACHE_PREFIX=next-cache
GITHUB_SHA=&amp;lt;git commit sha&amp;gt;   # used at build time; see BUILD_ID section
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Docker image copies &lt;code&gt;cache-handler.mjs&lt;/code&gt; into the standalone output alongside &lt;code&gt;server.js&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The bug we missed the first time: BUILD_ID scoping
&lt;/h2&gt;

&lt;p&gt;The first version of the handler wrote everything under:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gs://bucket/next-cache/entries/…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;strong&gt;worked&lt;/strong&gt; for sharing cache across instances — but &lt;strong&gt;failed across deploys&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each Next.js build produces new hashed filenames:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/_next/static/chunks/abc123.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After deploy, the new build only has &lt;code&gt;xyz789.css&lt;/code&gt;. If GCS still serves &lt;strong&gt;old cached HTML&lt;/strong&gt; referencing &lt;code&gt;abc123.css&lt;/code&gt;, you get broken styling.&lt;/p&gt;

&lt;h3&gt;
  
  
  The fix: prefix every cache key by build ID
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;BUILD_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;readBuildId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// from .next/BUILD_ID&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;CACHE_PREFIX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`next-cache/${BUILD_ID}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each deploy reads and writes only under its own prefix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;next-cache/a1b2c3d4/entries/…   ← deploy N
next-cache/e5f6g7h8/entries/…   ← deploy N+1 (clean slate)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New deploys never read stale HTML from a previous release.&lt;/p&gt;

&lt;h3&gt;
  
  
  Critical detail: read BUILD_ID from the filesystem
&lt;/h3&gt;

&lt;p&gt;We initially keyed the prefix off &lt;code&gt;process.env.GITHUB_SHA&lt;/code&gt; at &lt;strong&gt;runtime&lt;/strong&gt;. That &lt;em&gt;usually&lt;/em&gt; matched the build — unless someone deployed without the env var, or it fell back to &lt;code&gt;development&lt;/code&gt;. Meanwhile &lt;code&gt;generateBuildId()&lt;/code&gt; at &lt;strong&gt;build time&lt;/strong&gt; baked a different ID into asset URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mismatch = stale cache prefix = broken styles.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reliable approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readBuildId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cwd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.next'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'BUILD_ID'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'utf8'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GITHUB_SHA&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'development'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cache prefix must match the build embedded in the running container — not a loosely synced environment variable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;generateBuildId&lt;/code&gt; uses the same source:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;generateBuildId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GITHUB_SHA&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;BUILD_ID&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'development'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Browser tabs and long-lived sessions
&lt;/h2&gt;

&lt;p&gt;GCS scoping fixes &lt;strong&gt;server-side&lt;/strong&gt; stale HTML. It doesn't help a user who:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Opened the site &lt;strong&gt;before&lt;/strong&gt; a deploy&lt;/li&gt;
&lt;li&gt;Left the tab open for hours&lt;/li&gt;
&lt;li&gt;Navigated client-side with old JavaScript still in memory&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We added a small client component (&lt;code&gt;BuildIdGuard&lt;/code&gt;) that, when the tab regains focus, sends a &lt;code&gt;HEAD&lt;/code&gt; request and compares the &lt;code&gt;X-Next-Build-Id&lt;/code&gt; response header to the build ID baked into the current bundle. Mismatch → automatic reload.&lt;/p&gt;

&lt;p&gt;Production HTML already sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="na"&gt;Cache-Control: private, no-cache, no-store, max-age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;must&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;revalidate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Static assets correctly use &lt;code&gt;immutable&lt;/code&gt; long cache — they're content-hashed and safe to cache forever &lt;strong&gt;for that build&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bonus: middleware, Edge, and &lt;code&gt;node:crypto&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;While hardening caching, we hit a separate production crash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failed to load external module node:crypto
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next.js 16 still runs &lt;code&gt;middleware.ts&lt;/code&gt; on the &lt;strong&gt;Edge&lt;/strong&gt; runtime. Our bundler pulled &lt;code&gt;cache-handler.mjs&lt;/code&gt; (which imports &lt;code&gt;node:crypto&lt;/code&gt; and &lt;code&gt;@google-cloud/storage&lt;/code&gt;) into the Edge middleware chunk — even though middleware never imported it directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; migrate to &lt;code&gt;proxy.ts&lt;/code&gt; (Next.js 16's Node.js-default request interceptor). Edge middleware is a poor fit when your server graph touches Node-only APIs like GCS clients or &lt;code&gt;node:v8&lt;/code&gt; serialization.&lt;/p&gt;




&lt;h2&gt;
  
  
  Operational safeguards
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Startup logging
&lt;/h3&gt;

&lt;p&gt;On boot, the handler logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[cache-handler] GCS cache enabled: gs://bucket/next-cache/{BUILD_ID}/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;GCS_BUCKET&lt;/code&gt; is unset, it warns and falls back to in-memory cache (fine for local dev, &lt;strong&gt;not&lt;/strong&gt; for multi-instance production).&lt;/p&gt;

&lt;h3&gt;
  
  
  Post-deploy CI check
&lt;/h3&gt;

&lt;p&gt;After every Cloud Run deploy, GitHub Actions runs a smoke script that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fetches homepage HTML&lt;/li&gt;
&lt;li&gt;Extracts a &lt;code&gt;/_next/static/…css&lt;/code&gt; reference&lt;/li&gt;
&lt;li&gt;Confirms CSS returns HTTP 200&lt;/li&gt;
&lt;li&gt;Logs &lt;code&gt;X-Next-Build-Id&lt;/code&gt; and &lt;code&gt;Cache-Control&lt;/code&gt; for debugging&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This catches "HTML references dead assets" before customers do.&lt;/p&gt;

&lt;h3&gt;
  
  
  GCS lifecycle
&lt;/h3&gt;

&lt;p&gt;Old build prefixes accumulate. A bucket lifecycle rule deletes objects under &lt;code&gt;next-cache/&lt;/code&gt; after 30 days. Other bucket prefixes (CMS uploads, user files, etc.) are unaffected.&lt;/p&gt;

&lt;p&gt;Manual purge if needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud storage rm -r &lt;span class="s2"&gt;"gs://your-bucket/next-cache/**"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Architecture diagram
&lt;/h2&gt;



&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TB
  subgraph deploy [Each deploy]
    Build["docker build\nGITHUB_SHA → BUILD_ID"]
    Image["Cloud Run revision\n.next/static + BUILD_ID file"]
  end

  subgraph runtime [Request path]
    User[Browser]
    CR[Cloud Run instance]
    NX[Next.js server]
    CH[cache-handler.mjs]
    GCS[(GCS bucket\nnext-cache/BUILD_ID/)]
  end

  Build --&amp;gt; Image
  User --&amp;gt; CR --&amp;gt; NX
  NX --&amp;gt;|get/set cache| CH
  CH --&amp;gt; GCS
  NX --&amp;gt;|/_next/static/*| User&lt;/code&gt;&lt;/pre&gt;






&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Treat the filesystem as ephemeral.&lt;/strong&gt; Anything in &lt;code&gt;.next/cache&lt;/code&gt; must be externalized for production serverless.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scope cache by build ID&lt;/strong&gt; — never share rendered HTML cache across deploys. Read &lt;code&gt;.next/BUILD_ID&lt;/code&gt;, don't guess from env vars alone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use binary serialization&lt;/strong&gt; for Next.js cache values — JSON will corrupt internal structures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set &lt;code&gt;cacheMaxMemorySize: 0&lt;/code&gt;&lt;/strong&gt; when you want GCS to be authoritative, not a silent in-memory fallback per instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify after deploy&lt;/strong&gt; — confirm HTML-referenced CSS returns 200 before calling the release done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plan for stale browser tabs&lt;/strong&gt; — server cache fixes aren't enough for long-lived single-page sessions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch Edge vs Node boundaries&lt;/strong&gt; — custom cache handlers and GCS clients belong on Node, not in middleware bundles.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Stack reference
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Piece&lt;/th&gt;
&lt;th&gt;Choice&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;td&gt;Next.js 16 (App Router, standalone output)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosting&lt;/td&gt;
&lt;td&gt;Google Cloud Run&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cache storage&lt;/td&gt;
&lt;td&gt;Google Cloud Storage (custom &lt;code&gt;cacheHandler&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CI/CD&lt;/td&gt;
&lt;td&gt;GitHub Actions → Artifact Registry → Cloud Run&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Build ID&lt;/td&gt;
&lt;td&gt;Git commit SHA via &lt;code&gt;GITHUB_SHA&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;&lt;em&gt;Building on Next.js or moving a product to GCP? &lt;a href="https://cusicdigital.com/contact" rel="noopener noreferrer"&gt;Get in touch with Cusic Digital&lt;/a&gt; — we help teams ship, scale, and productionize custom web platforms.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://cusicdigital.com/insights/fixing-broken-styles-after-deploy-nextjs-cloud-run-and-a-custom-gcs-cache-handler" rel="noopener noreferrer"&gt;cusicdigital.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>googlecloud</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>From 67 to 95: What Was Really Slowing Down Our Next.js Homepage</title>
      <dc:creator>Eamon Cusic, Jr.</dc:creator>
      <pubDate>Tue, 24 Nov 2026 04:38:54 +0000</pubDate>
      <link>https://dev.to/cusicdigital/from-67-to-95-what-was-really-slowing-down-our-nextjs-homepage-48kc</link>
      <guid>https://dev.to/cusicdigital/from-67-to-95-what-was-really-slowing-down-our-nextjs-homepage-48kc</guid>
      <description>&lt;p&gt;We sell website speed. So when our own redesigned homepage went live with a &lt;strong&gt;mobile PageSpeed score of 67&lt;/strong&gt;, it stung.&lt;/p&gt;

&lt;p&gt;The design itself wasn't the problem. Three details were: decoration we had added on purpose and never measured, a framework setting meant to make pages &lt;em&gt;faster&lt;/em&gt;, and one line of CSS on the headline. Fixing them took the homepage to &lt;strong&gt;95-99 on mobile&lt;/strong&gt;, with the main content showing in &lt;strong&gt;1.3-1.7 seconds&lt;/strong&gt; instead of about 5.&lt;/p&gt;

&lt;p&gt;This post walks through all three, with the numbers, the code, and what we'd check on any site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; engineers working on Next.js (or any framework that renders on the server and hydrates in the browser), and owners who want to know why a site that looks fine on their laptop scores badly on Google's phone test.&lt;/p&gt;




&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If you are…&lt;/th&gt;
&lt;th&gt;Here's the headline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Business / product&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The page was carrying far more than it showed: hundreds of tiny animations and three copies of the same styling. Removing the duplicates made it lighter and let the headline appear first. Nothing visible changed.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Engineering&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;762 individually animated SVG paths were serialized into both the HTML and the RSC payload; we merged them into 50. Then &lt;code&gt;experimental.inlineCss&lt;/code&gt; put an 84 KB stylesheet in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and Next copied it twice more into the RSC payload; turning it off took the compressed homepage from 123 KB to 74 KB. Finally, the hero headline faded in from &lt;code&gt;opacity: 0&lt;/code&gt;, so it couldn't count as the LCP element.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  How we measured
&lt;/h2&gt;

&lt;p&gt;All the "live" numbers below come from Google's &lt;a href="https://pagespeed.web.dev/" rel="noopener noreferrer"&gt;PageSpeed Insights&lt;/a&gt; on the production homepage, phone mode. A single run can swing by several points, so we ran three and compared them.&lt;/p&gt;

&lt;p&gt;The metric that matters most here is &lt;strong&gt;Largest Contentful Paint (LCP)&lt;/strong&gt;: how long until the biggest piece of content in view, usually the headline or hero image, is shown. Google treats under 2.5 seconds as good.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem 1: 762 animations nobody counted
&lt;/h2&gt;

&lt;p&gt;The redesign has a "garden" theme. Root systems grow down from the top of some sections and into the footer, drawn by code so they look organic but render the same every time.&lt;/p&gt;

&lt;p&gt;Each root was its own animated SVG &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt;. The homepage had &lt;strong&gt;502&lt;/strong&gt; of them in one section, and the footer added &lt;strong&gt;231&lt;/strong&gt; more, on every page. Each one also had to be serialized twice: once into the HTML and once into the data React uses to take over the page in the browser (the RSC payload).&lt;/p&gt;

&lt;p&gt;PageSpeed's verdict on launch day:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;762&lt;/strong&gt; non-composited animated elements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;4.4 seconds&lt;/strong&gt; of main-thread work on a throttled phone&lt;/li&gt;
&lt;li&gt;Homepage HTML: &lt;strong&gt;800 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Mobile performance: &lt;strong&gt;67&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;The roots didn't need to be individual elements to &lt;em&gt;look&lt;/em&gt; individual. We now draw one combined path per depth and stroke width, plus one for the tips, and reveal the whole network with a single &lt;code&gt;clip-path&lt;/code&gt; transition from the top down. The geometry is identical, so the footer roots still meet the seedlings above them.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SVG paths&lt;/td&gt;
&lt;td&gt;917&lt;/td&gt;
&lt;td&gt;234&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Animated elements&lt;/td&gt;
&lt;td&gt;762&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Homepage HTML&lt;/td&gt;
&lt;td&gt;800 KB&lt;/td&gt;
&lt;td&gt;590 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The lesson:&lt;/strong&gt; decorative motion has a cost per element, not per effect. Ten thousand pixels of animation in one element is cheap. The same picture split into hundreds of animated pieces is not, and on a server-rendered page you pay for it twice.&lt;/p&gt;

&lt;p&gt;It helped, but it wasn't the whole story. A month later the score was in the high 70s and the main content still took &lt;strong&gt;4.8-5.0 seconds&lt;/strong&gt; to appear on Google's phone test, and our own &lt;a href="https://cusicdigital.com/check" rel="noopener noreferrer"&gt;free website check&lt;/a&gt; put "slower than it should be on phones" at the top of our list. Being flagged by your own tool is a good motivator.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem 2: the stylesheet, three times
&lt;/h2&gt;

&lt;p&gt;Next.js has an experimental setting, &lt;code&gt;inlineCss&lt;/code&gt;, that puts your stylesheet directly in each page instead of in a separate file. The idea is sound: the browser can paint without waiting on a second, render-blocking request. We had turned it on for exactly that reason.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.ts (before)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;experimental&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Ship the stylesheet inside the HTML, so first paint doesn't wait&lt;/span&gt;
    &lt;span class="c1"&gt;// on a separate render-blocking request.&lt;/span&gt;
    &lt;span class="n"&gt;inlineCss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we hadn't checked was what it did to the page's size. Looking at the raw HTML, the 84 KB Tailwind stylesheet appeared in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; as expected, and then &lt;strong&gt;twice more inside the RSC payload&lt;/strong&gt; that Next.js streams with the page. Every page carried the same CSS three times, about 40% of the homepage. On a phone, all of it downloads before the headline can appear.&lt;/p&gt;

&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;We turned the setting off. The CSS goes back to being one separate file, which the browser downloads once and reuses on every other page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.ts (after)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// No experimental.inlineCss: it put the 84 KB stylesheet in every page and&lt;/span&gt;
  &lt;span class="c1"&gt;// Next also copied it twice into the RSC payload. A separate, cached&lt;/span&gt;
  &lt;span class="c1"&gt;// stylesheet is lighter.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compressed homepage went from &lt;strong&gt;123 KB to 74 KB&lt;/strong&gt;, and every other page got the same saving.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The lesson:&lt;/strong&gt; "inline the critical CSS" is good advice when the CSS is small and appears once. Check what your framework actually sends. View the page source, search for a distinctive class name, and count how many times it shows up.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem 3: the headline that wasn't there yet
&lt;/h2&gt;

&lt;p&gt;The last issue was the smallest change and the most interesting cause.&lt;/p&gt;

&lt;p&gt;The hero headline had an entrance animation: it rose into place while fading in from fully transparent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* before */&lt;/span&gt;
&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="nt"&gt;hero-rise&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;26px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looked nice. But an element at &lt;code&gt;opacity: 0&lt;/code&gt; can't count as the page's largest content. When the browser looked for the LCP element, the headline was invisible, so it picked the next biggest thing: our small logo. PageSpeed then charged all the page's early JavaScript work to that logo, and reported a long "render delay" for the page's main content.&lt;/p&gt;

&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;The headline still moves; it just never disappears. It settles into place from a few pixels lower, fully visible from the first frame.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* after: the LCP element is never invisible */&lt;/span&gt;
&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="nt"&gt;hero-settle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visitors who ask their device for reduced motion get no animation at all, as before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The lesson:&lt;/strong&gt; never fade in your LCP element. Animate the decoration around it, or animate position only.&lt;/p&gt;




&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;PageSpeed Insights on the live homepage, phone mode, three runs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Launch day&lt;/th&gt;
&lt;th&gt;A month later&lt;/th&gt;
&lt;th&gt;Now&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Mobile performance&lt;/td&gt;
&lt;td&gt;67&lt;/td&gt;
&lt;td&gt;high 70s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;95, 95, 99&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main content shown (LCP)&lt;/td&gt;
&lt;td&gt;n/a&lt;/td&gt;
&lt;td&gt;4.8-5.0 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.7, 1.7, 1.3 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accessibility, Best Practices, SEO&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The page looks exactly as it did on launch day. It just stopped carrying things nobody could see.&lt;/p&gt;




&lt;h2&gt;
  
  
  A checklist for your own site
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Run PageSpeed Insights three times on mobile&lt;/strong&gt; and look at the middle result, not the best one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find your LCP element&lt;/strong&gt; (PageSpeed names it). If it isn't your headline or hero image, ask why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never fade in that element.&lt;/strong&gt; Animate position, or animate something else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;View the page source and search for one of your CSS class names.&lt;/strong&gt; If it appears more than once, you're shipping duplicates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count animated elements, not animations.&lt;/strong&gt; Hundreds of small animated SVG pieces cost far more than one larger one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-measure on the live site after every fix.&lt;/strong&gt; Local Lighthouse runs a different version and a different network, so treat it as direction, not the final word.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you'd rather not do this by hand, our &lt;a href="https://cusicdigital.com/check" rel="noopener noreferrer"&gt;free website check&lt;/a&gt; runs Google's test three times, names the element holding your page back, and tells you what to fix first. It's the same check that flagged ours.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://cusicdigital.com/insights/what-slowed-down-our-nextjs-homepage" rel="noopener noreferrer"&gt;cusicdigital.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>performance</category>
      <category>webdev</category>
      <category>react</category>
    </item>
  </channel>
</rss>
