Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top Level Await of Modules with SSR #7356

Closed
7 tasks done
modderme123 opened this issue Mar 17, 2022 · 1 comment · Fixed by #8641
Closed
7 tasks done

Top Level Await of Modules with SSR #7356

modderme123 opened this issue Mar 17, 2022 · 1 comment · Fixed by #8641

Comments

@modderme123
Copy link
Contributor

Describe the bug

In SSR, files seem to be collapsed into a single output file. However, this causes an issue with top level await because while promises would normally allow the rest of the file to be parsed and all top level variables to be defined, the browser immediately evaluates the promise and cannot get the value of the variable.

For example, consider the following code (this is roughly what you would expect await import("simple.js") to compile into)

console.log(await Promise.resolve().then(function () { return simple; }))
var simple = "hi"

The browser logs undefined, instead of the expected value of "hi".

This is important because using imports and top level await is a way to only import modules when in the browser and not on the server (or vice versa). For example, with SolidJS, I'm doing const mymodule = isServer ? await import ("server-module") : await import("client-stub")

Reproduction

https://stackblitz.com/edit/node-zgcsub

System Info

System:
    OS: macOS 11.6.1
    CPU: (8) x64 Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz
    Memory: 86.71 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.7.1 - ~/.brew/bin/node
    Yarn: 1.22.17 - ~/.brew/bin/yarn
    npm: 8.5.4 - ~/.brew/bin/npm
  Browsers:
    Chrome: 99.0.4844.74
    Chrome Canary: 101.0.4948.0
    Firefox: 96.0.3
    Safari: 15.1
  npmPackages:
    vite: ^2.8.6 => 2.8.6

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

Vite turns on inlineDynamicImports for SSR build.

inlineDynamicImports: ssr && typeof input === 'string',

inlineDynamicImports changes execution order.
https://rollupjs.org/guide/en/#outputinlinedynamicimports
rollup/rollup#4166

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants