iso-morphic-style-loader-jeseeq
v0.0.2
Published
isomorphic style loader module for webpack
Downloads
7
Readme
npm install iso-morphic-style-loader --save-dev
In fact, there is nothing different with the style-loader, just use the same config as you like.
However, some more work if you want to add critical path CSS with isomorphic app:
/// Such as server.js, where you render your isomorphic app and will send it
/// back to your user.
data.styles = []
// iso-morphic-style-loader will export global.__universal__ and you
// can access it to get styles.
if (global.__universal__) {
global.__universal__.forEach(v => {
data.styles.push({
...v.attrs,
id: v.id,
cssText: v.content.join('\n')
})
})
}
// Then we will pass this styles to your React Component.
const html = ReactDOM.renderToStaticMarkup(<Component {...data} />)
res.status(route.status || 200)
res.send(`<!doctype html>${html}`)
///////////
// Here maybe your component.js
// Perfect, we can insert styles easily.
render() {
return (
{styles.map(({id, cssText, ...rest}) =>
<style
{...rest}
key={id}
id={id}
dangerouslySetInnerHTML={{ __html: cssText }}
/>
)}
)
}
Features
For server side, the lib will export
__universal__
toglobal
, and you can access it to get styles.__universal__.forEach(v => console.log(v.id, v.attrs, v.content))
__universal__.map(v => console.log(v.id, v.attrs, v.content))
Nothing will happens if you ignore
__universal__
, no errors in server side rendering, and works the same asstyle-loader
do.But if you want to optimize for critical path CSS rendering, please inject styles during server side rendering.
The browser side behaviour is exactly the same as
[email protected]
. And you can enjoy all features supported by[email protected]
.No FOUC, no duplicate!
- The script will try to remove the styles injected at server side to prevent duplicate.
- However it only remove after client side styles created, so no FOUC.
Demo
Left is with style-loader
and right is with iso-morphic-style-loader
.