question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[V2] Using SSR API on client side

See original GitHub issue

I still can’t find a way to extract styles from client side with SC2. With SC1, I did it this way:

[render static component...]
let cssRules = StyleSheet.rules().map(rule => rule.cssText).join('\n');

When upgrading to SC2, I switch to the new SSR API:

let sheet = new ServerStyleSheet();
[render static component...]
let cssRules = sheet.getStyleTags();

But since I’m on client side, I get the “BrowserTag cannot be cloned!” error when trying to create the ServerStyleSheet object (see #811).

Digging the source code, I find this way to do it:

[render static component...]
let cssRules = StyleSheet.instance.toHTML();

But it generates several <styles> tags, some with data-styled-components attributes, some comments…not a very clean stylesheet.

What is the proper way to extract SC <styles> from the client side, with or without SSR API ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
PlanetIratacommented, Jun 9, 2017

I’m using React to build a static page editor/generator, so I need at some point to extract the HTML (with React renderStatic…) and the styles (with SC). At this stage I do everything on the client side. In the end, I may split the app with a server, or not: I may embed the app in an Electron like wrapper, so I’ll still need to extract the style from the client side.

4reactions
Igorbekcommented, Aug 1, 2017

Sure, it is pretty simple:

import { create } from 'react-test-renderer';
import { ServerStyleSheet } from 'styled-components';
import StyleSheet from 'styled-components/lib/models/StyleSheet';
import * as css from 'css';

StyleSheet.reset(true); // reset to use server stylesheet

export function testComponent(element) {
    const sheet = new ServerStyleSheet();
    const output = create(sheet.collectStyles(element));

    expect(output).toMatchSnapshot(); // take snapshot of output

    // prettify css output slightly
    const cssStyles = sheet.getStyleElement()
        .map(el => el.props.dangerouslySetInnerHTML.__html)
        .join();
    const styles = css.stringify(css.parse(cssStyles));

    expect(styles).toMatchSnapshot(); // take a snapshot of produced css
}

// somewhere in the test:
import { testComponent } from ...

it('renders', () => {
    testComponent(<MyStyledComponentToTest ... />);
});

In fact, I have my own snapshot serializer that produces nice snapshots of input element, its rendered output and captured css.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client-side vs. Server-side vs. Pre-rendering for Web Apps
SSR is used to fetch data and pre-populate a page with custom content, leveraging the server's reliable internet connection. That is, the server's...
Read more >
Server-side rendering - Apollo GraphQL Docs
Server -side rendering (SSR) is a performance optimization for modern web apps. It enables you to render your app's initial state to raw...
Read more >
Server Side Rendering (SSR) vs. Client Side Rendering (CSR ...
Client Side Rendering means generating the HTML components on the browser side, by executing Javascript code within the browser that manipulates the HTML ......
Read more >
Server-Side Rendering (SSR) - Vue.js
Code Structure # · Serve client files by adding server.use(express.static('.')) in server.js . · Load the client entry by adding <script type="module" src="/ ......
Read more >
How to stop 2 times data fetch in SSR (first on server side and ...
Now issue is that after fetching data on server, client is again calling fetchRequestQuery(dispatch) in useEffect. so it is calling same API 2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found