From 1a8a5e6d37c4812d75d7140612887b7dc6816c65 Mon Sep 17 00:00:00 2001 From: endiliey Date: Mon, 25 Nov 2019 19:16:06 +0700 Subject: [PATCH] perf(v2): unblock metadata processing when possible --- .../src/metadata.ts | 64 +++++++++---------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/metadata.ts b/packages/docusaurus-plugin-content-docs/src/metadata.ts index 31567af0e3c1..cb649286ff15 100644 --- a/packages/docusaurus-plugin-content-docs/src/metadata.ts +++ b/packages/docusaurus-plugin-content-docs/src/metadata.ts @@ -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 @@ -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' @@ -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.