Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(v2): unblock metadata processing when possible #2054

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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