Skip to content

Commit

Permalink
doc: wmr example with prerender
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Jan 2, 2021
1 parent 9d17c79 commit bee862e
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions docs/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,20 @@ async function ssr() {
> The [tw-in-js/example-wmr](https://github.com/tw-in-js/example-wmr) repository uses this setup.
```js
/* pages/twind.config.js */
/* public/twind.config.js */
export default {
/* Shared config */
}
```

```js
/* pages/index.js */
/* public/index.js */
import hydrate from 'preact-iso/hydrate'

import { setup } from 'twind'
// Or if you are using twind/shim
// import { setup } from 'twind/shim'

import twindConfig from './twind.config'

if (typeof window !== 'undefined') {
Expand All @@ -278,39 +281,42 @@ export function App() {
hydrate(<App />)

export async function prerender(data) {
const { default: prerender } = await import('preact-iso/prerender')

const { sheet, getStyleTagProperties } = await import('./twind.prerender')

sheet.reset()

const result = await prerender(<App {...data} />)

const { id, textContent } = getStyleTagProperties(sheet)

result.html = `<style id="${id}">${textContent}</style>${result.html}`
const { default: prerender } = await import('./prerender')

return result
return prerender(<App {...data} />)
// Or if you are using twind/shim
// return prerender(<App {...data} />, { shim: true })
}
```

```js
/* pages/twind.prerender.js */
import { setup } from 'twind'
/* public/prerender.js */
import prerender from 'preact-iso/prerender'

// twind/server has currently only a CJS bundle
// which available as the default export
import twind from 'twind/server'
import { setup } from 'twind'
import { asyncVirtualSheet, getStyleTagProperties, shim } from 'twind/server'

import twindConfig from './twind.config'

const { asyncVirtualSheet, getStyleTagProperties } = twind

const sheet = asyncVirtualSheet()

setup({ ...twindConfig, sheet })

export { sheet, getStyleTagProperties }
export default async (app, options = {}) => {
sheet.reset()

const result = await prerender(app)

if (options.shim) {
result.html = shim(result.html)
}

const { id, textContent } = getStyleTagProperties(sheet)

result.html = `<style id="${id}">${textContent}</style>${result.html}`

return result
}
```

## Next.js
Expand Down

0 comments on commit bee862e

Please sign in to comment.