Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
isomorphic-style-loader--react-context
Advanced tools
CSS style loader for Webpack optimized for critical path CSS rendering and isomoprhic web apps
This is a forked version of Kriasoft's isomorphic-style-loader
that works with new React.Context API. Until the PR will be merged I'll maintain this package. To use this, run
$ npm install isomorphic-style-loader--react-context
Then put <InsertCssProvider>
on top of your app, with insertCss
function as usual.
import {InsertCssProvider} from 'isomorphic-style-loader/lib/withStyles';
// ...
<InsertCssProvider value={insertCssFn}> // <--
{props.children}
</InsertCssProvider>
CSS style loader for Webpack that works similarly to style-loader,
but is optimized for critical path CSS
rendering and also works great in the context of isomorphic apps.
It provides two helper methods on to the styles
object - ._insertCss()
(injects CSS into the
DOM) and ._getCss()
(returns a CSS string).
See getting started | changelog | Join #react-starter-kit chat room on Gitter to stay up to date
$ npm install isomorphic-style-loader --save-dev
module.exports = {
/* ... */
module: {
rules: [
{
test: /\.css$/,
use: [
'isomorphic-style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
'postcss-loader'
]
}
]
}
/* ... */
};
Note: Configuration is the same for both client-side and server-side bundles. For more information visit https://webpack.js.org/configuration/module/.
// MyComponent.scss
.root { padding: 10px; }
.title { color: red; }
// MyComponent.js
import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './MyComponent.scss';
function MyComponent(props) {
return (
<div className={s.root}>
<h1 className={s.title}>Hello, world!</h1>
</div>
);
}
export default withStyles(s)(MyComponent); // <--
P.S.: It works great with CSS Modules!
Just decorate your React component with the withStyles
higher-order component, and pass a insertCss
function to your React app via <InsertCssProvider value={insertCss}>
context provider (see React's context API)
that either calls styles._insertCss()
on a client or styles._getCss()
on the server.
The context provider should appear in upper position in a component tree.
// MyRootComponent.js
import React from 'react';
import {InsertCssProvider} from 'isomorphic-style-loader/lib/withStyles';
export default function MyRootComponent(props) {
return (
<InsertCssProvider value={props.insertCss}> // <--
{props.children}
</InsertCssProvider>
);
}
And see server-side rendering example below:
import express from 'express';
import ReactDOM from 'react-dom';
import router from './router.js'; // <-- isomorphic router, see react-starter-kit for example
const server = express();
const port = process.env.PORT || 3000;
// Server-side rendering of the React app
server.get('*', (req, res, next) => {
const css = new Set(); // CSS for all rendered React components
const insertCss = (...styles) => styles.forEach(style => css.add(style._getCss()));
router.dispatch({ ...req, context }).then((component, state) => {
const body = ReactDOM.renderToString(component);
const html = `<!doctype html>
<html>
<head>
<script async src="/client.js"></script>
<style type="text/css">${[...css].join('')}</style>
</head>
<body>
<div id="root">${body}</div>
</body>
</html>`;
res.status(state.statusCode).send(html);
}).catch(next);
});
server.listen(port, () => {
console.log(`Node.js app is running at http://localhost:${port}/`);
});
It should generate an HTML output similar to this one:
<html>
<head>
<title>My Application</title>
<script async src="/client.js"></script>
<style type="text/css">
.MyComponent_root_Hi8 { padding: 10px; }
.MyComponent_title_e9Q { color: red; }
</style>
</head>
<body>
<div id="root">
<div class="MyComponent_root_Hi8" data-reactid=".cttboum80" data-react-checksum="564584530">
<h1 class="MyComponent_title_e9Q" data-reactid=".cttboum80.0">Hello, World!</h1>
</div>
</div>
</body>
</html>
Regardless of how many styles components there are in the app.js
bundle,
only critical CSS is going to be rendered on the server inside the <head>
section of HTML document. Critical CSS is what actually used on the
requested web page, effectively dealing with FOUC
issue and improving client-side performance. CSS of the unmounted components
will be removed from the DOM.
You can activate hot module reload for style by setting the debug
option to true in your webpack
configuration. If you are using webpack 2, you need to supply it though the LoaderOptionsPlugin
because the debug
option has been removed.
The MIT License © 2015-present Kriasoft (@kriasoft). All rights reserved.
Made with ♥ by Konstantin Tarkus (@koistya, blog), Vladimir Kutepov (frenzzy) and contributors
FAQs
CSS style loader for Webpack optimized for critical path CSS rendering and isomoprhic web apps
We found that isomorphic-style-loader--react-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.