mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-21 13:06:58 +02:00
fix(v2): i18n perf issue: getTranslationFile() should not load content again (#4593)
* Fix getTranslationFiles() perf issue * improve doc
This commit is contained in:
parent
e99bb43823
commit
cb403afa93
5 changed files with 22 additions and 20 deletions
|
@ -104,8 +104,8 @@ export default function pluginContentDocs(
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async getTranslationFiles() {
|
async getTranslationFiles({content}) {
|
||||||
return getLoadedContentTranslationFiles(await this.loadContent!());
|
return getLoadedContentTranslationFiles(content);
|
||||||
},
|
},
|
||||||
|
|
||||||
getClientModules() {
|
getClientModules() {
|
||||||
|
|
16
packages/docusaurus-types/src/index.d.ts
vendored
16
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -216,14 +216,14 @@ export type AllContent = Record<
|
||||||
// TODO improve type (not exposed by postcss-loader)
|
// TODO improve type (not exposed by postcss-loader)
|
||||||
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
|
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
|
||||||
|
|
||||||
export interface Plugin<T> {
|
export interface Plugin<Content> {
|
||||||
name: string;
|
name: string;
|
||||||
loadContent?(): Promise<T>;
|
loadContent?(): Promise<Content>;
|
||||||
contentLoaded?({
|
contentLoaded?({
|
||||||
content,
|
content,
|
||||||
actions,
|
actions,
|
||||||
}: {
|
}: {
|
||||||
content: T; // the content loaded by this plugin instance
|
content: Content; // the content loaded by this plugin instance
|
||||||
allContent: AllContent; // content loaded by ALL the plugins
|
allContent: AllContent; // content loaded by ALL the plugins
|
||||||
actions: PluginContentLoadedActions;
|
actions: PluginContentLoadedActions;
|
||||||
}): void;
|
}): void;
|
||||||
|
@ -249,7 +249,11 @@ export interface Plugin<T> {
|
||||||
// TODO before/afterDevServer implementation
|
// TODO before/afterDevServer implementation
|
||||||
|
|
||||||
// translations
|
// translations
|
||||||
getTranslationFiles?(): Promise<TranslationFiles>;
|
getTranslationFiles?({
|
||||||
|
content,
|
||||||
|
}: {
|
||||||
|
content: Content;
|
||||||
|
}): Promise<TranslationFiles>;
|
||||||
getDefaultCodeTranslationMessages?(): Promise<
|
getDefaultCodeTranslationMessages?(): Promise<
|
||||||
Record<
|
Record<
|
||||||
string, // id
|
string, // id
|
||||||
|
@ -260,9 +264,9 @@ export interface Plugin<T> {
|
||||||
content,
|
content,
|
||||||
translationFiles,
|
translationFiles,
|
||||||
}: {
|
}: {
|
||||||
content: T; // the content loaded by this plugin instance
|
content: Content; // the content loaded by this plugin instance
|
||||||
translationFiles: TranslationFiles;
|
translationFiles: TranslationFiles;
|
||||||
}): T;
|
}): Content;
|
||||||
translateThemeConfig?({
|
translateThemeConfig?({
|
||||||
themeConfig,
|
themeConfig,
|
||||||
translationFiles,
|
translationFiles,
|
||||||
|
|
|
@ -30,7 +30,10 @@ async function writePluginTranslationFiles({
|
||||||
options: WriteTranslationsOptions;
|
options: WriteTranslationsOptions;
|
||||||
}) {
|
}) {
|
||||||
if (plugin.getTranslationFiles) {
|
if (plugin.getTranslationFiles) {
|
||||||
const translationFiles = await plugin.getTranslationFiles();
|
const content = await plugin.loadContent?.();
|
||||||
|
const translationFiles = await plugin.getTranslationFiles({
|
||||||
|
content,
|
||||||
|
});
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
translationFiles.map(async (translationFile) => {
|
translationFiles.map(async (translationFile) => {
|
||||||
|
|
|
@ -52,14 +52,6 @@ export function sortConfig(routeConfigs: RouteConfig[]): void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AllPluginsTranslationFiles = Record<
|
|
||||||
string, // plugin name
|
|
||||||
Record<
|
|
||||||
string, // plugin id
|
|
||||||
TranslationFiles
|
|
||||||
>
|
|
||||||
>;
|
|
||||||
|
|
||||||
export async function loadPlugins({
|
export async function loadPlugins({
|
||||||
pluginConfigs,
|
pluginConfigs,
|
||||||
context,
|
context,
|
||||||
|
@ -96,7 +88,9 @@ export async function loadPlugins({
|
||||||
const contentLoadedTranslatedPlugins: ContentLoadedTranslatedPlugin[] = await Promise.all(
|
const contentLoadedTranslatedPlugins: ContentLoadedTranslatedPlugin[] = await Promise.all(
|
||||||
contentLoadedPlugins.map(async (contentLoadedPlugin) => {
|
contentLoadedPlugins.map(async (contentLoadedPlugin) => {
|
||||||
const translationFiles =
|
const translationFiles =
|
||||||
(await contentLoadedPlugin.plugin?.getTranslationFiles?.()) ?? [];
|
(await contentLoadedPlugin.plugin?.getTranslationFiles?.({
|
||||||
|
content: contentLoadedPlugin.content,
|
||||||
|
})) ?? [];
|
||||||
const localizedTranslationFiles = await Promise.all(
|
const localizedTranslationFiles = await Promise.all(
|
||||||
translationFiles.map((translationFile) =>
|
translationFiles.map((translationFile) =>
|
||||||
localizePluginTranslationFile({
|
localizePluginTranslationFile({
|
||||||
|
|
|
@ -601,7 +601,7 @@ For example, the in docusaurus-plugin-content-docs:
|
||||||
|
|
||||||
## i18n lifecycles {#i18n-lifecycles}
|
## i18n lifecycles {#i18n-lifecycles}
|
||||||
|
|
||||||
### `getTranslationFiles()` {#get-translation-files}
|
### `getTranslationFiles({content})` {#get-translation-files}
|
||||||
|
|
||||||
Plugins declare the JSON translation files they want to use.
|
Plugins declare the JSON translation files they want to use.
|
||||||
|
|
||||||
|
@ -619,7 +619,7 @@ module.exports = function (context, options) {
|
||||||
return {
|
return {
|
||||||
name: 'my-plugin',
|
name: 'my-plugin',
|
||||||
// highlight-start
|
// highlight-start
|
||||||
async getTranslationFiles() {
|
async getTranslationFiles({content}) {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
path: 'sidebar-labels',
|
path: 'sidebar-labels',
|
||||||
|
@ -628,6 +628,7 @@ module.exports = function (context, options) {
|
||||||
message: 'Some Sidebar Label',
|
message: 'Some Sidebar Label',
|
||||||
description: 'A label used in my plugin in the sidebar',
|
description: 'A label used in my plugin in the sidebar',
|
||||||
},
|
},
|
||||||
|
someLabelFromContent: content.myLabel,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue