Skip to content

Instantly share code, notes, and snippets.

@anderser
Forked from dlmr/main.html
Last active June 8, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderser/7846d594b3c9f5114fa4129753ab37ed to your computer and use it in GitHub Desktop.
Save anderser/7846d594b3c9f5114fa4129753ab37ed to your computer and use it in GitHub Desktop.
Above fold style tag (css) using roc webapp react
body {
background-color: red;
}
require('./styles/abovefold.scss');
export default true
<!DOCTYPE html>
<html>
<head>
<style>
{{ custom.aboveFoldCSS }}
</style>
</head>
<body>
Hello World
</body>
</html>
module.exports = {
webpack: {
entry: {
abovefold: './src/abovefoldentry',
}
},
}
// this content should be in the client entry file
__DEV__? require('./styles/abovefold.scss'): {}
import { appConfig } from 'roc-package-web-app-react/app/shared';
import path from 'path';
import fs from 'fs';
import { getAbsolutePath } from 'roc';
// get chunks by name and extensions
const getChunks = (stats, name, ext = 'js') => {
let chunk = stats.assetsByChunkName[name];
// a chunk could be a string or an array, so make sure it is an array
if (!(Array.isArray(chunk))) {
chunk = [chunk];
}
return chunk
.filter((chunkName) => path.extname(chunkName) === `.${ext}`)
};
let preloadContent;
const getAboveFoldCSS = (settings) => {
if (!preloadContent) {
const assetsDirectory = getAbsolutePath(settings.build.output.web);
const stats = JSON.parse(fs.readFileSync(path.join(assetsDirectory, 'webpack-analysis.json'), 'utf8'));
const preloadFilename = getChunks(stats, 'abovefold', 'css')[0];
preloadContent = fs.readFileSync(path.join(assetsDirectory, preloadFilename), 'utf8');
}
return preloadContent;
};
export default function getTemplateValues({ reduxState, settings }) {
return {
bodyClass: 'main',
publication: appConfig.publication,
device: reduxState.contents.device.device,
aboveFoldCSS: __DIST__? getAboveFoldCSS(settings): '',
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment