Skip to content

Commit

Permalink
perf(v2): unblock metadata processing when possible (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey authored and yangshun committed Nov 25, 2019
1 parent 91b261a commit be39616
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions packages/docusaurus-plugin-content-docs/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,11 @@ export default async function processMetadata({
const {versioning} = env;
const filePath = path.join(refDir, source);

const fileString = await fs.readFile(filePath, 'utf-8');
const {frontMatter = {}, excerpt} = parse(fileString);

// Default id is the file name.
const baseID: string =
frontMatter.id || path.basename(source, path.extname(source));
if (baseID.includes('/')) {
throw new Error('Document id cannot include "/".');
}

// Default title is the id.
const title: string = frontMatter.title || baseID;

const description: string = frontMatter.description || excerpt;
const fileStringPromise = fs.readFile(filePath, 'utf-8');
const lastUpdatedPromise = lastUpdated(filePath, options);

let version;
let id = baseID;

// Append subdirectory as part of id.
const dirName = path.dirname(source);
if (dirName !== '.') {
id = `${dirName}/${baseID}`;
}

if (versioning.enabled) {
if (/^version-/.test(dirName)) {
const inferredVersion = dirName
Expand All @@ -101,6 +82,34 @@ export default async function processMetadata({
const versionPath =
version && version !== versioning.latestVersion ? version : '';

const relativePath = path.relative(siteDir, filePath);

// Cannot use path.join() as it resolves '../' and removes the '@site'. Let webpack loader resolve it.
const aliasedPath = `@site/${relativePath}`;

const docsEditUrl = editUrl
? normalizeUrl([editUrl, posixPath(relativePath)])
: undefined;

const {frontMatter = {}, excerpt} = parse(await fileStringPromise);
const {sidebar_label, custom_edit_url} = frontMatter;

// Default base id is the file name.
const baseID: string =
frontMatter.id || path.basename(source, path.extname(source));

if (baseID.includes('/')) {
throw new Error('Document id cannot include "/".');
}

// Append subdirectory as part of id.
const id = dirName !== '.' ? `${dirName}/${baseID}` : baseID;

// Default title is the id.
const title: string = frontMatter.title || baseID;

const description: string = frontMatter.description || excerpt;

// The last portion of the url path. Eg: 'foo/bar', 'bar'
const routePath =
version && version !== 'next'
Expand All @@ -113,18 +122,7 @@ export default async function processMetadata({
routePath,
]);

const {sidebar_label, custom_edit_url} = frontMatter;

const relativePath = path.relative(siteDir, filePath);

// Cannot use path.join() as it resolves '../' and removes the '@site'. Let webpack loader resolve it.
const aliasedPath = `@site/${relativePath}`;

const docsEditUrl = editUrl
? normalizeUrl([editUrl, posixPath(relativePath)])
: undefined;

const {lastUpdatedAt, lastUpdatedBy} = await lastUpdated(filePath, options);
const {lastUpdatedAt, lastUpdatedBy} = await lastUpdatedPromise;

// Assign all of object properties during instantiation (if possible) for NodeJS optimization
// Adding properties to object after instantiation will cause hidden class transitions.
Expand Down

0 comments on commit be39616

Please sign in to comment.