Skip to content

Commit

Permalink
Adds experimental support for disabling streaming (#13036)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Lefebvre <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
4 people authored Jan 24, 2025
1 parent 1a14b53 commit 3c90d8f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .changeset/three-steaks-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@astrojs/react": minor
---

Adds experimental support for disabling streaming

This is useful to support libraries that are not compatible with streaming such as some CSS-in-JS libraries. To disable streaming for all React components in your project, set `experimentalDisableStreaming: true` as a configuration option for `@astrojs/react`:

```diff
// astro.config.mjs
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';

export default defineConfig({
integrations: [
react({
+ experimentalDisableStreaming: true,
}),
],
});
```
4 changes: 3 additions & 1 deletion packages/integrations/react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl
formState,
};
let html;
if ('renderToReadableStream' in ReactDOM) {
if (opts.experimentalDisableStreaming) {
html = ReactDOM.renderToString(vnode);
} else if ('renderToReadableStream' in ReactDOM) {
html = await renderToReadableStreamAsync(vnode, renderOptions);
} else {
html = await renderToPipeableStreamAsync(vnode, renderOptions);
Expand Down
27 changes: 22 additions & 5 deletions packages/integrations/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export type ReactIntegrationOptions = Pick<
'include' | 'exclude' | 'babel'
> & {
experimentalReactChildren?: boolean;
/**
* Disable streaming in React components
*/
experimentalDisableStreaming?: boolean;
};

const FAST_REFRESH_PREAMBLE = react.preambleCode;
Expand All @@ -26,7 +30,13 @@ function getRenderer(reactConfig: ReactVersionConfig) {
};
}

function optionsPlugin(experimentalReactChildren: boolean): vite.Plugin {
function optionsPlugin({
experimentalReactChildren = false,
experimentalDisableStreaming = false
}: {
experimentalReactChildren: boolean;
experimentalDisableStreaming: boolean;
}): vite.Plugin {
const virtualModule = 'astro:react:opts';
const virtualModuleId = '\0' + virtualModule;
return {
Expand All @@ -40,7 +50,8 @@ function optionsPlugin(experimentalReactChildren: boolean): vite.Plugin {
if (id === virtualModuleId) {
return {
code: `export default {
experimentalReactChildren: ${JSON.stringify(experimentalReactChildren)}
experimentalReactChildren: ${JSON.stringify(experimentalReactChildren)},
experimentalDisableStreaming: ${JSON.stringify(experimentalDisableStreaming)}
}`,
};
}
Expand All @@ -49,15 +60,20 @@ function optionsPlugin(experimentalReactChildren: boolean): vite.Plugin {
}

function getViteConfiguration(
{ include, exclude, babel, experimentalReactChildren }: ReactIntegrationOptions = {},
{ include, exclude, babel, experimentalReactChildren, experimentalDisableStreaming }: ReactIntegrationOptions = {},
reactConfig: ReactVersionConfig,
) {
return {
optimizeDeps: {
include: [reactConfig.client],
exclude: [reactConfig.server],
},
plugins: [react({ include, exclude, babel }), optionsPlugin(!!experimentalReactChildren)],
plugins: [
react({ include, exclude, babel }),
optionsPlugin({
experimentalReactChildren: !!experimentalReactChildren, experimentalDisableStreaming: !!experimentalDisableStreaming
}),
],
ssr: {
noExternal: [
// These are all needed to get mui to work.
Expand All @@ -76,6 +92,7 @@ export default function ({
exclude,
babel,
experimentalReactChildren,
experimentalDisableStreaming,
}: ReactIntegrationOptions = {}): AstroIntegration {
const majorVersion = getReactMajorVersion();
if (isUnsupportedVersion(majorVersion)) {
Expand All @@ -90,7 +107,7 @@ export default function ({
addRenderer(getRenderer(versionConfig));
updateConfig({
vite: getViteConfiguration(
{ include, exclude, babel, experimentalReactChildren },
{ include, exclude, babel, experimentalReactChildren, experimentalDisableStreaming },
versionConfig,
),
});
Expand Down

0 comments on commit 3c90d8f

Please sign in to comment.