Improve build perf (#2697)
* improve md perf * chore: add changesets Co-authored-by: Nate Moore <nate@skypack.dev>
This commit is contained in:
parent
2482fe70b9
commit
91765d79b1
4 changed files with 92 additions and 75 deletions
5
.changeset/orange-coins-whisper.md
Normal file
5
.changeset/orange-coins-whisper.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/markdown-remark': patch
|
||||
---
|
||||
|
||||
Improve performance by optimizing calls to `getHighlighter`
|
5
.changeset/short-rats-double.md
Normal file
5
.changeset/short-rats-double.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Improve build performance by processing `ssrPreload` in serial rather than in parallel
|
|
@ -35,8 +35,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
|
|||
// NOTE: This enforces that `getStaticPaths()` is only called once per route,
|
||||
// and is then cached across all future SSR builds. In the past, we've had trouble
|
||||
// with parallelized builds without guaranteeing that this is called first.
|
||||
await Promise.all(
|
||||
manifest.routes.map(async (route) => {
|
||||
for (const route of manifest.routes) {
|
||||
// static route:
|
||||
if (route.pathname) {
|
||||
allPages[route.component] = {
|
||||
|
@ -63,7 +62,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
|
|||
throw err;
|
||||
}),
|
||||
};
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
// dynamic route:
|
||||
const result = await getStaticPathsForRoute(opts, route)
|
||||
|
@ -116,8 +115,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
|
|||
viteServer,
|
||||
}),
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return { assets, allPages };
|
||||
}
|
||||
|
|
|
@ -30,16 +30,25 @@ export interface ShikiConfig {
|
|||
wrap?: boolean | null;
|
||||
}
|
||||
|
||||
const remarkShiki = async ({ langs = [], theme = 'github-dark', wrap = false }: ShikiConfig) => {
|
||||
const highlighter = await getHighlighter({ theme });
|
||||
/**
|
||||
* getHighlighter() is the most expensive step of Shiki. Instead of calling it on every page,
|
||||
* cache it here as much as possible. Make sure that your highlighters can be cached, state-free.
|
||||
*/
|
||||
const highlighterCache = new Map<string, shiki.Highlighter>();
|
||||
|
||||
const remarkShiki = async ({ langs = [], theme = 'github-dark', wrap = false }: ShikiConfig) => {
|
||||
const cacheID: string = typeof theme === 'string' ? theme : theme.name;
|
||||
let highlighter = highlighterCache.get(cacheID);
|
||||
if (!highlighter) {
|
||||
highlighter = await getHighlighter({ theme });
|
||||
highlighterCache.set(cacheID, highlighter);
|
||||
}
|
||||
for (const lang of langs) {
|
||||
await highlighter.loadLanguage(lang);
|
||||
}
|
||||
|
||||
return () => (tree: any) => {
|
||||
visit(tree, 'code', (node) => {
|
||||
let html = highlighter.codeToHtml(node.value, { lang: node.lang ?? 'plaintext' });
|
||||
let html = highlighter!.codeToHtml(node.value, { lang: node.lang ?? 'plaintext' });
|
||||
|
||||
// Replace "shiki" class naming with "astro" and add "data-astro-raw".
|
||||
html = html.replace('<pre class="shiki"', '<pre data-astro-raw class="astro-code"');
|
||||
|
|
Loading…
Reference in a new issue