From 666a13a3ae4da0e1c8fbbe15623184f52341bccb Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Mon, 28 Feb 2022 11:10:36 +0200 Subject: [PATCH] Load WASM in parallel without blocking the main thread --- packages/next/server/web/sandbox/context.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/next/server/web/sandbox/context.ts b/packages/next/server/web/sandbox/context.ts index 2c56aa841a60..eae4020201e1 100644 --- a/packages/next/server/web/sandbox/context.ts +++ b/packages/next/server/web/sandbox/context.ts @@ -1,6 +1,6 @@ import type { Context } from 'vm' import { Blob, File, FormData } from 'next/dist/compiled/formdata-node' -import { readFileSync } from 'fs' +import { readFileSync, promises as fs } from 'fs' import { requireDependencies } from './require' import { TransformStream } from 'next/dist/compiled/web-streams-polyfill' import cookie from 'next/dist/compiled/cookie' @@ -270,9 +270,15 @@ async function loadWasmBindings( wasmBindings: WasmBinding[] ): Promise> { const modules: Record = {} - for (const binding of wasmBindings) { - const module = await WebAssembly.compile(readFileSync(binding.filePath)) - modules[binding.name] = module - } + + await Promise.all( + wasmBindings.map(async (binding) => { + const module = await WebAssembly.compile( + await fs.readFile(binding.filePath) + ) + modules[binding.name] = module + }) + ) + return modules }