Skip to content

Commit

Permalink
fix(v2): i18n perf issue: getTranslationFile() should not load conten…
Browse files Browse the repository at this point in the history
…t again (#4593)

* Fix getTranslationFiles() perf issue

* improve doc
  • Loading branch information
slorber authored Apr 9, 2021
1 parent e99bb43 commit cb403af
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export default function pluginContentDocs(
});
},

async getTranslationFiles() {
return getLoadedContentTranslationFiles(await this.loadContent!());
async getTranslationFiles({content}) {
return getLoadedContentTranslationFiles(content);
},

getClientModules() {
Expand Down
16 changes: 10 additions & 6 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ export type AllContent = Record<
// TODO improve type (not exposed by postcss-loader)
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};

export interface Plugin<T> {
export interface Plugin<Content> {
name: string;
loadContent?(): Promise<T>;
loadContent?(): Promise<Content>;
contentLoaded?({
content,
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
actions: PluginContentLoadedActions;
}): void;
Expand All @@ -249,7 +249,11 @@ export interface Plugin<T> {
// TODO before/afterDevServer implementation

// translations
getTranslationFiles?(): Promise<TranslationFiles>;
getTranslationFiles?({
content,
}: {
content: Content;
}): Promise<TranslationFiles>;
getDefaultCodeTranslationMessages?(): Promise<
Record<
string, // id
Expand All @@ -260,9 +264,9 @@ export interface Plugin<T> {
content,
translationFiles,
}: {
content: T; // the content loaded by this plugin instance
content: Content; // the content loaded by this plugin instance
translationFiles: TranslationFiles;
}): T;
}): Content;
translateThemeConfig?({
themeConfig,
translationFiles,
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus/src/commands/writeTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ async function writePluginTranslationFiles({
options: WriteTranslationsOptions;
}) {
if (plugin.getTranslationFiles) {
const translationFiles = await plugin.getTranslationFiles();
const content = await plugin.loadContent?.();
const translationFiles = await plugin.getTranslationFiles({
content,
});

await Promise.all(
translationFiles.map(async (translationFile) => {
Expand Down
12 changes: 3 additions & 9 deletions packages/docusaurus/src/server/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
pluginConfigs,
context,
Expand Down Expand Up @@ -96,7 +88,9 @@ export async function loadPlugins({
const contentLoadedTranslatedPlugins: ContentLoadedTranslatedPlugin[] = await Promise.all(
contentLoadedPlugins.map(async (contentLoadedPlugin) => {
const translationFiles =
(await contentLoadedPlugin.plugin?.getTranslationFiles?.()) ?? [];
(await contentLoadedPlugin.plugin?.getTranslationFiles?.({
content: contentLoadedPlugin.content,
})) ?? [];
const localizedTranslationFiles = await Promise.all(
translationFiles.map((translationFile) =>
localizePluginTranslationFile({
Expand Down
5 changes: 3 additions & 2 deletions website/docs/lifecycle-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ For example, the in docusaurus-plugin-content-docs:

## i18n lifecycles {#i18n-lifecycles}

### `getTranslationFiles()` {#get-translation-files}
### `getTranslationFiles({content})` {#get-translation-files}

Plugins declare the JSON translation files they want to use.

Expand All @@ -619,7 +619,7 @@ module.exports = function (context, options) {
return {
name: 'my-plugin',
// highlight-start
async getTranslationFiles() {
async getTranslationFiles({content}) {
return [
{
path: 'sidebar-labels',
Expand All @@ -628,6 +628,7 @@ module.exports = function (context, options) {
message: 'Some Sidebar Label',
description: 'A label used in my plugin in the sidebar',
},
someLabelFromContent: content.myLabel,
},
},
];
Expand Down

0 comments on commit cb403af

Please # to comment.