fix(v2): handle non existent blog, docs, pages (#1459)

* fix(v2): handle non existent blog, docs, pages

* nits
This commit is contained in:
Endi 2019-05-15 13:55:07 +07:00 committed by GitHub
parent f84abfe2d1
commit 55d7920825
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 12 deletions

View file

@ -12,18 +12,18 @@ import loadSidebars from '../sidebars';
describe('loadSidebars', () => {
test('normal site with sidebars', async () => {
const sidebar = require(path.join(
const sidebarPath = path.join(
__dirname,
'__fixtures__',
'website',
'sidebars.json',
));
const result = loadSidebars({sidebar});
);
const result = loadSidebars(sidebarPath);
expect(result).toMatchSnapshot();
});
test('site without sidebars', () => {
const result = loadSidebars({sidebar: {}});
const result = loadSidebars(null);
expect(result).toMatchSnapshot();
});
});

View file

@ -6,7 +6,7 @@
*/
const globby = require('globby');
const importFresh = require('import-fresh');
const fs = require('fs');
const path = require('path');
const {idx, normalizeUrl, docuHash} = require('@docusaurus/utils');
@ -48,14 +48,16 @@ class DocusaurusPluginContentDocs {
// Fetches blog contents and returns metadata for the contents.
async loadContent() {
const {include, routeBasePath, sidebarPath} = this.options;
const {siteDir, siteConfig} = this.context;
const {siteConfig} = this.context;
const docsDir = this.contentPath;
// We don't want sidebars to be cached because of hotreloading.
const sidebar = importFresh(sidebarPath);
const docsSidebars = loadSidebars({siteDir, sidebar});
if (!fs.existsSync(docsDir)) {
return null;
}
// @tested - build the docs ordering such as next, previous, category and sidebar
const docsSidebars = loadSidebars(sidebarPath);
// Build the docs ordering such as next, previous, category and sidebar
const order = createOrder(docsSidebars);
// Prepare metadata container.
@ -111,6 +113,9 @@ class DocusaurusPluginContentDocs {
}
async contentLoaded({content, actions}) {
if (!content) {
return;
}
const {docLayoutComponent, docItemComponent, routeBasePath} = this.options;
const {addRoute, createData} = actions;

View file

@ -5,6 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const importFresh = require('import-fresh');
/**
* Check that item contains only allowed keys
*
@ -109,7 +112,11 @@ function normalizeSidebar(sidebars) {
}, {});
}
module.exports = function loadSidebars({sidebar}) {
const allSidebars = sidebar;
module.exports = function loadSidebars(sidebarPath) {
// We don't want sidebars to be cached because of hotreloading.
let allSidebars = {};
if (sidebarPath && fs.existsSync(sidebarPath)) {
allSidebars = importFresh(sidebarPath);
}
return normalizeSidebar(allSidebars);
};