DIYgod/RSSHub · error · Error

Failed to parse __NEXT_DATA__ JSON: ${error instanceof Error

Error message

Failed to parse __NEXT_DATA__ JSON: ${error instanceof Error ? error.message : String(error)}

What it means

Thrown by the `extractNextData` utility when the content inside the `<script id="__NEXT_DATA__">` tag cannot be parsed as JSON via `JSON.parse`. The error includes the underlying parse error message and chains the original error via the `cause` option. This indicates the Next.js data payload is syntactically malformed.

Source

Thrown at lib/routes/hupu/utils.ts:18

import { load } from 'cheerio';

import type { DataItem } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

export function extractNextData<T = unknown>(html: string, url?: string): T {
    const scriptMatch = html.match(/<script id="__NEXT_DATA__" type="application\/json">(.*?)<\/script>/);
    if (!scriptMatch || !scriptMatch[1]) {
        throw new Error(`Failed to find __NEXT_DATA__ script tag in page${url ? `: ${url}` : ''}`);
    }

    try {
        return JSON.parse(scriptMatch[1]) as T;
    } catch (error) {
        throw new Error(`Failed to parse __NEXT_DATA__ JSON: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
    }
}

interface ThreadNextData {
    props: {
        pageProps: {
            threadData: {
                data: {
                    moduleConfigList: {
                        content: {
                            moduleContent: {
                                content: string;
                            };
                        };
                    };
                };
            };
        };

View on GitHub (pinned to bed535e087)

Solutions

  1. Fetch the page HTML manually and inspect the raw content of the `__NEXT_DATA__` script tag for truncation or malformed JSON.
  2. If the data is split across multiple chunks (RSC streaming), update the regex or extraction logic to concatenate all script chunks before parsing.
  3. Add response length validation or check for Content-Encoding issues in the `got` response.
  4. Retry to rule out a transient truncation from network instability.
Defensive patterns

Strategy: try-catch

Type guard

function isValidNextDataJson(content: string): boolean {
    try {
        JSON.parse(content);
        return true;
    } catch {
        return false;
    }
}

Try / catch

try {
    return JSON.parse(scriptMatch[1]) as T;
} catch (error) {
    // Log the length and a snippet for diagnosis
    throw new Error(`Failed to parse __NEXT_DATA__ JSON (length=${scriptMatch[1].length}): ${error instanceof Error ? error.message : String(error)}`, { cause: error });
}

Prevention

When it happens

Trigger: The `__NEXT_DATA__` script content is truncated, contains escaped characters that break JSON syntax, is split across multiple script tags (React Server Components streaming), or the upstream site embeds non-JSON content inside the script tag.

Common situations: The Hupu page uses RSC streaming that fragments the data payload, a CDN or proxy truncates the response, the page has an encoding issue, or the script tag matching regex greedily captures too little/too much content.

Understand the failure class

Related errors


AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12). Data as JSON: /api/errors/61b161c64a565125. Report an issue: GitHub.