{"record":{"id":"61b161c64a565125","repo":"DIYgod/RSSHub","slug":"failed-to-parse-next-data-json-error-instan","errorCode":null,"errorMessage":"Failed to parse __NEXT_DATA__ JSON: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to parse __NEXT_DATA__ JSON: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/routes/hupu/utils.ts","lineNumber":18,"sourceCode":"import { load } from 'cheerio';\n\nimport type { DataItem } from '@/types';\nimport cache from '@/utils/cache';\nimport got from '@/utils/got';\nimport { parseDate, parseRelativeDate } from '@/utils/parse-date';\nimport timezone from '@/utils/timezone';\n\nexport function extractNextData<T = unknown>(html: string, url?: string): T {\n    const scriptMatch = html.match(/<script id=\"__NEXT_DATA__\" type=\"application\\/json\">(.*?)<\\/script>/);\n    if (!scriptMatch || !scriptMatch[1]) {\n        throw new Error(`Failed to find __NEXT_DATA__ script tag in page${url ? `: ${url}` : ''}`);\n    }\n\n    try {\n        return JSON.parse(scriptMatch[1]) as T;\n    } catch (error) {\n        throw new Error(`Failed to parse __NEXT_DATA__ JSON: ${error instanceof Error ? error.message : String(error)}`, { cause: error });\n    }\n}\n\ninterface ThreadNextData {\n    props: {\n        pageProps: {\n            threadData: {\n                data: {\n                    moduleConfigList: {\n                        content: {\n                            moduleContent: {\n                                content: string;\n                            };\n                        };\n                    };\n                };\n            };\n        };","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/routes/hupu/utils.ts#L1-L36","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fetch the page HTML manually and inspect the raw content of the `__NEXT_DATA__` script tag for truncation or malformed JSON.","If the data is split across multiple chunks (RSC streaming), update the regex or extraction logic to concatenate all script chunks before parsing.","Add response length validation or check for Content-Encoding issues in the `got` response.","Retry to rule out a transient truncation from network instability."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isValidNextDataJson(content: string): boolean {\n    try {\n        JSON.parse(content);\n        return true;\n    } catch {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    return JSON.parse(scriptMatch[1]) as T;\n} catch (error) {\n    // Log the length and a snippet for diagnosis\n    throw new Error(`Failed to parse __NEXT_DATA__ JSON (length=${scriptMatch[1].length}): ${error instanceof Error ? error.message : String(error)}`, { cause: error });\n}","preventionTips":["Handle RSC streaming where data may be split across multiple script chunks.","Validate the script content length before parsing.","Check for encoding issues (gzip, BOM) in the response."],"tags":["upstream","parsing","json","schema-change","rsshub","bbs"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}