Skip to content
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

Merged
merged 18 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
include Content-Length header when streaming is disabled
  • Loading branch information
Tony Sullivan committed Jun 30, 2022
commit 1869202299717465d9345e0652fd4e255f6b8263
7 changes: 5 additions & 2 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,8 @@ export async function renderPage(

if (isAstroComponent(factoryReturnValue)) {
let iterable = renderAstroComponent(factoryReturnValue);
let init = result.response;
let headers = new Headers(init.headers);
let body: BodyInit;
if (streaming) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah, I wish CF supported ReadableStream. I'm still on a mission to reduce forking code paths, so in that spirit I'd love to use ReadableStream in both cases and then just pipe it to a string as a smaller, additional step in the non-streaming code path.

I can't think of any good way to remove the copy-paste code between the two if-else code paths, so I guess this might not be actionable. Just venting I guess :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was actually my first solution! We can't use ReadableStream in CF at all unfortunately, the constructor throws an error immediately. Even worse, the error is basically silent if you don't add an extra try/catch around the entire SSR handler and logs are accessible yet for Cloudflare Pages Functions 🙃

body = new ReadableStream({
Expand Down Expand Up @@ -766,9 +768,10 @@ export async function renderPage(
body += chunk;
i++;
}
headers.set('Content-Length', `${Buffer.byteLength(body, 'utf-8')}`);
}
let init = result.response;
let response = createResponse(body, init);

let response = createResponse(body, { ...init, headers });
return response;
} else {
return factoryReturnValue;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/test/streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ describe('Streaming disabled', () => {

expect(response.status).to.equal(200);
expect(response.headers.get('content-type')).to.equal('text/html');
expect(response.headers.has('content-length')).to.equal(true);
expect(parseInt(response.headers.get('content-length'))).to.be.greaterThan(0);

const html = await response.text();
const $ = cheerio.load(html);
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.