diff --git a/.changeset/khaki-bags-brake.md b/.changeset/khaki-bags-brake.md new file mode 100644 index 0000000000..adb570869c --- /dev/null +++ b/.changeset/khaki-bags-brake.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix: wait for async operation to finish diff --git a/packages/astro/src/build/page.ts b/packages/astro/src/build/page.ts index 89e2ac2a5e..691b37507f 100644 --- a/packages/astro/src/build/page.ts +++ b/packages/astro/src/build/page.ts @@ -169,7 +169,7 @@ const defaultExtensions: Readonly> = { }; /** Gather necessary framework runtimes (React, Vue, Svelte, etc.) for dynamic components */ -async function gatherRuntimes({ astroConfig, buildState, filepath, logging, resolvePackageUrl, mode, runtime }: PageBuildOptions) { +async function gatherRuntimes({ astroConfig, buildState, filepath, logging, resolvePackageUrl, mode, runtime }: PageBuildOptions): Promise> { const imports = new Set(); // Only astro files @@ -332,20 +332,22 @@ async function gatherRuntimes({ astroConfig, buildState, filepath, logging, reso }); // add all imports to build output - [...imports].map(async (url) => { - // don’t end up in an infinite loop building same URLs over and over - const alreadyBuilt = buildState[url]; - if (alreadyBuilt) return; + await Promise.all( + [...imports].map(async (url) => { + if (buildState[url]) return; // don’t build already-built URLs - // add new results to buildState - const result = await runtime.load(url); - if (result.statusCode === 200) { - buildState[url] = { - srcPath: filepath, - contents: result.contents, - contentType: result.contentType || mime.getType(url) || '', - encoding: 'utf8', - }; - } - }); + // add new results to buildState + const result = await runtime.load(url); + if (result.statusCode === 200) { + buildState[url] = { + srcPath: filepath, + contents: result.contents, + contentType: result.contentType || mime.getType(url) || '', + encoding: 'utf8', + }; + } + }) + ); + + return imports; }