{"record":{"id":"c04130c4ae225fed","repo":"sveltejs/kit","slug":"u001b-1m-u001b-31mremoving-comments-in-transformp","errorCode":null,"errorMessage":"\\u001B[1m\\u001B[31mRemoving comments in transformPageChunk can break Svelte's hydration\\u001B[39m\\u001B[22m","messagePattern":"\\\\u001B\\[1m\\\\u001B\\[31mRemoving comments in transformPageChunk can break Svelte's hydration\\\\u001B\\[39m\\\\u001B\\[22m","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/kit/src/runtime/server/page/render.js","lineNumber":642,"sourceCode":"\t});\n\n\t// TODO flush chunks as early as we can\n\tconst transformed =\n\t\t(await resolve_opts.transformPageChunk({\n\t\t\thtml,\n\t\t\tdone: true\n\t\t})) || '';\n\n\tif (!chunks) {\n\t\theaders.set('etag', `\"${hash(transformed)}\"`);\n\t}\n\n\tif (DEV) {\n\t\tif (page_config.csr) {\n\t\t\tif (count_non_ssi_comments(transformed) < count_non_ssi_comments(html)) {\n\t\t\t\t// the \\u001B stuff is ANSI codes, so that we don't need to add a library to the runtime\n\t\t\t\t// https://svelte.dev/playground/1b3f49696f0c44c881c34587f2537aa2?version=4.2.19\n\t\t\t\tconsole.warn(\n\t\t\t\t\t\"\\u001B[1m\\u001B[31mRemoving comments in transformPageChunk can break Svelte's hydration\\u001B[39m\\u001B[22m\"\n\t\t\t\t);\n\t\t\t}\n\t\t} else {\n\t\t\tif (chunks) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t'\\u001B[1m\\u001B[31mReturning promises from server `load` functions will only work if `csr === true`\\u001B[39m\\u001B[22m'\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn chunks\n\t\t? new Response(stream_text(transformed + '\\n', chunks), { status, headers })\n\t\t: text(transformed, { status, headers });\n}\n\nclass Head {","sourceCodeStart":624,"sourceCodeEnd":660,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/server/page/render.js#L624-L660","documentation":"In dev, after running the user's transformPageChunk hook, SvelteKit counts non-SSI HTML comments in the output. If comments were removed (e.g. a minifier or regex stripping <!-- -->), Svelte's hydration markers (which are HTML comments) are gone and client hydration will break, so it prints a bold red console warning. It applies only when csr is enabled, since hydration only happens then.","triggerScenarios":"A transformPageChunk hook in svelte.config.js that strips HTML comments (e.g. html.replace(/<!--.*?-->/gs, '') or an HTML minifier) while csr === true and the app runs in DEV mode; the warning fires when the transformed chunk has fewer non-SSI comments than the original.","commonSituations":"Adding an HTML minifier/optimizer to transformPageChunk to shrink payload size; regex-based cleanup of the SSR output; copying minification snippets from production guides into a dev server config.","solutions":["Remove any comment-stripping logic from transformPageChunk, at least when DEV is true","Only strip comments that are not Svelte hydration markers, or use an SSR-aware minifier that preserves them","Disable the minification when csr is true (keep it only for csr === false builds where nothing hydrates)"],"exampleFix":"// before\ntransformPageChunk: ({ html }) => html.replace(/<!--[\\s\\S]*?-->/g, '');\n\n// after\ntransformPageChunk: ({ html, done }) => {\n  if (done) return html; // preserve Svelte hydration comments\n  return html;\n};","handlingStrategy":"validation","validationCode":"const before = countComments(html);\nconst after = countComments(transform(html));\nif (after < before) console.warn('transformPageChunk removed comments; hydration may break');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not strip HTML comments in transformPageChunk","Test hydration after any SSR output transformation","Gate any minification behind !DEV and csr === false"],"tags":["ssr","hydration","vite-hook","dev-warning"],"backgroundTag":"hydration-comments-removed","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}