Skip to content

Commit

Permalink
chore: update SSR docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Dec 15, 2023
1 parent ae4eccd commit 861e409
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions docs/src/content/docs/reference/server-side-rendering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,21 @@ import Seo from '../../../components/Seo.astro'

Unistyles can render styles on the server-side, which is useful, for example, for SEO purposes.

### Intiial configuration

Configure your `unistyles.ts` file as it's listed in the [Getting started](/start/setup) section.

### Server side configuration

You just need to modify the `__document.ts` or `__document.js` file in your project root.
You just need to modify the `__document.ts` or `__document.js` file in your project's root by importing
`unistyles.ts` file.

```diff lang="tsx"
+ import '../unistyles'

export const getInitialProps = async ({ renderPage }) => {
AppRegistry.registerComponent('Main', () => Main)

+ UnistylesRegistry
+ .addBreakpoints({
+ xs: 0,
+ sm: 300,
+ md: 500,
+ lg: 800,
+ xl: 1200
+ })
+ .addThemes({
+ light: theme
+ })

const { getStyleElement } = AppRegistry.getApplication('Main')
const page = await renderPage()
const styles = [
Expand All @@ -46,14 +41,23 @@ export const getInitialProps = async ({ renderPage }) => {

### Client side configuration

You need to import a file where you configured Unistyles with `UnistylesRegistry`.
To setup the listeners and Unistyles bindings, simply repeat the process,
but this time with `_app.tsx` file.

```diff lang="tsx"
+ import '../unistyles'

export const MyApp = ({ Component, pageProps }: SolitoAppProps) => {
// your component
}
```

Some dynamic features like breakpoint are not available on the server. That's why you need additional configuration with the `isClient` flag.

```tsx /import './styles'/ /isClient/
import React, { useEffect, useState } from 'react'
import { View, Text, ActivityIndicator } from 'react-native'
import { useStyles } from 'react-native-unistyles'
import './styles'

export const HomeScreen = () => {
const { styles, breakpoint, theme } = useStyles(stylesheet)
Expand Down Expand Up @@ -92,6 +96,10 @@ export const HomeScreen = () => {
const stylesheet = ...
```

:::tip
If you are struggling with the working example, you can check out the [example repository](https://github.com/jpudysz/react-native-unistyles-ssr-exmaple).
:::

:::caution
SSR support is still experimental and may not work in all cases.
It still requires JS to compute your styles.
Expand Down

0 comments on commit 861e409

Please sign in to comment.