Repository navigation
Closed
Labels
bugAn error in the Docusaurus core causing instability or issues with its executiondifficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.externalThis issue is caused by an external dependency and not Docusaurus.
Description
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
Hi, I have enabled top-level await support in webpack config, and things work fine during docusaurus run, but fails during docusaurus build
This is my mdx content:
---
title: Markdown page example
---
export const test = await Promise.resolve('hello')
<div>{test}</div>Reproducible demo
Steps to reproduce
- Enable top-level await support in webpack by setting
experiments.topLevelAwaittotrueusing configureWebpack (see plugins at docusaurus.config.js ) - Try using top-level await during
docusaurus run, things work fine. - It fails during
docusaurus build
Expected behavior
top-level await should also work during build process
Actual behavior
top-level await doesn't work during build process
[INFO] [en] Creating an optimized production build...
✔ Client
✖ Server
Compiled with some errors in 18.35s
[ERROR] Docusaurus server-side rendering could not render static page with path /test.
TypeError: Cannot read properties of undefined (reading 'title')
[ERROR] Unable to build website for locale en.
[ERROR] Error: Failed to compile with errors.
Your environment
- Docusaurus version used: 2.0.0-beta.18
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): windows 10
Self-service
- I'd be willing to fix this bug myself.
Metadata
Metadata
Assignees
Labels
bugAn error in the Docusaurus core causing instability or issues with its executiondifficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.externalThis issue is caused by an external dependency and not Docusaurus.
Activity
Josh-Cena commentedon Apr 25, 2022
Related to #6520
We can investigate this—my hypothesis is that it has something to do with the static site generation plugin, which probably doesn't wait for the top-level promise to resolve. No guarantee that it will be fixed, though, since it's not our own code.
Josh-Cena commentedon May 6, 2022
For some reason, TLA results in the default export being a promise, instead of the entire module being a promise. In order to investigate, I logged the output of what got loaded (the first prop passed to
Loadable.Map). In CSR (both dev and prod):But in SSR:
I will spin up an isolated Webpack config to see who that is to blame (Babel? Webpack? React-loadable?)
Josh-Cena commentedon May 6, 2022
I strongly suspect this has something to do with
babel-plugin-dynamic-import-node. Maybe it convertsimport()to something like{ default: Promise.resolve(require(<path>)) }? I need to check their implementation. I know 2.3.3 has broken some stuff for us.Edit. Seems that's indeed the case: playground The reason why it puts it inside a
defaultis becauserequirereturns a promise, which Babel doesn't know to await, so it can't see the__esModulemarker...So the next question is, do we actually need this, now that all Node versions we support also support dynamic import...
Josh-Cena commentedon May 6, 2022
While trying to remove
babel-plugin-dynamic-import-nodeI'm running into more problems. Here's my change:And I attempted to build a site with nothing but
src/pages/index.jscontaining the following:Surprisingly,
the landing page compiled successfully(edit: it didn't; it silently failed and outputs an HTML file containing nothing but an error banner), but the 404 page gave the error:The actual chunk for the 404 page is
763.d3922ca1.js.I've seen the same errors in the past when I'm setting up SWC, but I don't have the patience to look into it since it looks super obscure. My hypothesis is that it's because the same file is compiled differently, leading to different hashes (? I decided to not touch it at this point. If anyone wants to look into it, help appreciated.
[-]Top level await support during build[/-][+]Module with Top-level await transformed incorrectly during server build[/+]Josh-Cena commentedon May 12, 2022
I actually dug into the server bundle output (before SSG plugin kicks in), and the "module not found" is thrown from here:
Note that
763is mapped to3e8c76c2, which differs from its actual hash that's output to disk (from the client build), because the Babel output is different, causing the content hash to be different. When we had thebabel-plugin-dynamic-import-node, nothing similar to that has been outputted, because Webpack didn't do code-splitting withrequire, so there were no "chunks" at that time. I guess we just have to(a) make the server build output code-splitted files and later clean them upor (b) stop server build from doing code-splitting and referencing physical assets even with dynamic import (?)Edit. The problem is that the SSG webpack plugin kicks in too early: before the dynamically imported code gets emitted. Here's a minimum repro:
When you comment out the SSG plugin, everything gets emitted, including the dynamic chunks; however, the above code throws:
Cannot find module './assets/js/comp_js.b71b1699.js'Maybe we can fix it in the SSG plugin?
slorber commentedon Oct 16, 2026
This is fixed on
main(upcoming v4), where Rspack + SWC is now the default bundler and JS loader (#12625, #12626).The root cause was
babel-plugin-dynamic-import-node, which Docusaurus applies to the server bundle in Babel mode. With top-level await, it turned the page module's default export into a Promise during SSR.Verified on current
mainwith the reporter's exact page (src/pages/test.mdxwithexport const test = await Promise.resolve('hello')):[SUCCESS] Generated static files. The built HTML contains<title data-rh="true">Markdown page example | …</title>and<article><div>hello</div></article>.configureWebpackplugin returning{experiments: {topLevelAwait: true}}: same result.src/pages/tla-js.js): builds and renders<div id="tla-js">Hello</div>.hellois still rendered after hydration, with no console errors.Note: the deprecated Webpack + Babel mode (
siteConfig.webpack: true) still fails the same way (Cannot destructure property 'title' of 'metadata' as it is undefined), because it still appliesbabel-plugin-dynamic-import-nodeto the server bundle. Use the default Rspack bundler if you need top-level await.(AI-assisted)