Skip to content

Commit aa71797

Browse files
ssr: add docs for new SSR support (#1510)
* ssr: add docs for new SSR support * minor tweaks * add comment to assign properties dynamically
1 parent 33d1097 commit aa71797

File tree

13 files changed

+356
-200
lines changed

13 files changed

+356
-200
lines changed

docs/framework-integration/react.md

-64
Original file line numberDiff line numberDiff line change
@@ -393,70 +393,6 @@ function App() {
393393
export default App;
394394
```
395395

396-
### Enable Server Side Rendering (SSR)
397-
398-
If your React framework supports server side rendering, e.g. [Next.js](https://nextjs.org/) your Stencil components will get automatically server side rendered, if set up correctly. In order to enable this:
399-
400-
1. Add a `dist-hydrate-script` output target to your `stencil.config.ts` if not already existing, e.g.:
401-
```ts title="stencil.config.ts"
402-
import { Config } from '@stencil/core';
403-
404-
export const config: Config = {
405-
outputTargets: [
406-
{
407-
type: 'dist-hydrate-script',
408-
dir: './hydrate',
409-
},
410-
// ...
411-
]
412-
};
413-
```
414-
415-
2. Create an export for the compiled files within the `/hydrate` directory, e.g.
416-
```json title="package.json"
417-
{
418-
"name": "component-library",
419-
...
420-
"exports": {
421-
...
422-
"./hydrate": {
423-
"types": "./hydrate/index.d.ts",
424-
"import": "./hydrate/index.js",
425-
"require": "./hydrate/index.cjs.js",
426-
"default": "./hydrate/index.js"
427-
},
428-
...
429-
},
430-
...
431-
}
432-
```
433-
434-
3. Set the `hydrateModule` in your React output target configuration, e.g.
435-
```ts title="stencil.config.ts"
436-
import { Config } from '@stencil/core';
437-
import { reactOutputTarget } from '@stencil/react-output-target';
438-
439-
export const config: Config = {
440-
outputTargets: [
441-
reactOutputTarget({
442-
outDir: '../react-library/lib/components/stencil-generated/',
443-
hydrateModule: 'component-library/hydrate'
444-
}),
445-
// ...
446-
]
447-
};
448-
```
449-
450-
That's it! Your Next.js application should now render a Declarative Shadow DOM on the server side which will get automatically hydrated once the React runtime initiates.
451-
452-
#### SSR Limitations
453-
454-
Please be aware of the following limitations when working with server side rendered applications:
455-
456-
- A Declarative Shadow DOM not only encapsulates the HTML structure of a component but also includes all associated CSS. When server-side rendering numerous small components with extensive CSS, the overall document size can significantly increase, leading to longer initial page load times. To optimize performance, it's essential to maintain a manageable document size that aligns with your performance objectives. It is advisable to server-side render only the critical components required for rendering the initial viewport, while deferring the loading of additional components until after the initial render.
457-
- A Stencil component that dynamically renders content based on elements within its Light DOM may not render correctly as the Output Target can't always serialize the children of the React component into a template string especially when working with multiple nested Stencil components.
458-
- Currently Stencil does not support the serialization of objects within properties. This means that Stencil ignores server side rendering all properties that are defined with a reference types, e.g. `object`.
459-
460396
## API
461397

462398
### esModule

docs/framework-integration/vue.md

-67
Original file line numberDiff line numberDiff line change
@@ -348,73 +348,6 @@ In your page or component, you can now import and use your component wrappers:
348348
</template>
349349
```
350350

351-
### Enable Server Side Rendering (SSR)
352-
353-
If your Vue framework supports server side rendering, e.g. when using [Nuxt](https://nuxt.com/) your Stencil components will get automatically server side rendered, if set up correctly. In order to enable this:
354-
355-
1. Add a `dist-hydrate-script` output target to your `stencil.config.ts` if not already existing, e.g.:
356-
```ts title="stencil.config.ts"
357-
import { Config } from '@stencil/core';
358-
359-
export const config: Config = {
360-
outputTargets: [
361-
{
362-
type: 'dist-hydrate-script',
363-
dir: './hydrate',
364-
},
365-
// ...
366-
]
367-
};
368-
```
369-
370-
2. Create an export for the compiled files within the `/hydrate` directory, e.g.
371-
```json title="package.json"
372-
{
373-
"name": "component-library",
374-
...
375-
"exports": {
376-
...
377-
"./hydrate": {
378-
"types": "./hydrate/index.d.ts",
379-
"import": "./hydrate/index.js",
380-
"require": "./hydrate/index.cjs.js",
381-
"default": "./hydrate/index.js"
382-
},
383-
...
384-
},
385-
...
386-
}
387-
```
388-
389-
3. Set the `hydrateModule` in your Vue output target configuration, e.g.
390-
```ts title="stencil.config.ts"
391-
import { Config } from '@stencil/core';
392-
import { vueOutputTarget } from '@stencil/vue-output-target';
393-
394-
export const config: Config = {
395-
outputTargets: [
396-
vueOutputTarget({
397-
includeImportCustomElements: true,
398-
includePolyfills: false,
399-
includeDefineCustomElements: false,
400-
componentCorePackage: 'component-library',
401-
hydrateModule: 'component-library/hydrate',
402-
proxiesFile: '../component-library-vue/src/index.ts',
403-
}),
404-
// ...
405-
]
406-
};
407-
```
408-
409-
That's it! Your Nuxt application should now render a Declarative Shadow DOM on the server side which will get automatically hydrated once the Vue runtime initiates.
410-
411-
#### SSR Limitations
412-
413-
Please be aware of the following limitations when working with server side rendered applications:
414-
415-
- A Declarative Shadow DOM not only encapsulates the HTML structure of a component but also includes all associated CSS. When server-side rendering numerous small components with extensive CSS, the overall document size can significantly increase, leading to longer initial page load times. To optimize performance, it's essential to maintain a manageable document size that aligns with your performance objectives. It is advisable to server-side render only the critical components required for rendering the initial viewport, while deferring the loading of additional components until after the initial render.
416-
- Currently Stencil does not support the serialization of objects within properties. This means that Stencil ignores server side rendering all properties that are defined with a reference types, e.g. `object`.
417-
418351
## API
419352

420353
### componentCorePackage

docs/guides/hydrate-app.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ If set to `true` it removes any abundant HTML comments. Stencil still requires t
190190

191191
__Type:__ `(document: Document, url: URL) => <void> | Promise<void>`
192192

193-
Allows to modify the document and all its containing components to be modified before the hydration process starts.
193+
Allows to modify the document and all its containing components to be modified before the hydration process starts. This allows e.g. to assign properties to the components dynamically:
194+
195+
```ts
196+
await renderToString(response.body, {
197+
beforeHydrate: (doc: Document) => {
198+
doc.querySelector(`my-component`).someComplexThing = new Map(...)
199+
},
200+
})
201+
```
194202

195203
##### `afterHydrate`
196204

0 commit comments

Comments
 (0)