mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-29 22:47:52 +02:00
fix writeRedirectFiles
This commit is contained in:
parent
26beb00476
commit
e93b98a242
3 changed files with 48 additions and 16 deletions
|
@ -5,7 +5,6 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import {flatten, uniqBy} from 'lodash';
|
||||
import {
|
||||
RedirectsCreator,
|
||||
|
@ -13,8 +12,6 @@ import {
|
|||
RedirectMetadata,
|
||||
PluginOptions,
|
||||
} from './types';
|
||||
import createRedirectPageContent from './createRedirectPageContent';
|
||||
import {addTrailingSlash, getFilePathForRoutePath} from './utils';
|
||||
import {
|
||||
fromExtensionsRedirectCreator,
|
||||
toExtensionsRedirectCreator,
|
||||
|
|
|
@ -31,7 +31,7 @@ export default function pluginClientRedirectsPages(
|
|||
const redirects: RedirectMetadata[] = collectRedirects(pluginContext);
|
||||
|
||||
// Write files only at the end: make code more easy to test without IO
|
||||
await writeRedirectFiles(redirects);
|
||||
await writeRedirectFiles(redirects, pluginContext);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -6,23 +6,58 @@
|
|||
*/
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import {RedirectMetadata} from './types';
|
||||
import path from 'path';
|
||||
import {memoize} from 'lodash';
|
||||
|
||||
type RedirectFile = Pick<
|
||||
RedirectMetadata,
|
||||
'redirectAbsoluteFilePath' | 'redirectPageContent'
|
||||
>;
|
||||
import {PluginContext, RedirectMetadata} from './types';
|
||||
import createRedirectPageContent from './createRedirectPageContent';
|
||||
import {addTrailingSlash, getFilePathForRoutePath} from './utils';
|
||||
|
||||
export default async function writeRedirectFiles(redirects: RedirectFile[]) {
|
||||
async function writeRedirectFile(redirect: RedirectFile) {
|
||||
type FileMetadata = RedirectMetadata & {
|
||||
fileAbsolutePath: string;
|
||||
fileContent: string;
|
||||
};
|
||||
|
||||
function toFileMetadata(
|
||||
redirects: RedirectMetadata[],
|
||||
pluginContext: PluginContext,
|
||||
): FileMetadata[] {
|
||||
// Perf: avoid rendering the template twice with the exact same "props"
|
||||
// We might create multiple redirect pages for the same destination url
|
||||
// note: the first fn arg is the cache key!
|
||||
const createPageContentMemoized = memoize((toUrl: string) => {
|
||||
return createRedirectPageContent({toUrl});
|
||||
});
|
||||
|
||||
return redirects.map((redirect) => {
|
||||
const fileAbsolutePath = path.join(
|
||||
pluginContext.outDir,
|
||||
getFilePathForRoutePath(redirect.fromRoutePath),
|
||||
);
|
||||
const toUrl = addTrailingSlash(
|
||||
`${pluginContext.baseUrl}${redirect.toRoutePath}`,
|
||||
);
|
||||
const fileContent = createPageContentMemoized(toUrl);
|
||||
return {
|
||||
...redirect,
|
||||
fileAbsolutePath,
|
||||
fileContent,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export default async function writeRedirectFiles(
|
||||
redirects: RedirectMetadata[],
|
||||
pluginContext: PluginContext,
|
||||
) {
|
||||
async function writeFile(file: FileMetadata) {
|
||||
try {
|
||||
await fs.writeFile(
|
||||
redirect.redirectAbsoluteFilePath,
|
||||
redirect.redirectPageContent,
|
||||
);
|
||||
await fs.writeFile(file.fileAbsolutePath, file.fileContent);
|
||||
} catch (err) {
|
||||
throw new Error(`Redirect file creation error: ${err}`);
|
||||
}
|
||||
}
|
||||
await Promise.all(redirects.map(writeRedirectFile));
|
||||
|
||||
const files = toFileMetadata(redirects, pluginContext);
|
||||
await Promise.all(files.map(writeFile));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue