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.

Serverside rendering / client rehydration does not work after there has been a redirect on the server.

See original GitHub issue

Serverside rendering / client rehydration does not work after there has been a redirect on the server.

Version

2.1.2 (not using babel-plugin-tyled-components)

Reproduction

I will try to explain this as well as I can, sorry if I am a bit vauge. It is non-trivial to provide a reproduction.

Using react-router I have a protected route, which requires the user to be logged in. If the user is not logged in, it gets reidrected to the login page. After logging in and navigating back to the protected route (so it hits the server), the wrong styles get applied. The only reason that I can see for this happening is that styled-components somehow caches the styles the first time the route is rendered and therefor reuses the wrong styles the second time. I have no idea if you are doing something like this though, it’s just a guess.

My styled-components related code on the server looks like this;


...
const component = (
    <Provider store={store}>
        <RouterContext {...props}/>
    </Provider>
)
    
const sheet   = new ServerStyleSheet()
const styles  = sheet.collectStyles(component) 
const content = renderToString(styles)
const css     = sheet.getStyleTags()
    
const html = renderToString(<Html store={store} content={content} css={css}/>)

res.status(200).send(`<!doctype html>${html}`)
...

Steps to reproduce

  1. Access the hidden route on the server (by URL) when you are not logged in
  2. Application redirects you to the login screen (this happens on the server)
  3. Login
  4. Access the hidden (now accessible) route on the server (by URL)

Expected Behavior

The same styles gets applied on both the server and on the client

Actual Behavior

The client and server has different styles for the same route

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
kittencommented, Oct 18, 2017

@GGAlanSmithee yes, different classes do hint at the Babel plugin’s ssr feature being needed 😬

0reactions
GGAlanSmitheecommented, Oct 20, 2017

@philpl

I followed your advise and added babel-plugin-styled-components and it seems like it did the trick! At least I haven’t been able reproduce the behavior yet.

For any future googler;

['babel-plugin-styled-components', {
    'ssr' : true,
    // ... other options
}]

Thanks for the quick and accurate help. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side rendering - Apollo GraphQL Docs
This will ensure the client-side React hydration works correctly by using ReactDOMServer. renderToString to generate the string.
Read more >
Client side state is not hydrated from server on page reload
Expected behavior. On page reload, upon hydration client should also get authenticated, and hence console would print logged in true.
Read more >
Client-Side and Server-Side Redirects in Next.js
For example, you might have an HOC that only renders the component when the user is authenticated and otherwise redirects to the login...
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
This is because the page doesn't know that the underlying data has changed. After the page is server-rendered, those props are immutable.
Read more >
Client-side vs. Server-side vs. Pre-rendering for Web Apps
But it can be a lot better, and it doesn't have to be difficult to make it that way. That's what we will...
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