Skip to content

Instantly share code, notes, and snippets.

@lxsmnsyc
Created March 3, 2020 09:06
Show Gist options
  • Save lxsmnsyc/72628beafe37dc8ac76749709d674969 to your computer and use it in GitHub Desktop.
Save lxsmnsyc/72628beafe37dc8ac76749709d674969 to your computer and use it in GitHub Desktop.
Styletron SSR Setup wtih TypeScript
import { Client, Server } from 'styletron-engine-atomic';
import { DebugEngine } from 'styletron-react';
function getHydratedClass(): HTMLStyleElement[] {
const els = document.getElementsByClassName('_styletron_hydrate_');
const newEls = [];
for (let i = 0; i < els.length; i += 1) {
const el: Element = els[i];
if (el instanceof HTMLStyleElement) {
newEls.push(el);
}
}
return newEls;
}
export const styletron = (
(typeof window === 'undefined')
? new Server()
: new Client({
hydrate: getHydratedClass(),
})
);
export const debug = (
process.env.NODE_ENV !== 'production' ? new DebugEngine() : undefined
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment