@astrojs/react
Advanced tools
Comparing version 2.1.2 to 2.1.3
# @astrojs/react | ||
## 2.1.3 | ||
### Patch Changes | ||
- [#6976](https://github.com/withastro/astro/pull/6976) [`ca329bbca`](https://github.com/withastro/astro/commit/ca329bbcae7a6075af4f428f6f64466e9d152c8f) Thanks [@SudoCat](https://github.com/SudoCat)! - Prevent ID collisions in React.useId | ||
## 2.1.2 | ||
@@ -4,0 +10,0 @@ |
@@ -16,2 +16,5 @@ import { createElement, startTransition } from 'react'; | ||
if (!element.hasAttribute('ssr')) return; | ||
const renderOptions = { | ||
identifierPrefix: element.getAttribute('prefix'), | ||
}; | ||
for (const [key, value] of Object.entries(slotted)) { | ||
@@ -32,8 +35,8 @@ props[key] = createElement(StaticHtml, { value, name: key }); | ||
return startTransition(() => { | ||
createRoot(element).render(componentEl); | ||
createRoot(element, renderOptions).render(componentEl); | ||
}); | ||
} | ||
return startTransition(() => { | ||
hydrateRoot(element, componentEl); | ||
hydrateRoot(element, componentEl, renderOptions); | ||
}); | ||
}; |
{ | ||
"name": "@astrojs/react", | ||
"description": "Use React components within Astro", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"type": "module", | ||
@@ -40,3 +40,3 @@ "types": "./dist/index.d.ts", | ||
"react-dom": "^18.1.0", | ||
"astro": "2.3.3", | ||
"astro": "2.4.0", | ||
"astro-scripts": "0.0.14" | ||
@@ -43,0 +43,0 @@ }, |
import React from 'react'; | ||
import ReactDOM from 'react-dom/server'; | ||
import StaticHtml from './static-html.js'; | ||
import { incrementId } from './context.js'; | ||
@@ -61,2 +62,8 @@ const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); | ||
async function renderToStaticMarkup(Component, props, { default: children, ...slotted }, metadata) { | ||
let prefix; | ||
if (this && this.result) { | ||
prefix = incrementId(this.result); | ||
} | ||
const attrs = { prefix }; | ||
delete props['class']; | ||
@@ -78,20 +85,23 @@ const slots = {}; | ||
const vnode = React.createElement(Component, newProps); | ||
const renderOptions = { | ||
identifierPrefix: prefix, | ||
}; | ||
let html; | ||
if (metadata && metadata.hydrate) { | ||
if ('renderToReadableStream' in ReactDOM) { | ||
html = await renderToReadableStreamAsync(vnode); | ||
html = await renderToReadableStreamAsync(vnode, renderOptions); | ||
} else { | ||
html = await renderToPipeableStreamAsync(vnode); | ||
html = await renderToPipeableStreamAsync(vnode, renderOptions); | ||
} | ||
} else { | ||
if ('renderToReadableStream' in ReactDOM) { | ||
html = await renderToReadableStreamAsync(vnode); | ||
html = await renderToReadableStreamAsync(vnode, renderOptions); | ||
} else { | ||
html = await renderToStaticNodeStreamAsync(vnode); | ||
html = await renderToStaticNodeStreamAsync(vnode, renderOptions); | ||
} | ||
} | ||
return { html }; | ||
return { html, attrs }; | ||
} | ||
async function renderToPipeableStreamAsync(vnode) { | ||
async function renderToPipeableStreamAsync(vnode, options) { | ||
const Writable = await getNodeWritable(); | ||
@@ -102,2 +112,3 @@ let html = ''; | ||
let stream = ReactDOM.renderToPipeableStream(vnode, { | ||
...options, | ||
onError(err) { | ||
@@ -124,7 +135,7 @@ error = err; | ||
async function renderToStaticNodeStreamAsync(vnode) { | ||
async function renderToStaticNodeStreamAsync(vnode, options) { | ||
const Writable = await getNodeWritable(); | ||
let html = ''; | ||
return new Promise((resolve, reject) => { | ||
let stream = ReactDOM.renderToStaticNodeStream(vnode); | ||
let stream = ReactDOM.renderToStaticNodeStream(vnode, options); | ||
stream.on('error', (err) => { | ||
@@ -171,4 +182,4 @@ reject(err); | ||
async function renderToReadableStreamAsync(vnode) { | ||
return await readResult(await ReactDOM.renderToReadableStream(vnode)); | ||
async function renderToReadableStreamAsync(vnode, options) { | ||
return await readResult(await ReactDOM.renderToReadableStream(vnode, options)); | ||
} | ||
@@ -175,0 +186,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34766
16
518