Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rehydration #36

Open
PepijnSenders opened this issue Dec 7, 2016 · 3 comments
Open

Rehydration #36

PepijnSenders opened this issue Dec 7, 2016 · 3 comments

Comments

@PepijnSenders
Copy link

Hey guys, was wondering why you guys don't implement client rehydration like Aphrodite and Glamor do?

Isn't this a bit wasteful, since all the CSS is already generated?

@PepijnSenders
Copy link
Author

Also I created a small blog post on this topic: https://engineering.hellofresh.com/the-css-in-js-battle-89c34a7a83ea

@corysimmons
Copy link

Thanks for linking to these. I think this lib had a ton of potential, but needs hydration/vendor prefixes to be viable for my use case anyway. :(

@souporserious
Copy link

souporserious commented Aug 16, 2022

If anyone comes across this issue, I wrote a simple rehydration script if you're using a fork of cxs like I am:

export function hydrateCss() {
  if (typeof document === 'undefined') {
    return
  }

  const styleElement = document.getElementById(
    STYLE_ID
  ) as HTMLStyleElement

  if (styleElement === null) {
    return
  }

  const sheet = styleElement.sheet!
  const rules = sheet.cssRules
  const maxLength = rules.length - 1

  for (let index = maxLength; index >= 0; index--) {
    const rule = rules[index]
    const className = rule.cssText.split(' ')[0].slice(1)
    const declaration = rule.cssText.slice(
      rule.cssText.indexOf('{') + 2,
      rule.cssText.lastIndexOf('}') - 2
    )
    const [key, value] = declaration.split(': ')
    const cacheKey = hash(key + value)

    insert(rule.cssText)

    cache[cacheKey] = className

    sheet.deleteRule(index)
  }
}

It only handles simple rules right now, but I plan to expand it to handle nested selectors and media queries when I get a chance.

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

No branches or pull requests

3 participants