Skip to content

SSR of styles from emotion css-in-js library #16

Closed as not planned
Closed as not planned
@ghost

Description

I'm trying to render the initial styles on the server to avoid CLS. Is there a way to do it currently?

ex:
https://emotion.sh/docs/ssr#renderstylestostring

This is what I'm trying but it obviously doesn't work,

import React from 'react'
import { MantineProvider } from '@mantine/core';
import { escapeInject } from 'vite-plugin-ssr'
import { renderStylesToNodeStream } from '@emotion/server'
import { renderToStream } from 'react-streaming/server'
import { PageLayout } from './PageLayout'

export async function render(pageContext) {
  const { Page, pageProps } = pageContext;
  const page = (
    <MantineProvider withGlobalStyles withNormalizeCSS>
      <PageLayout pathname={pageContext.urlPathname}>
        <Page {...pageProps} />
      </PageLayout>
    </MantineProvider>
  )
  const stream = await renderToStream(page, { disable: false })
  const newStream = stream.pipe(renderStylesToNodeStream())
  return escapeInject`<!DOCTYPE html>
    <html lang="en">
      <body>
        <div id="page-view">${newStream}</div>
      </body>
    </html>`
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions