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

Conversation

tony-sull
Copy link
Contributor

@tony-sull tony-sull commented Jun 30, 2022

Closes #3764

Changes

This adds an option to Astro's app server to disable HTTP streaming (enabled by default)

Cloudflare Page Functions don't fully support ReadableStream yet - it is defined so a polyfill wouldn't really do the trick, but the server will throw an error when the ReadableStream constructor is called. This change updates the Cloudflare adapter to always disable streaming

Testing

  • Added tests for disabling streaming in dev and production
  • Manually tested the fix deploying to Cloudflare Pages

Docs

Doc comments added for new config

@changeset-bot
Copy link

changeset-bot bot commented Jun 30, 2022

🦋 Changeset detected

Latest commit: 14952ca

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@astrojs/cloudflare Patch
astro Patch
@e2e/astro-component Patch
@e2e/lit-component Patch
@e2e/preact-component Patch
@e2e/react-component Patch
@e2e/solid-component Patch
@e2e/svelte-component Patch
@e2e/e2e-tailwindcss Patch
@e2e/ts-resolution Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) labels Jun 30, 2022
@tony-sull tony-sull marked this pull request as ready for review June 30, 2022 19:28
@tony-sull tony-sull changed the title WIP: Adding an option to disable HTTP streaming Adding an option to disable HTTP streaming Jun 30, 2022
* }
* ```
*/
streaming?: boolean;
Copy link
Contributor Author

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!

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 🙃

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM!

@FredKSchott
Copy link
Member

@tony-sull big fan of starting as an option to App as an incremental step. We may still eventually get a non-CF user asking for the ability to turn it off in their final build output, but we can cross that bridge when we come to it and it will be an easy switch to flip when we do.

@tony-sull tony-sull merged commit 976e1f1 into main Jul 1, 2022
@tony-sull tony-sull deleted the fix/cloudflare-polyfill branch July 1, 2022 02:30
@astrobot-houston astrobot-houston mentioned this pull request Jul 1, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Adding a flag to disable HTTP streaming

* refactor: adding support for SSG builds

* handling string responses in the server runtime, adding tests

* removing streaming CLI flag

* removing import.meta.env.STREAMING

* include Content-Length header when streaming is disabled

* Verifying content-length header in dev

* fix: default streaming to enabled in the base App server

* TEMP: disabling the production test to investigate the test-adapter

* re-enabling the test with an adapter option to disable streaming for the test

* fix: use the existing TextEncoder to get the body's byte length

* moving config to build.streaming, ignoring it in `dev`

* fixing dev test to expect response streaming

* chore: add changsets

* removing the new config option all together 🎉

* remove temp debug log

* Updating astro changeset now that streaming isn't a config option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Streaming (1.0.0-beta.55) breaks Cloudflare Pages
2 participants