-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@astrojs/react): export renderer for easy loading (#11234)
* wip * feat(@astrojs/react): export `renderer` for easy loading * restore change * chore: address feedback * revert changes * revert changes to react integration * update changeset
- Loading branch information
Showing
14 changed files
with
213 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds a new function called `addServerRenderer` to the Container API. Use this function to manually store renderers inside the instance of your container. | ||
|
||
This new function should be preferred when using the Container API in environments like on-demand pages: | ||
|
||
```ts | ||
import type {APIRoute} from "astro"; | ||
import { experimental_AstroContainer } from "astro/container"; | ||
import reactRenderer from '@astrojs/react/server.js'; | ||
import vueRenderer from '@astrojs/vue/server.js'; | ||
import ReactComponent from "../components/button.jsx" | ||
import VueComponent from "../components/button.vue" | ||
|
||
export const GET: APIRoute = async (ctx) => { | ||
const container = await experimental_AstroContainer.create(); | ||
container.addServerRenderer("@astrojs/react", reactRenderer); | ||
container.addServerRenderer("@astrojs/vue", vueRenderer); | ||
const vueComponent = await container.renderToString(VueComponent) | ||
return await container.renderToResponse(Component); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/container-react/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import react from '@astrojs/react'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [react()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@test/react-container", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@astrojs/react": "workspace:*", | ||
"astro": "workspace:*", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/container-react/src/components/button.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
export default () => { | ||
return <button id="arrow-fn-component">I am a react button</button>; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/container-react/src/pages/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type {APIRoute, SSRLoadedRenderer} from "astro"; | ||
import { experimental_AstroContainer } from "astro/container"; | ||
import server from '@astrojs/react/server.js'; | ||
import Component from "../components/button.jsx" | ||
|
||
export const GET: APIRoute = async (ctx) => { | ||
const container = await experimental_AstroContainer.create(); | ||
container.addServerRenderer("@astrojs/react", server); | ||
return await container.renderToResponse(Component); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,3 +230,4 @@ export default { | |
renderToStaticMarkup, | ||
supportsAstroStaticSlot: true, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { version as ReactVersion } from 'react-dom'; | ||
|
||
export type SupportedReactVersion = keyof typeof versionsConfig; | ||
export type ReactVersionConfig = (typeof versionsConfig)[SupportedReactVersion]; | ||
|
||
export function getReactMajorVersion(): number { | ||
const matches = /\d+\./.exec(ReactVersion); | ||
if (!matches) { | ||
return NaN; | ||
} | ||
return Number(matches[0]); | ||
} | ||
|
||
export function isUnsupportedVersion(majorVersion: number) { | ||
return majorVersion < 17 || majorVersion > 19 || Number.isNaN(majorVersion); | ||
} | ||
|
||
export const versionsConfig = { | ||
17: { | ||
server: '@astrojs/react/server-v17.js', | ||
client: '@astrojs/react/client-v17.js', | ||
externals: ['react-dom/server.js', 'react-dom/client.js'], | ||
}, | ||
18: { | ||
server: '@astrojs/react/server.js', | ||
client: '@astrojs/react/client.js', | ||
externals: ['react-dom/server', 'react-dom/client'], | ||
}, | ||
19: { | ||
server: '@astrojs/react/server.js', | ||
client: '@astrojs/react/client.js', | ||
externals: ['react-dom/server', 'react-dom/client'], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
"include": ["src"], | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
} | ||
}, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.