reactjs / React.NET

.NET library for JSX compilation and server-side rendering of React components
https://reactjs.net/
MIT License
2.28k stars 940 forks source link

Server side rendering different class names on routes than on main page, breaking StyledComponents #1281

Open SteveReece opened 2 years ago

SteveReece commented 2 years ago

Thanks for filing a bug! To save time, if you're having trouble using the library, please check off the items you have tried. If you are just asking a question, skip right to the bottom.

Please verify these steps before filing an issue, and check them off as you go

I'm using these library versions:

Runtime environment:

Steps to reproduce

Export a style from a .js file

export const BlogDisplayStyle = styled.div
  background-color: rgba(255,255,255,0.8);

  pre {
    margin-bottom: 0rem;
    background-color: black;
    color: white;
  }

  p {
    background-color: rgba(0,0,0,0) !important;
    font-size: 1.75em !important;
  }
...

Import the style: import { BlogDisplayStyle } from './Styles'

...
<BlogDisplayStyle>
  <Container fluid>
...

Map your routes in the main app.

<Route path='/blog/:id?' render={() => <Blog {...props} />} />
<Route exact path='/' render={() => <Home {...props} />} />

Run the app at / and then click a link to /blog/:id. Observe that the rendered output looks correct and has the style in the and that it's class name matches the named div in the rendered HTML.

/* sc-component-id: sc-EHOje */
.iBfTWj{background-color:rgba(255,255,255,0.8);} .iBfTWj pre{margin-bottom:0rem;background-color:black;color:white;} .iBfTWj p{background-color:rgba(0,0,0,0) !important;font-size:1.75em !important;} .iBfTWj span{background-color:rgba(0,0,0,0) !important;font-size:1.75em !important;} .iBfTWj h1{font-size:4em !important;} @media (min-width:768px){.iBfTWj h1{font-size:2.75em !important;}.iBfTWj p{font-size:1.1em !important;}.iBfTWj span{font-size:1.1em !important;}}
...
<div class="sc-EHOje iBfTWj">

Now, refresh the page at the route in the browser and observe that the styles are no longer applied and the

class has changed.

/* sc-component-id: sc-EHOje */
.iBfTWj{background-color:rgba(255,255,255,0.8);} .iBfTWj pre{margin-bottom:0rem;background-color:black;color:white;} .iBfTWj p{background-color:rgba(0,0,0,0) !important;font-size:1.75em !important;} .iBfTWj span{background-color:rgba(0,0,0,0) !important;font-size:1.75em !important;} .iBfTWj h1{font-size:4em !important;} @media (min-width:768px){.iBfTWj h1{font-size:2.75em !important;}.iBfTWj p{font-size:1.1em !important;}.iBfTWj span{font-size:1.1em !important;}}
/* sc-component-id: sc-bxivhb */
.dtehRz{font-size:2em;color:#ccc;}.dtehRz{font-size:2em;color:#ccc;}
...
<div class="sc-bxivhb dtehRz">

Notice how the original style is still there, but the server is now rendering the div with a different class name and style.

This behavior only occurs on a route, not at /, as the root renders correctly both server side and client side.


ReactJS.Net appears to be incorrectly rendering styles when the render happens on a route. I have created a sample app here. Sample App that duplicates this behvior.

Thank you, Steve