mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
perf(v2): unblock metadata processing when possible (#2054)
This commit is contained in:
parent
91b261af7a
commit
be396168d1
1 changed files with 31 additions and 33 deletions
|
@ -59,30 +59,11 @@ export default async function processMetadata({
|
||||||
const {versioning} = env;
|
const {versioning} = env;
|
||||||
const filePath = path.join(refDir, source);
|
const filePath = path.join(refDir, source);
|
||||||
|
|
||||||
const fileString = await fs.readFile(filePath, 'utf-8');
|
const fileStringPromise = fs.readFile(filePath, 'utf-8');
|
||||||
const {frontMatter = {}, excerpt} = parse(fileString);
|
const lastUpdatedPromise = lastUpdated(filePath, options);
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
let version;
|
let version;
|
||||||
let id = baseID;
|
|
||||||
|
|
||||||
// Append subdirectory as part of id.
|
|
||||||
const dirName = path.dirname(source);
|
const dirName = path.dirname(source);
|
||||||
if (dirName !== '.') {
|
|
||||||
id = `${dirName}/${baseID}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (versioning.enabled) {
|
if (versioning.enabled) {
|
||||||
if (/^version-/.test(dirName)) {
|
if (/^version-/.test(dirName)) {
|
||||||
const inferredVersion = dirName
|
const inferredVersion = dirName
|
||||||
|
@ -101,6 +82,34 @@ export default async function processMetadata({
|
||||||
const versionPath =
|
const versionPath =
|
||||||
version && version !== versioning.latestVersion ? version : '';
|
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'
|
// The last portion of the url path. Eg: 'foo/bar', 'bar'
|
||||||
const routePath =
|
const routePath =
|
||||||
version && version !== 'next'
|
version && version !== 'next'
|
||||||
|
@ -113,18 +122,7 @@ export default async function processMetadata({
|
||||||
routePath,
|
routePath,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const {sidebar_label, custom_edit_url} = frontMatter;
|
const {lastUpdatedAt, lastUpdatedBy} = await lastUpdatedPromise;
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Assign all of object properties during instantiation (if possible) for NodeJS optimization
|
// Assign all of object properties during instantiation (if possible) for NodeJS optimization
|
||||||
// Adding properties to object after instantiation will cause hidden class transitions.
|
// Adding properties to object after instantiation will cause hidden class transitions.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue