{"record":{"id":"620cd2daea185186","repo":"withastro/astro","slug":"responsesenterror-620cd2","errorCode":"ResponseSentError","errorMessage":"The response has already been sent to the browser and cannot be altered.","messagePattern":"The response has already been sent to the browser and cannot be altered\\.","errorType":"exception","errorClass":"AstroError","httpStatus":null,"severity":"error","filePath":"packages/astro/src/runtime/server/render/astro/render.ts","lineNumber":96,"sourceCode":"\n\treturn new ReadableStream({\n\t\tstart(controller) {\n\t\t\tconst destination: RenderDestination = {\n\t\t\t\twrite(chunk) {\n\t\t\t\t\t// Automatic doctype insertion for pages\n\t\t\t\t\tif (isPage && !renderedFirstPageChunk) {\n\t\t\t\t\t\trenderedFirstPageChunk = true;\n\t\t\t\t\t\tif (!result.partial && !DOCTYPE_EXP.test(String(chunk))) {\n\t\t\t\t\t\t\tconst doctype = result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\\n';\n\t\t\t\t\t\t\tcontroller.enqueue(encoder.encode(doctype));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// `chunk` might be a Response that contains a redirect,\n\t\t\t\t\t// that was rendered eagerly and therefore bypassed the early check\n\t\t\t\t\t// whether headers can still be modified. In that case, throw an error\n\t\t\t\t\tif (chunk instanceof Response) {\n\t\t\t\t\t\tthrow new AstroError({\n\t\t\t\t\t\t\t...AstroErrorData.ResponseSentError,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst bytes = chunkToByteArray(result, chunk);\n\t\t\t\t\tcontroller.enqueue(bytes);\n\t\t\t\t},\n\t\t\t};\n\n\t\t\t(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tawait renderStreaming(templateResult, result, destination);\n\t\t\t\t\tcontroller.close();\n\t\t\t\t} catch (e) {\n\t\t\t\t\t// We don't have a lot of information downstream, and upstream we can't catch the error properly\n\t\t\t\t\t// So let's add the location here\n\t\t\t\t\tif (AstroError.is(e) && !e.loc) {\n\t\t\t\t\t\te.setLocation({","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/withastro/astro/blob/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/runtime/server/render/astro/render.ts#L78-L114","documentation":"In the streaming render destination, each chunk is written to the browser as it's produced. If a chunk is itself a `Response` (typically a redirect produced eagerly inside the component tree), it arrives after the first page chunk was already enqueued — meaning headers/status have already been committed. Astro cannot honor a late Response, so it throws `ResponseSentError`.","triggerScenarios":"A `Response`/redirect returned from deep inside a streaming page after some HTML already flushed (e.g. inside a slotted child, an async island, or a component rendered after partial output); `return Astro.redirect(...)` placed after early rendering work in a streamed page.","commonSituations":"Conditional redirect logic placed mid-page instead of at the top of frontmatter; redirects emitted from nested components/slots in on-demand streaming pages; auth checks that run after some content renders.","solutions":["Move redirect/Response returns to the very top of the page frontmatter, before any markup or awaited render work.","Perform auth/condition checks before returning any HTML so the Response is set before the stream starts.","Avoid returning a Response from inside child components or slots of a streaming page."],"exampleFix":"// before — redirect after partial render in a streaming page\n---\n<html><body>Hello</body></html>\n{needsLogin ? Astro.redirect('/login') : null}\n---\n\n// after — redirect before any output\n---\nif (needsLogin) return Astro.redirect('/login');\n---\n<html><body>Hello</body></html>","handlingStrategy":"validation","validationCode":"// Page-level discipline: return Response before rendering\n// (no runtime guard possible once the stream has flushed)\nexport default async function (astro: APIContext): Promise<Response | void> {\n  if (await needsRedirect(astro)) return Astro.redirect('/elsewhere');\n  // render only after all Response decisions are made\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Put every redirect/auth check at the very top of page frontmatter, before any markup.","Never return a Response from nested components or slots in a streaming page.","Treat 'Response before output' as a hard ordering rule."],"tags":["streaming","response","redirect","render"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}