P
P
PrKassad2019-06-20 20:19:58
css
PrKassad, 2019-06-20 20:19:58

How to use SSR via react-dom/server with CSS Modules?

I'm making SSR using React and CSS Modules.
You need to do this without third-party solutions (like Next.js), but with pure react-dom/server.
At the moment there is the following application:

import styles from "./MyModule.module.scss";
...
<div className={styles.colorRed}>Test</div>
...

Initially, the project is built using react-scripts build (there is support for CSS modules out of the box).
And then there is the server that does the renderToStaticMarkup.
However, the renderToStaticMarkup method from react-dom/server returns: The fact is that className becomes null during server rendering, and should be 'MyModule_colorRed__XXXX'. How to make renderToStaticMarkup work correctly with CSS modules?
<div>Test</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PrKassad, 2019-07-02
@PrKassad

In general, I began to use Razzle . It is not practical to eject a configuration, or write an additional one for the sake of SSR.

A
Anton Spirin, 2019-06-20
@rockon404

You are doing something wrong:

import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import styles from './MyModule.module.css';

const html = renderToStaticMarkup(<div className={styles.colorRed}>Test</div>);

console.log(html); // <div class="src_MyModule_module__colorRed">Test</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question