-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat(@astrojs/react): export renderer for easy loading #11234
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
902f89e
wip
ematipico 0be636f
feat(@astrojs/react): export `renderer` for easy loading
ematipico 688127b
restore change
ematipico 9c07baa
chore: address feedback
ematipico 262f761
revert changes
ematipico f5d08a7
revert changes to react integration
ematipico 3d87d08
update changeset
ematipico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ | ||
--- | ||
'@astrojs/react': minor | ||
--- | ||
|
||
Exposes a `renderer` object from `@astrojs/react/server.js` and `@astrojs/react/server17.js`. This is useful when using the Container API for on-demand pages: | ||
|
||
```ts | ||
import type {APIRoute, SSRLoadedRenderer} from "astro"; | ||
import { experimental_AstroContainer } from "astro/container"; | ||
import { renderer } from '@astrojs/react/server.js'; | ||
import Component from "../components/button.jsx" | ||
|
||
export const GET: APIRoute = async (ctx) => { | ||
const renderers: SSRLoadedRenderer[] = [renderer]; | ||
const container = await experimental_AstroContainer.create({ | ||
renderers | ||
}); | ||
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
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>; | ||
} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
import type {APIRoute, SSRLoadedRenderer} from "astro"; | ||
import { experimental_AstroContainer } from "astro/container"; | ||
import { renderer } from '@astrojs/react/server.js'; | ||
import Component from "../components/button.jsx" | ||
|
||
export const GET: APIRoute = async (ctx) => { | ||
const renderers: SSRLoadedRenderer[] = [renderer]; | ||
const container = await experimental_AstroContainer.create({ | ||
renderers | ||
}); | ||
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to create a
runtime.js
entry point, and re-export these values, but rollup has some issues with it. For the time being, this works