-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding an option to disable HTTP streaming #3777
Changes from 13 commits
9207549
18b3a8b
884fab8
c043ff1
76ce1dd
93ff1b7
1869202
6dde2f9
5af3d7d
939d048
bae78c4
188c86b
693adf2
dc10d4d
bb85482
05e3e4a
c9f5d50
14952ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -704,7 +704,8 @@ export async function renderPage( | |
result: SSRResult, | ||
componentFactory: AstroComponentFactory, | ||
props: any, | ||
children: any | ||
children: any, | ||
streaming: boolean, | ||
): Promise<Response> { | ||
let iterable: AsyncIterable<any>; | ||
if (!componentFactory.isAstroComponentFactory) { | ||
|
@@ -730,28 +731,48 @@ export async function renderPage( | |
const factoryReturnValue = await componentFactory(result, props, children); | ||
|
||
if (isAstroComponent(factoryReturnValue)) { | ||
iterable = renderAstroComponent(factoryReturnValue); | ||
let stream = new ReadableStream({ | ||
start(controller) { | ||
async function read() { | ||
let i = 0; | ||
for await (const chunk of iterable) { | ||
let html = chunk.toString(); | ||
if (i === 0) { | ||
if (!/<!doctype html/i.test(html)) { | ||
controller.enqueue(encoder.encode('<!DOCTYPE html>\n')); | ||
let iterable = renderAstroComponent(factoryReturnValue); | ||
let init = result.response; | ||
let headers = new Headers(init.headers); | ||
let body: BodyInit; | ||
if (streaming) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bah, I wish CF supported I can't think of any good way to remove the copy-paste code between the two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was actually my first solution! We can't use |
||
body = new ReadableStream({ | ||
start(controller) { | ||
async function read() { | ||
let i = 0; | ||
for await (const chunk of iterable) { | ||
let html = chunk.toString(); | ||
if (i === 0) { | ||
if (!/<!doctype html/i.test(html)) { | ||
controller.enqueue(encoder.encode('<!DOCTYPE html>\n')); | ||
} | ||
} | ||
controller.enqueue(encoder.encode(html)); | ||
i++; | ||
} | ||
controller.enqueue(encoder.encode(html)); | ||
i++; | ||
controller.close(); | ||
} | ||
read(); | ||
}, | ||
}); | ||
} else { | ||
body = ''; | ||
let i = 0; | ||
for await (const chunk of iterable) { | ||
let html = chunk.toString(); | ||
if (i === 0) { | ||
if (!/<!doctype html/i.test(html)) { | ||
body += '<!DOCTYPE html>\n'; | ||
} | ||
controller.close(); | ||
} | ||
read(); | ||
}, | ||
}); | ||
let init = result.response; | ||
let response = createResponse(stream, init); | ||
body += chunk; | ||
i++; | ||
} | ||
const bytes = encoder.encode(body); | ||
headers.set('Content-Length', `${bytes.byteLength}`); | ||
} | ||
|
||
let response = createResponse(body, { ...init, headers }); | ||
return response; | ||
} else { | ||
return factoryReturnValue; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think I can actually remove this completely if we don't want dev or preview support!