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

new URL(url, import.meta.url) should not be fatal in SSR mode when its never called #5169

Closed
7 tasks done
doomnoodles opened this issue Oct 1, 2021 · 2 comments
Closed
7 tasks done

Comments

@doomnoodles
Copy link

doomnoodles commented Oct 1, 2021

Describe the bug

Temporary fix

Relevant line in Vite source that throws the described error
Relevant vite-plugin-rsw issue
Relevant SvelteKit issue

vite-plugin-rsw, is a plugin that lets one use Rust on the web with all the good things that come with Vite(HMR, SSR, ...).
That plugin uses wasm-pack under the hood, which creates javascript glue code and types to easily interface with the wasm file.
The glue code contains the following segment, which seems to create an error in Vite when SSR is enabled in SvelteKit:

async function init(input) {
    if (typeof input === 'undefined') { // not executed if input exists
         input = new URL('example-rust-crate-rs_bg.wasm', import.meta.url); // Error: 'new URL(url, import.meta.url)' is not supported in SSR.
    }
    const imports = {};


    if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
        input = fetch(input);
    }



    const { instance, module } = await load(await input, imports);

    wasm = instance.exports;
    init.__wbindgen_wasm_module = module;

    return wasm;
}

If the init function is called with a path to the .wasm file then that line never gets executed but Vite still throws the error. If the line is deleted then Vite does not complain and actually renders the .wasm file in SSR context, but that has to be done by hand.

There should be an ability to ignore such lines and not throw a fatal error at compile time, and only throw the error at run time, if that line is somehow reached, because its demonstrated that the code actually works and its Vite being too harsh.

Reproduction

  1. Download sveltekit-rust-ssr-template and its dependencies
  2. npm install
  3. npm run dev and exit
  4. npm run build

Interestingly when trying SSR with npm run dev then it works sometimes but sluggishly.

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 AMD Ryzen 7 4700U with Radeon Graphics
    Memory: 2.71 GB / 7.37 GB
  Binaries:
    Node: 16.0.0 - C:\Program Files\nodejs\node.EXE
    npm: 7.20.5 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 94.0.4606.61
    Edge: Spartan (44.19041.1023.0), Chromium (92.0.902.78)

Used Package Manager

npm

Logs

No response

Validations

@sh-tiye
Copy link

sh-tiye commented Dec 9, 2021

Also from wasm-pack nshen/test-npm-crate#1 .

@bluwy
Copy link
Member

bluwy commented Mar 20, 2022

Fixed by #7219

@bluwy bluwy closed this as completed Mar 20, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants