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.

Server Side Rendering and ReactJS.NET

See original GitHub issue

We’re currently working on a project which uses ReactJS.NET to render our React components server side, and then our client bundle takes over on the frontend once loaded.

Our styled components work perfectly client side, but we’re unable to get them working server side. All the examples we can see create a stylesheet using ServerStyleSheet and then render the results to html…but we can’t do this in the .NET environment. Is there another way?

We tried the following which has some success on the server, but when the client takes over we lose all styles (and we’re not actually sure why it’s working since we don’t run collectStyles on the sheet):

// ServerApp.js

const generateStyles = () => {
  const sheet = new ServerStyleSheet()
  return sheet.getStyleElement()
}

const ServerApp = (props) => {
  const styleTags = generateStyles()
  
  return (
    <Provider store={configureStore(props.initialState)}>
      <StaticRouter location={props.path} context={props.context}>
        {styleTags}
        <App />
      </StaticRouter>
    </Provider>
  )
}

The above only works when we include the following in our Babel config (but we lose styles when the js kicks in):

{
  "plugins": [
    ["babel-plugin-styled-components", {
      "ssr": true
    }]
  ]
}

Are there any examples of using Styled Components with ReactJS.NET server side, or it is not even possible without renderToHtml?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dustinsoftwarecommented, Dec 5, 2018

@daveaspinall we have implemented full support for server-rendered styles, check it out here:

https://reactjs.net/features/css-in-js.html

1reaction
daveaspinallcommented, Nov 6, 2017

@philpl okay, a quick update! We’ve managed to get our components working both server-side and client-side by forcing Styled Components to always generate the client-side style tag.

Essentially it was checking to see if the server-side <style> tag had been created, it found one in the DOM so didn’t create a new one, then the <style> tag inside the page content is removed so we’re left with no styles.

It’s hacky but we got it working by adding this on line 152 of src/models/BrowserStyleSheet.js:

tags = []
names = {}

Obviously it’s not optimal but it gets around our problem until #1102 gets resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-Side Rendering - ReactJS.NET
Server -side rendering allows you to pre-render the initial state of your React components server-side. This speeds up initial page loads as users...
Read more >
React Server-side Rendering with ReactJS.NET
Server -side rendering allows developers to pre-populate a web page using custom data directly on the server. In general, it is faster to...
Read more >
Server-Side Rendering React with .NET Core - Medium
To do server-side rendering what exactly we want is that send an html page which includes two things. First one is a markup...
Read more >
How to Server Side Rendering with ASP.NET Core and React ...
I'm developing an application with ASP.NET Core and React.js, my project was created with Visual Studio 2019, ( I have selected React.js project ......
Read more >
ASP.NET Core 3.1 with React - Server Side Rendering / Pre ...
Reactjs is working on the Server component to stream partially rendered UI in the form of JSON data to clientside.
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