astro-typst icon indicating copy to clipboard operation
astro-typst copied to clipboard

Two full rendering required when used as content collection

Open crd2333 opened this issue 6 months ago • 4 comments

I'm not familiar with astro or anything about frontend, but I noticed one thing: Each time I use pnpm build to build the site, the console.debug(`Detecting ${id}`) in target definition appears twice. And one in [content] Syncing content, the other in [build] Building static entrypoints....

I see some code like this let { getFrontmatter } = await renderToHTMLish(...). It seems getting the frontmatter info also needs rendering the full typst file to html (or svg?). This seems reasonable as typst's metadata is more flexible than markdown's text.

But this is extremely annoying when the file number and size get increased, where my build operation reached the max size of memory and crashed (even though I have tried to enlarge the max-old-space-size, maybe my file is too large XD)...

Trying to split the files maybe is able to solve the problem, but one file requiring two rendering is anyway an awful thing. I really appreciate your great work. Do you have any idea about the problem? Like "caching the first rendering for the next" / "caching the metadata to jump some rendering", etc?

crd2333 avatar May 23 '25 07:05 crd2333

It seems like renderToHTMLXXX is forgetting compilation cache evict, which causes memory leak. You can find similar call in the same source file:

$typst.evictCache(60);

To verify it locally, you could modify the source code in node_modules and see if your memory get relief.

Myriad-Dreamin avatar May 27 '25 17:05 Myriad-Dreamin

I added $typst.evictCache(60); for renderToHTMLish, seems not that useful, though... the memory can still run up to 7000MB.

But I'm sure with single rendering it cen be done, since I previously do not use content collection / frontmatter and simply render the pages as component, which runs well.

crd2333 avatar May 28 '25 16:05 crd2333

@Myriad-Dreamin Finally recall that $typst.query does not seem to support HTML export if CompileDocArgs is passed, so I render the source first to get a NodeTypstDocument. Is this correct?

OverflowCat avatar May 30 '25 17:05 OverflowCat

@Myriad-Dreamin Finally recall that $typst.query does not seem to support HTML export if CompileDocArgs is passed, so I render the source first to get a NodeTypstDocument. Is this correct?

Correct.

See https://github.com/Myriad-Dreamin/typst.ts/issues/647#issuecomment-2693595551. I didn't add $typst.queryHTML for seeking better API design.

When I'm playing with the vite-plugin-typst PR, I realize that there has been a way to make queryHtml by ourselves in current library:

typst.ts/projects/vite-plugin-typst/examples/mixin-parts/vite.config.mjs

Lines 8 to 11 in 733494d

const res = checkExecResult(mainFilePath, project.compileHtml({ mainFilePath }), ctx); return { frontmatter: res && project.query(res, { selector: '', field: 'value' })[0], }; In short, it is like query(compileHtml(args)?.result). It is just not quite convenient.

Myriad-Dreamin avatar May 30 '25 17:05 Myriad-Dreamin