feat(v2): lazy load algolia css so its not render blocking (#2125)

* perf(v2): algolia css should not be render blocking

* tweak

* lazy load css

* tweak

* tweak

* review
This commit is contained in:
Endi 2019-12-15 16:43:46 +07:00 committed by GitHub
parent b6315f4136
commit dcf1fef001
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 13 deletions

View file

@ -14,5 +14,25 @@ module.exports = function() {
getThemePath() { getThemePath() {
return path.resolve(__dirname, './theme'); return path.resolve(__dirname, './theme');
}, },
configureWebpack() {
// Ensure that algolia docsearch css is its own chunk
return {
optimization: {
splitChunks: {
cacheGroups: {
algolia: {
name: 'algolia',
test: /algolia\.css$/,
chunks: `all`,
enforce: true,
// Set priority higher than docusaurus single-css extraction
priority: 60,
},
},
},
},
};
},
}; };
}; };

View file

@ -11,10 +11,7 @@ import classnames from 'classnames';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useHistory} from '@docusaurus/router'; import {useHistory} from '@docusaurus/router';
import './styles.css'; let loaded = false;
const loadJS = () => import('docsearch.js');
let loadedJs = false;
const Search = props => { const Search = props => {
const initialized = useRef(false); const initialized = useRef(false);
@ -54,13 +51,15 @@ const Search = props => {
} }
}; };
const loadAlgoliaJS = () => { const loadAlgolia = () => {
if (!loadedJs) { if (!loaded) {
loadJS().then(a => { Promise.all([import('docsearch.js'), import('./algolia.css')]).then(
loadedJs = true; ([{default: docsearch}]) => {
window.docsearch = a.default; loaded = true;
initAlgolia(); window.docsearch = docsearch;
}); initAlgolia();
},
);
} else { } else {
initAlgolia(); initAlgolia();
} }
@ -99,8 +98,8 @@ const Search = props => {
{'search-bar-expanded': props.isSearchBarExpanded}, {'search-bar-expanded': props.isSearchBarExpanded},
{'search-bar': !props.isSearchBarExpanded}, {'search-bar': !props.isSearchBarExpanded},
)} )}
onClick={loadAlgoliaJS} onClick={loadAlgolia}
onMouseOver={loadAlgoliaJS} onMouseOver={loadAlgolia}
onFocus={toggleSearchIconClick} onFocus={toggleSearchIconClick}
onBlur={toggleSearchIconClick} onBlur={toggleSearchIconClick}
ref={searchBarRef} ref={searchBarRef}

View file

@ -180,6 +180,7 @@ export function createBaseConfig(
plugins: [ plugins: [
new MiniCssExtractPlugin({ new MiniCssExtractPlugin({
filename: isProd ? '[name].[contenthash:8].css' : '[name].css', filename: isProd ? '[name].[contenthash:8].css' : '[name].css',
chunkFilename: isProd ? '[name].[contenthash:8].css' : '[name].css',
// remove css order warnings if css imports are not sorted alphabetically // remove css order warnings if css imports are not sorted alphabetically
// see https://github.com/webpack-contrib/mini-css-extract-plugin/pull/422 for more reasoning // see https://github.com/webpack-contrib/mini-css-extract-plugin/pull/422 for more reasoning
ignoreOrder: true, ignoreOrder: true,