[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-nuxt-4-shallowref-data-not-updating":3},{"content":4,"lastModified":5},"\u003Cp>The ticket was short: pending review count says 4, database says 3, only happens if you sit on the page. I closed it as a caching issue, which is what you write when you have not looked yet. It came back twice in one week, and both reporters had recently pulled the Nuxt 4 migration branch.\u003C\u002Fp>\n\n\u003Cp>That dashboard does nothing clever. It loads its numbers with \u003Ccode>useAsyncData\u003C\u002Fcode>, and when someone approves a review it posts, then patches the local copy so the UI does not wait for a refetch.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-vue\">const { data: stats } = await useAsyncData('dashboard-stats', () => $fetch('\u002Fapi\u002Fstats'))\n\nasync function approve(id) {\n  await $fetch(`\u002Fapi\u002Freviews\u002F${id}\u002Fapprove`, { method: 'POST' })\n  stats.value.pending -= 1\n  stats.value.recent.unshift({ id, state: 'approved' })\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>On Nuxt 3 that worked. On Nuxt 4 the badge sat there until I navigated away and back, which made the fetch look stale. It was not. The fetch was fine. The component simply never re-rendered.\u003C\u002Fp>\n\n\u003Cp>Here is the line that explains it, from \u003Ccode>node_modules\u002Fnuxt\u002Fdist\u002Fapp\u002Fcomposables\u002FasyncData.js\u003C\u002Fcode>:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">const _ref = options.deep ? ref : shallowRef;\n\u002F\u002F a few lines further down\ndata: _ref(hasCachedData ? initialCachedData : options.default()),\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>The option's own comment in \u003Ccode>asyncData.d.ts\u003C\u002Fcode> says \u003Ccode>deep\u003C\u002Fcode> is false by default. So \u003Ccode>data\u003C\u002Fcode> is a plain \u003Ccode>shallowRef\u003C\u002Fcode> now, which means tracking stops one level in. Assigning \u003Ccode>stats.value\u003C\u002Fcode> is a signal. Changing \u003Ccode>stats.value.pending\u003C\u002Fcode> is not, because that object was never wrapped in a proxy.\u003C\u002Fp>\n\n\u003Ch2>Measured, not guessed\u003C\u002Fh2>\n\n\u003Cp>I did not want to trust my own reading of a bundled file, so I ran the smallest probe I could against the Vue build in this project. One effect reading two nested fields, three nested mutations, one replacement:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">import { shallowRef, ref, isShallow, isReactive, effect } from 'vue'\n\nfunction probe(label, makeRef) {\n  const data = makeRef({ rows: [{ id: 1 }] })\n  let runs = 0\n  effect(() => { runs++; data.value.rows.length + (data.value.rows[0]?.id ?? 0) })\n\n  data.value.rows.push({ id: 2 })              \u002F\u002F nested push\n  data.value.rows[0].id = 99                   \u002F\u002F nested set\n  data.value.rows.sort((a, b) => b.id - a.id)  \u002F\u002F nested reorder\n  data.value = { rows: [{ id: 5 }] }           \u002F\u002F replacement\n\n  console.log(label, isShallow(data), isReactive(data.value), runs)\n}\n\nprobe('shallow', shallowRef) \u002F\u002F true  false 2\nprobe('deep', ref)           \u002F\u002F false true  4\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Two runs for the shallow ref: the initial one and the replacement. All three nested mutations went past it unseen. The deep ref caught the push and the nested set as well, and stayed quiet on the sort only because that array happened to be in order already.\u003C\u002Fp>\n\n\u003Ch2>Why it reads like a caching bug\u003C\u002Fh2>\n\n\u003Cp>Nothing got frozen or copied. The object is still live, so the next time the component renders for any reason at all (a route change, a hover state, a parent update), the template reads the mutated object and the right number is there.\u003C\u002Fp>\n\n\u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F5833807\u002Fpexels-photo-5833807.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1600\" alt=\"Three desk clocks on a white table showing three different times, with a dashboard on a monitor behind them\" loading=\"lazy\" \u002F>\n\n\u003Cp>That self-healing is what sends people hunting in the wrong place. A normal bug stays broken. This one is wrong only until something unrelated forces a render, so the suspects become the API, the payload, and \u003Ccode>getCachedData\u003C\u002Fcode>, and all three are innocent.\u003C\u002Fp>\n\n\u003Ch2>Three fixes, and the one I picked\u003C\u002Fh2>\n\n\u003Cp>Cheapest is to stop mutating in place. Build a new object and assign it:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">\u002F\u002F silent under shallowRef\nstats.value.pending -= 1\n\n\u002F\u002F visible\nstats.value = { ...stats.value, pending: stats.value.pending - 1 }\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Second is opting back into deep reactivity for that one call, \u003Ccode>useAsyncData('dashboard-stats', handler, { deep: true })\u003C\u002Fcode>. Third is keeping the shallow ref and holding the parts you mutate in a separate \u003Ccode>ref\u003C\u002Fcode>, which is what I did for the recent list since it also gets locally re-sorted.\u003C\u002Fp>\n\n\u003Cp>I went with replacement plus one local ref and left \u003Ccode>deep: true\u003C\u002Fcode> out. Deep is what we had before, and honestly I liked it. Nuxt changed that default for a reason though: a deep ref turns every object in the response into a proxy the first time something touches it, and most of a stats payload never reaches the DOM. I am not going to undo a deliberate performance default to save three lines of assignment. Shallow is the better call. It is just a call that fails quietly, and quiet failures eat more afternoons than loud ones.\u003C\u002Fp>\n\n\u003Ch2>Traps that ride along\u003C\u002Fh2>\n\n\u003Cp>Before you sprinkle \u003Ccode>deep: true\u003C\u002Fcode> around, know that entries are keyed and \u003Ccode>buildAsyncData\u003C\u002Fcode> only runs for the first call on a key. A second component calling \u003Ccode>useAsyncData('dashboard-stats', handler, { deep: true })\u003C\u002Fcode> gets handed the existing shallow ref, plus a dev warning about a mismatching \u003Ccode>deep\u003C\u002Fcode> option. The first caller decides, not the one asking for deep.\u003C\u002Fp>\n\n\u003Cp>The shared key also means shared \u003Ccode>data\u003C\u002Fcode>, \u003Ccode>error\u003C\u002Fcode> and \u003Ccode>status\u003C\u002Fcode>, so a second call with its own \u003Ccode>transform\u003C\u002Fcode> or \u003Ccode>default\u003C\u002Fcode> will disagree with the first. And \u003Ccode>dedupe\u003C\u002Fcode> no longer takes a boolean. It is \u003Ccode>'cancel'\u003C\u002Fcode> or \u003Ccode>'defer'\u003C\u002Fcode>, defaulting to \u003Ccode>'cancel'\u003C\u002Fcode>.\u003C\u002Fp>\n\n\u003Cp>One more, from the same upgrade and unrelated to reactivity: the initial value is \u003Ccode>undefined\u003C\u002Fcode> now. \u003Ccode>getDefault\u003C\u002Fcode> in that file is literally \u003Ccode>() =&gt; void 0\u003C\u002Fcode>, and \u003Ccode>error\u003C\u002Fcode> is a \u003Ccode>toRef\u003C\u002Fcode> into an errors object that also starts empty. Every \u003Ccode>=== null\u003C\u002Fcode> check around fetched data stops matching. I found four in our codebase, all inside empty-state branches that had been dead for a fortnight without anyone noticing.\u003C\u002Fp>\n\n\u003Ch2>What I run before the next upgrade\u003C\u002Fh2>\n\n\u003Cp>Greps. Cheap and boring:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\"># nested mutation on fetched data\ngrep -rnE \"data\\.value\\.[A-Za-z0-9_]+\\.(push|splice|sort|reverse|shift|unshift)\\(\" \\\n  --include=*.vue --include=*.ts app\u002F\n\ngrep -rnE \"data\\.value\\.[A-Za-z0-9_]+\\s*[-+*\u002F]?=[^=]\" \\\n  --include=*.vue --include=*.ts app\u002F\n\n# null checks that are now dead\ngrep -rnE \"(data|error)\\.value\\s*[!=]==?\\s*null\" --include=*.vue --include=*.ts app\u002F\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>That is the whole audit. Three greps and a read of every hit, which for us was nineteen hits and six real problems. I keep them in \u003Ca href=\"\u002Fsnippetark\u002F\" target=\"_blank\" rel=\"noopener noreferrer\">Snippet Ark\u003C\u002Fa> tagged per repo, because I have retyped that last regex from memory twice and botched it both times.\u003C\u002Fp>\n\n\u003Cp>If you are still on Nuxt 3, the \u003Ca href=\"\u002Fposts\u002Fnuxt-usefetch-vs-fetch-double-fetch-trap\u002F\">useFetch vs $fetch trap\u003C\u002Fa> is the right thing to read before this one, since shallow data changes what \"the data is already there\" actually buys you. And if your migration produced duplicated state between users instead of stale numbers, that is a \u003Ca href=\"\u002Fposts\u002Fnuxt-ssr-state-leak-between-users\u002F\">different bug\u003C\u002Fa> with a similar smell.\u003C\u002Fp>\n\n\u003Cp>The upgrade itself was fine. Nuxt 4 has been solid for us since the first week and I would make the same call again. But this default deserved a louder line in the release notes than it got, because it breaks no build, fails no test, and shows up only as a number that is quietly one behind.\u003C\u002Fp>\n","2026-11-16",1794910419990]