Server-side rendering utilities for emotion CSS-in-JS library that extract and inline critical CSS.
npx @tessl/cli install tessl/npm-emotion-server@10.0.0emotion-server provides server-side rendering utilities for the emotion CSS-in-JS library, enabling developers to extract and inline critical CSS for improved performance and SEO. It offers three main APIs for extracting critical CSS, rendering styles as strings, and streaming styles during server-side rendering.
npm install emotion emotion-serverimport { extractCritical, renderStylesToString, renderStylesToNodeStream } from 'emotion-server';For CommonJS:
const { extractCritical, renderStylesToString, renderStylesToNodeStream } = require('emotion-server');import React from 'react';
import { renderToString } from 'react-dom/server';
import { extractCritical, renderStylesToString } from 'emotion-server';
import { css } from 'emotion';
// Component with emotion styles
const StyledComponent = () => (
<div className={css`
color: blue;
font-size: 16px;
`}>
Hello World
</div>
);
// Extract critical CSS during server-side rendering
const { html, css: criticalCss, ids } = extractCritical(
renderToString(<StyledComponent />)
);
// Or render styles inline in HTML
const htmlWithStyles = renderStylesToString(
renderToString(<StyledComponent />)
);emotion-server works by leveraging emotion's cache system to identify and extract only the CSS that is actually used in rendered components. It uses regex pattern matching to find emotion class names in HTML and extracts corresponding styles from emotion's internal registry. The package serves as a lightweight wrapper around create-emotion-server, configured with emotion's default cache.
Extracts critical CSS from rendered HTML and returns detailed information about used styles.
/**
* Extracts critical CSS from rendered HTML string
* @param html - The rendered HTML string containing emotion class names
* @returns Object containing the original HTML, extracted CSS, and style IDs
*/
function extractCritical(html: string): EmotionCritical;
interface EmotionCritical {
/** The original HTML string */
html: string;
/** Array of emotion style IDs found in the HTML */
ids: Array<string>;
/** The critical CSS styles as a string */
css: string;
}Usage Example:
import React from 'react';
import { renderToString } from 'react-dom/server';
import { extractCritical } from 'emotion-server';
import { css } from 'emotion';
const MyComponent = () => (
<div className={css`background: red; padding: 10px;`}>
Content
</div>
);
const { html, css: criticalCss, ids } = extractCritical(
renderToString(<MyComponent />)
);
// Use the extracted CSS for hydration or performance optimization
console.log('Critical CSS:', criticalCss);
console.log('Style IDs for hydration:', ids);Renders emotion styles directly inline into HTML by injecting <style> tags with appropriate attributes.
/**
* Renders emotion styles inline into HTML string by injecting style tags
* @param html - The rendered HTML string containing emotion class names
* @returns HTML string with inlined style tags containing emotion CSS
*/
function renderStylesToString(html: string): string;Usage Example:
import React from 'react';
import { renderToString } from 'react-dom/server';
import { renderStylesToString } from 'emotion-server';
import { css } from 'emotion';
const MyComponent = () => (
<div className={css`color: blue; margin: 20px;`}>
Styled content
</div>
);
const htmlWithStyles = renderStylesToString(
renderToString(<MyComponent />)
);
// Send this HTML to the client - styles are already inline
res.send(`<!DOCTYPE html><html><body>${htmlWithStyles}</body></html>`);Creates a Node.js transform stream that automatically inlines emotion styles during streaming server-side rendering.
/// <reference types="node" />
/**
* Creates a Node.js transform stream that inlines emotion styles on-the-fly
* @returns A readable/writable stream that processes HTML and injects emotion styles
*/
function renderStylesToNodeStream(): NodeJS.ReadWriteStream;Usage Example:
import React from 'react';
import { renderToNodeStream } from 'react-dom/server';
import { renderStylesToNodeStream } from 'emotion-server';
import { css } from 'emotion';
const MyComponent = () => (
<div className={css`background: green; height: 100px;`}>
Streaming content
</div>
);
// Create the style injection stream
const styleStream = renderStylesToNodeStream();
// Pipe React's node stream through the emotion style stream
renderToNodeStream(<MyComponent />)
.pipe(styleStream)
.pipe(response);emotion-server functions expect properly rendered HTML strings containing emotion class names. Common issues include: