From 464e4bfeafb0fd2f73e697ab2ea8b5087b0e6813 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 24 Apr 2024 16:46:08 -0400 Subject: [PATCH] feat: updates to server render react 19 correctly --- packages/integrations/react/package.json | 4 ++-- packages/integrations/react/server.js | 2 +- packages/integrations/react/src/index.ts | 25 ++++++++---------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/integrations/react/package.json b/packages/integrations/react/package.json index 3fe01d49fb..894b8f9a5e 100644 --- a/packages/integrations/react/package.json +++ b/packages/integrations/react/package.json @@ -62,8 +62,8 @@ "peerDependencies": { "@types/react": "^17.0.50 || ^18.0.21", "@types/react-dom": "^17.0.17 || ^18.0.6", - "react": "^17.0.2 || ^18.0.0", - "react-dom": "^17.0.2 || ^18.0.0" + "react": "^17.0.2 || ^18.0.0 || ^19.0.0", + "react-dom": "^17.0.2 || ^18.0.0 || ^19.0.0" }, "engines": { "node": "^18.17.1 || ^20.3.0 || >=21.0.0" diff --git a/packages/integrations/react/server.js b/packages/integrations/react/server.js index b2e94fbf0b..47280c0f3e 100644 --- a/packages/integrations/react/server.js +++ b/packages/integrations/react/server.js @@ -1,6 +1,6 @@ import opts from 'astro:react:opts'; import React from 'react'; -import ReactDOM from 'react-dom/server'; +import ReactDOM from 'react-dom/server.browser'; import { incrementId } from './context.js'; import StaticHtml from './static-html.js'; diff --git a/packages/integrations/react/src/index.ts b/packages/integrations/react/src/index.ts index c6bf1a200f..f0b75da93a 100644 --- a/packages/integrations/react/src/index.ts +++ b/packages/integrations/react/src/index.ts @@ -10,17 +10,14 @@ export type ReactIntegrationOptions = Pick< experimentalReactChildren?: boolean; }; +const isLegacy = ReactVersion.startsWith('17.'); const FAST_REFRESH_PREAMBLE = react.preambleCode; function getRenderer() { return { name: '@astrojs/react', - clientEntrypoint: ReactVersion.startsWith('18.') - ? '@astrojs/react/client.js' - : '@astrojs/react/client-v17.js', - serverEntrypoint: ReactVersion.startsWith('18.') - ? '@astrojs/react/server.js' - : '@astrojs/react/server-v17.js', + clientEntrypoint: isLegacy ? '@astrojs/react/client-v17.js' : '@astrojs/react/client.js', + serverEntrypoint: isLegacy ? '@astrojs/react/server-v17.js' : '@astrojs/react/server.js', }; } @@ -55,28 +52,22 @@ function getViteConfiguration({ return { optimizeDeps: { include: [ - ReactVersion.startsWith('18.') - ? '@astrojs/react/client.js' - : '@astrojs/react/client-v17.js', + isLegacy ? '@astrojs/react/client-v17.js' : '@astrojs/react/client.js', 'react', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react-dom', ], - exclude: [ - ReactVersion.startsWith('18.') - ? '@astrojs/react/server.js' - : '@astrojs/react/server-v17.js', - ], + exclude: [isLegacy ? '@astrojs/react/server-v17.js' : '@astrojs/react/server.js'], }, plugins: [react({ include, exclude, babel }), optionsPlugin(!!experimentalReactChildren)], resolve: { dedupe: ['react', 'react-dom', 'react-dom/server'], }, ssr: { - external: ReactVersion.startsWith('18.') - ? ['react-dom/server', 'react-dom/client'] - : ['react-dom/server.js', 'react-dom/client.js'], + external: isLegacy + ? ['react-dom/server.js', 'react-dom/client.js'] + : ['react-dom/server', 'react-dom/client'], noExternal: [ // These are all needed to get mui to work. '@mui/material',