ssr-emotion-react 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/dist/astro-integration.js +67 -67
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -158,6 +158,50 @@ import InteractiveBox from '../components/InteractiveBox'
|
|
|
158
158
|
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
## 🚦 Handling Asynchronous Logic (SSR vs. CSR)
|
|
162
|
+
|
|
163
|
+
Since this plugin uses synchronous `renderToString`, you cannot `throw Promise` (Suspense) during the server-side rendering phase.
|
|
164
|
+
|
|
165
|
+
If your component includes asynchronous logic (like `use()` or data fetching), use the Vite-standard `import.meta.env.SSR` flag to branch your code. This ensures the server-side render stays synchronous while the browser handles the dynamic parts.
|
|
166
|
+
|
|
167
|
+
### Example: SSR-Safe Data Loading
|
|
168
|
+
|
|
169
|
+
```jsx
|
|
170
|
+
import React from 'react'
|
|
171
|
+
import { css } from '@emotion/css'
|
|
172
|
+
|
|
173
|
+
export default props => {
|
|
174
|
+
|
|
175
|
+
// Return a skeleton or null during SSR to avoid suspending
|
|
176
|
+
if (import.meta.env.SSR) {
|
|
177
|
+
return (
|
|
178
|
+
<div className={css({ background: '#eee', height: '100px' })}>
|
|
179
|
+
Loading...
|
|
180
|
+
</div>
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Client-side only: use the full power of asynchronous resources
|
|
185
|
+
const data = React.use(myAsyncResource);
|
|
186
|
+
|
|
187
|
+
return (
|
|
188
|
+
<div className={css({ background: 'white' })}>
|
|
189
|
+
{data.message}
|
|
190
|
+
</div>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Why use `import.meta.env.SSR` ?
|
|
197
|
+
|
|
198
|
+
* **Zero Overhead:** Vite automatically removes the "server-only" or "client-only" code blocks during the build process (Dead Code Elimination).
|
|
199
|
+
|
|
200
|
+
* **Standard Way:** Since it's a built-in Vite feature (and Astro uses Vite), you don't need any special utility functions.
|
|
201
|
+
|
|
202
|
+
* **Predictable Styling:** It guarantees that your Emotion styles are extracted correctly without being interrupted by pending Promises.
|
|
203
|
+
|
|
204
|
+
|
|
161
205
|
## 🛠 The Patterns
|
|
162
206
|
|
|
163
207
|
We refer to reusable CSS logic as "The Patterns".
|