Skip to content

SPA, SSR and hydration

__NUXT_DATA__ parses as JSON but reads as an index table

Turn a Nuxt page's __NUXT_DATA__ array back into objects with devalue's unflatten and revivers for Nuxt's type tags, without a browser and without eval.

By Cerys Eaton, SiteRender team · Published

This article was drafted with AI assistance from the sources listed below and edited and published by the SiteRender team.

From __NUXT_DATA__ to readable objects
  1. 1Find the elementid __NUXT_DATA__, or a data-nuxt-data attribute in multi-app mode.
  2. 2JSON.parse the textYou get one flat array: values are indices into it.
  3. 3unflatten with reviversMap Nuxt's tags such as ShallowReactive and Ref to plain values.
  4. 4Check data-srcIf set, part of the data comes from the URL in data-src.
  5. 5Compare with the pageThe visible text wins when the two disagree.

The page is server-rendered with Nuxt, the text you want is not in a tidy <article>, and the HTML has a script element with the id __NUXT_DATA__. You run JSON.parse on it and it works. The result is a long array of numbers, strings and short lists. Nothing in it looks like an object you can read.

That is not broken data. It is devalue's format, and you need one more step to read it. The Next.js hydration data post covers the same idea for __NEXT_DATA__; this post does the Nuxt side.

Find the element

URL=https://example.com/
curl -sL -H 'Accept-Encoding: identity' "$URL" -o raw.html
grep -oE '<script[^>]*(id="__NUXT_DATA__"|data-nuxt-data=)[^>]*>' raw.html

Nuxt's client looks the element up by the id __NUXT_DATA__. In multi-app mode it uses a data-nuxt-data attribute instead, which is why the grep matches both. No match: the page has no inline payload, so look at the rendering mode the page uses before you go further.

Symptom: JSON.parse gives an index table

devalue writes a value as one flat array. Inside an object or array entry, a number is not a number: it is the position of another entry. A hand-made example in that format:

[["ShallowReactive",1],{"data":2,"state":5,"serverRendered":6},{"article":3},{"title":4},"Hello from example.com",{},true]

Entry 0 is the root. Its tag points to entry 1, whose data points to entry 2, and so on down to the string at entry 4. A value used twice is stored once and referenced twice, which is how the format handles repeated and circular references.

Negative numbers are sentinels, defined in devalue's constants.js:

ValueMeaning
-1undefined
-2array hole
-3NaN
-4Infinity
-5-Infinity
-6-0
-7sparse array marker

A list whose first item is a string is a typed value: the string is the tag. Walking this by hand works for a small page. It does not scale, and it gets the sentinels wrong the first time.

Symptom: devalue.parse throws "Unknown type"

The obvious fix is devalue's own parse, which is JSON.parse followed by unflatten. On a Nuxt payload it fails:

Error: Unknown type ShallowReactive

devalue's parser knows a fixed set of tags: Date, Set, Map, RegExp, BigInt, URL, the typed arrays and a few more. For any other tag it looks for a reviver you passed in, and if there is none it throws Unknown type with the tag name.

Nuxt adds its own tags. Its client plugin revive-payload.client.ts registers revivers for NuxtError, EmptyShallowRef, EmptyRef, ShallowRef, ShallowReactive, Ref and Reactive, plus Island when component islands are on. In the browser those turn values back into Vue refs and reactive objects. A reader needs none of that. The plain value inside is enough.

Verdict: call unflatten yourself and pass a reviver for each Nuxt tag.

The script

// nuxt-data.mjs: npm install devalue
import { readFileSync } from "node:fs";
import { unflatten } from "devalue";

const html = readFileSync(process.argv[2], "utf8");
const match = html.match(
  /<script\b[^>]*(?:id="__NUXT_DATA__"|data-nuxt-data=)[^>]*>([\s\S]*?)<\/script>/
);
if (!match) {
  console.error("no __NUXT_DATA__ element");
  process.exit(1);
}

const same = (value) => value;
const empty = (value) => (value === "_" ? undefined : value);
const revivers = {
  NuxtError: same,
  EmptyShallowRef: empty,
  EmptyRef: empty,
  ShallowRef: same,
  ShallowReactive: same,
  Ref: same,
  Reactive: same,
  Island: same,
};

const payload = unflatten(JSON.parse(match[1]), revivers);
const toJson = (key, value) => (typeof value === "bigint" ? value.toString() : value);
console.log(JSON.stringify(payload, toJson, 2));
node nuxt-data.mjs raw.html > payload.json

For the two Empty… tags, Nuxt's own reviver treats the string "_" as undefined; the script does the same and keeps every other value as it is. The toJson replacer turns BigInt values, which devalue revives, into strings for the output.

Nothing here runs the page's code. JSON.parse and unflatten only build data.

Symptom: a new "Unknown type" error

Apps can register their own types. Nuxt exposes definePayloadReducer on the server and definePayloadReviver on the client for that, and the name is the tag that lands in the payload. If the script stops with Unknown type and a name that is not in the list above, the site added a custom type. Add that name to revivers with same and look at what comes out.

Symptom: the data is thinner than the page

Check the element's attributes again:

grep -oE '<script[^>]*id="__NUXT_DATA__"[^>]*>' raw.html | grep -o 'data-src="[^"]*"'

A data-src value means the client also loads an external payload file from that URL, and merges it with the inline data. Nuxt's client merges in this order: inline data, then the external file, then window.__NUXT__. So the inline element can be only part of what the page renders. The file is referenced from the page itself; read it like any other public URL on the site, under the same robots.txt and terms.

Symptom: a window.NUXT script and no JSON

Some pages set window.__NUXT__ in a plain script instead. That is JavaScript, not JSON. devalue's README is explicit that its uneval output is code that has to be evaluated, and the same holds for any payload written as a script. Evaluating a page's script in your process means running code from the Internet. Do not eval it and do not wrap it in new Function.

Read the visible text first. If you need the payload, run the page in an isolated browser and read the result there.

Test it yourself

Take one Nuxt page you can see in a browser. Fetch it, run the grep, run nuxt-data.mjs and search payload.json for a sentence from the main content:

grep -c "a sentence you can see on the page" payload.json

One or more: the content is in the first response and you can read it without a browser. Zero: check data-src, then the visible text. When the payload and the page disagree, trust the page. The payload is what the app hydrates from, not a promise about what it shows.

Frequently asked questions

Why does __NUXT_DATA__ look like a list of numbers and strings?

Nuxt serializes its payload with devalue. The output is one flat JSON array in which object and array values are indices that point to other entries, so repeated and circular references are stored once. Negative numbers are sentinels for values like undefined and NaN.

Why does devalue.parse throw Unknown type ShallowReactive?

ShallowReactive is a Nuxt type tag, not one of devalue's built-in types. devalue throws an Unknown type error for any tag it has no reviver for. Pass a reviver for each Nuxt tag, such as ShallowReactive, Reactive, Ref, ShallowRef, EmptyRef, EmptyShallowRef and NuxtError.

Can I eval window.__NUXT__ to get the data?

Do not. A window.__NUXT__ assignment in a script tag is JavaScript, and running it means running code from the page in your own process. Parse the JSON element instead, or read the page's visible text.

What does data-src on the __NUXT_DATA__ element mean?

Nuxt's client reads an external payload file from the URL in data-src and merges it with the inline data. In that case the inline element may not hold everything the page renders.

Sources

  1. devalue README (Documentation)
  2. devalue parse.js (source) (Documentation)
  3. devalue constants.js (source) (Documentation)
  4. Nuxt payload.ts (source) (Documentation)
  5. Nuxt revive-payload.client.ts (source) (Documentation)