mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-30 15:00:09 +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.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
|
||||||
import {flatten, uniqBy} from 'lodash';
|
import {flatten, uniqBy} from 'lodash';
|
||||||
import {
|
import {
|
||||||
RedirectsCreator,
|
RedirectsCreator,
|
||||||
|
@ -13,8 +12,6 @@ import {
|
||||||
RedirectMetadata,
|
RedirectMetadata,
|
||||||
PluginOptions,
|
PluginOptions,
|
||||||
} from './types';
|
} from './types';
|
||||||
import createRedirectPageContent from './createRedirectPageContent';
|
|
||||||
import {addTrailingSlash, getFilePathForRoutePath} from './utils';
|
|
||||||
import {
|
import {
|
||||||
fromExtensionsRedirectCreator,
|
fromExtensionsRedirectCreator,
|
||||||
toExtensionsRedirectCreator,
|
toExtensionsRedirectCreator,
|
||||||
|
|
|
@ -31,7 +31,7 @@ export default function pluginClientRedirectsPages(
|
||||||
const redirects: RedirectMetadata[] = collectRedirects(pluginContext);
|
const redirects: RedirectMetadata[] = collectRedirects(pluginContext);
|
||||||
|
|
||||||
// Write files only at the end: make code more easy to test without IO
|
// 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 fs from 'fs-extra';
|
||||||
import {RedirectMetadata} from './types';
|
import path from 'path';
|
||||||
|
import {memoize} from 'lodash';
|
||||||
|
|
||||||
type RedirectFile = Pick<
|
import {PluginContext, RedirectMetadata} from './types';
|
||||||
RedirectMetadata,
|
import createRedirectPageContent from './createRedirectPageContent';
|
||||||
'redirectAbsoluteFilePath' | 'redirectPageContent'
|
import {addTrailingSlash, getFilePathForRoutePath} from './utils';
|
||||||
>;
|
|
||||||
|
|
||||||
export default async function writeRedirectFiles(redirects: RedirectFile[]) {
|
type FileMetadata = RedirectMetadata & {
|
||||||
async function writeRedirectFile(redirect: RedirectFile) {
|
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 {
|
try {
|
||||||
await fs.writeFile(
|
await fs.writeFile(file.fileAbsolutePath, file.fileContent);
|
||||||
redirect.redirectAbsoluteFilePath,
|
|
||||||
redirect.redirectPageContent,
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Redirect file creation error: ${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