Skip to content

Fix undefined sheet error when SSR without rendering any styles - #391

Closed
kristojorg wants to merge 2 commits into
styled-components:masterfrom
kristojorg:master
Closed

kristojorg wants to merge 2 commits into
styled-components:masterfrom
kristojorg:master

Conversation

@kristojorg

Copy link
Copy Markdown
Contributor

When SSR without any styles, StyleSheet.sheet returned by glamor is
undefined, and thus the cssRules property is undefined. I have
substituted this with an empty array instead, and it is now working in
my case. This should solve one of the issues mentioned multiple times in
#124.

Side note: this is my first ever PR anywhere... please let me know if I'm doing something dumb

When SSR without any styles, StyleSheet.sheet returned by glamor is
undefined, and thus the cssRules property is undefined. I have
substituted this with an empty array instead, and it is now working in
my case. This should solve the issue mentioned multiple times in
[#124](#124
).
@kristojorg kristojorg mentioned this pull request Jan 20, 2017
@mxstbr

mxstbr commented Jan 20, 2017

Copy link
Copy Markdown
Member

Hey @kristojorg, this is great, thanks for taking the time to submit a PR!

We just rebuilt the entire SSR system in #214, which was merged into the v2 branch. Would you mind making a similar change to that branch? Also it'd be great if we could circumvent this issue from our source, not the vendored dependencies. (i.e. not src/vendor/) Changing a vendored dependency makes updating it very hard in the future, as we have to remember what happened to it.

Thanks so much again!

@kristojorg

Copy link
Copy Markdown
Contributor Author

Cool, I just submitted a new PR to the v2 branch with a fix :-) ! Hope it helps

@rnnyrk

rnnyrk commented Jan 24, 2017

Copy link
Copy Markdown

How to install the v2 branch? I tried npm i [email protected] but this didn't work. And is there some clear documentation on how to SSR styled components?

I've tried using https://github.com/styled-components/babel-plugin-styled-components but my app crashed after installing it. Any heads up?

@mxstbr

mxstbr commented Jan 24, 2017

Copy link
Copy Markdown
Member

npm install --save styled-components@next, then use the babel plugin. It's not documented yet as it's not released yet, we're still working out the kinks.

@rnnyrk

rnnyrk commented Jan 24, 2017

Copy link
Copy Markdown

Thanks for your reply. Before using this I will wait until v2 is stable, because literally none of my styles work after updating (although the babel plugin error did disappear haha).

@mxstbr

mxstbr commented Jan 24, 2017

Copy link
Copy Markdown
Member

Oh oops 🙈 are you server-side rendering?

@rnnyrk

rnnyrk commented Jan 24, 2017

Copy link
Copy Markdown

Not yet. Is that required with v2? Still need to use the flush etc, or is the getCSS() as stated in #124 possible?

@mxstbr

mxstbr commented Jan 24, 2017

Copy link
Copy Markdown
Member

Not required with v2 at all, getCSS works perfectly fine see #214. You're not using SSR and no styles show up?

@rnnyrk

rnnyrk commented Jan 24, 2017 •

Copy link
Copy Markdown

Ok thanks, will try the example shown in 214.

Yes indeed, no serverside rendering atm. For instance my Header.jsx as shown below work perfectly fine with styled-components 1.3.1, but after updating to with styled-components@next no styles are working anymore.

import React, { Component } from 'react';
import { Link } from 'react-router';
import styled from 'styled-components';

class Header extends Component {
  render() {
    return (
      <HeaderElem>
          <NavElem>
            <NavLink to="/">
              <span>Terug</span>
            </NavLink>

            <NavTitle>Title of my page</NavTitle>
          </NavElem>
      </HeaderElem>
    );
  }
}

const HeaderElem = styled.header`
  position: fixed;
  top: 0;
  left: 0;
`;

const NavElem = styled.nav`
  display: flex;
  position: relative;
  width: 100vw;
  padding: 21px 20px 16px;
  color: #fff;
  font-size: 18px;
  text-align: center;
  background: #38659e;
`;

const NavLink = styled(Link)`
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translate(0, -50%);
  color: #fff;
  text-decoration: none;
`;

const NavTitle = styled.div`
  flex-grow: 2;
`;

export default Header;

So for now I downgraded to 1.3.1 again ;)

@kristojorg

Copy link
Copy Markdown
Contributor Author

Did you build the v2 branch after npm install? To use the v2 branch I had to :

cd node_modules/styled-components

then

yarn build or npm build

After that it worked fine

@mxstbr

mxstbr commented Jan 24, 2017

Copy link
Copy Markdown
Member

Ohh that's my mistake when I published that then @kristojorg! 🙈 will fix that with the next pre-release!

@rnnyrk

rnnyrk commented Jan 25, 2017 •

Copy link
Copy Markdown

Thanks for pointing that out, although building doesn't seem to fix my problem.
As you can see in my example I defined all my styled outside my React component. I'd check the <head> of my webpage through the inspector and I saw a lot of styles, many of them empty:

<style type="text/css">.sc-bdVaJa {}.sc-bwzfXH {}.sc-htpNat {}.sc-bxivhb {}.sc-ifAKCX {}.sc-EHOje {}.sc-bZQynM {}.sc-gzVnrw {}.sc-htoDjs {}.sc-dnqmqq {}@font-face {
    font-family: 'Aller';
    font-weight: 500;
    @font-face {
        font-family: 'Aller';
        font-weight: 200;
        @font-face {
            font-family: 'Aller';
            font-weight: 800;
            html {
                text-rendering: optimizeLegibility;
                box-sizing: border-box;
            }
            *,
            *:after,
            *:before {
                box-sizing: inherit;
                margin: 0;
                padding: 0;
            }
            .sc-iwsKbI {}.leMwTI {
                position: relative;
                width: 100%;
                min-height: 100vh;
                background-image: url('/achtergrond.png?f196c');
                background-size: cover;
            }
            .sc-gZMcBi {}.dOVyUJ {
                position: fixed;
                top: 0;
                left: 0;
            }
            .sc-gqjmRU {}.sc-VigVT {}.sc-jTzLTM {}.sc-fjdhpX {}.haSjo {
                position: absolute;
                top: 124px;
                left: 50%;
                width: 330px;
                -webkit-transform: translate(-50.4%, 0);
                -ms-transform: translate(-50.4%, 0);
                transform: translate(-50.4%, 0);
            }
            .sc-jzJRlG {}.gyjQZj {
                list-style: none;
            }
            .sc-cSHVUG {}.cjEito {
                border-bottom: 2px solid #fff;
                background: #38659e;
            }
            .cjEito:last-of-type {
                border-bottom: 0;
            }
            .sc-chPdSV {}.ieEmoF {
                position: absolute;
                bottom: 54px;
                left: 0;
                width: 100%;
            }
            .sc-kgoBCf {}.sc-kGXeez {}.gVQFWk {
                position: absolute;
                bottom: 0;
                width: 175px;
                height: 170px;
                text-align: center;
                text-decoration: none;
                background: no-repeat bottom center/40%;
                -webkit-tap-highlight-color: rgba(255, 255, 255, 0);
                left: 100px;
            }
            .gVQFWk:active {
                background-position: center 70px;
            }
            @-webkit-keyframes kuBrLH {
                from {
                    left: -1000px
                }
                to {
                    left: -470px
                }
            }</style>

So somehow I thought moving the styled elements would work. And this worked. The styles where added, but my global styles (body styling and font-face) didn't load.
I'll imported the globalSyles by adding import './assets/globalStyle'; at the top of my main component App.jsx.

I prettified my code above, but it looks like it isn't loading the in correct order. Also they're many empty classes which seems not like it use to be.

I downgraded to 1.3.1 again and moved most of my styles back outside the component. I hope styles outside the component will still be allowed in v2 since it looks a lot clearer to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants