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

Different wasm filenames between cargo leptos serve and cargo leptos build #1337

Closed
sebadob opened this issue Jul 13, 2023 · 2 comments · Fixed by #1338
Closed

Different wasm filenames between cargo leptos serve and cargo leptos build #1337

sebadob opened this issue Jul 13, 2023 · 2 comments · Fixed by #1338
Labels
bug Something isn't working

Comments

@sebadob
Copy link
Contributor

sebadob commented Jul 13, 2023

Describe the bug
I stumpled across this while I wanted to provide my input to the Docs issue.

The index page requests different file names between a cargo leptos build (-r) and cargo leptos serve, which makes the build step fail. This happens with leptos in SSR mode.

When I execute it in development mode with either cargo leptos serve or watch, the <link> matches the filename on the system:

<link rel="preload" href="/pkg/leptos-axum-template.wasm" as="fetch" type="application/wasm" crossorigin="">

When I execute the compiled binary, it requests the following wasm in the html with the added _bg, which does not exist at all in target/site, only the filename from above does:

<link rel="preload" href="/pkg/leptos-axum-template_bg.wasm" as="fetch" type="application/wasm" crossorigin="">

Leptos Dependencies

A list of dependencies would be pretty complex, since this a workspace'd setup.
The basis is the start-axum template from the docs though from a few days ago.

Expected behavior
I would expect the same filename all the time, no matter at what stage we are.
I don't know where the _bg is coming from or if there is maybe a good reason for this, but with this being added, I can only make a deployment work with a manual workaround in code inside my custom static files handler and basically rewrite the request from the browser and remove the _bg so it actually maps to an existing wasm file.

Screenshots

I am using rust_embed for all static files to only have a single binary in the end and also
a custom handler for the static files. I just quickly added some debug logging to actually compare what is availbale and what is requested. It is working fine during cargo leptos serve but will fail like in the screenshot after cargo leptos build and then manually executing the binary:

Bildschirmfoto vom 2023-07-13 14-47-48

The .br extension is added by me manually since I want to serve precompressed files. But just to prove, that I did not add this _bg somehow, here a screenshot from the sourcecode from the browser:

Bildschirmfoto vom 2023-07-13 14-52-37

While it does not have the _bg extension with cargo leptos serve:

Bildschirmfoto vom 2023-07-13 14-53-56

If any additional information is needed, please let me know.
I am just in the middle of creating a template for a few upcoming projects that should be based on leptos, but I could push it even though it is not complete yet.

@sebadob sebadob changed the title Different wasm filenames between cargo leptos serve and `cargo leptos build´ Different wasm filenames between cargo leptos serve and cargo leptos build Jul 13, 2023
@gbj gbj added the bug Something isn't working label Jul 13, 2023
@gbj
Copy link
Collaborator

gbj commented Jul 13, 2023

I don't know where the _bg is coming from or if there is maybe a good reason for this

This comes from some code that's used to ensure compatibility between scenarios in which people are building the frontend with wasm-pack (which appends _bg to all .wasm file names) and in which people are building it with cargo-leptos (which does not).

// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
// we add _bg to the wasm files if cargo-leptos doesn't set the env var LEPTOS_OUTPUT_NAME at compile time
// Otherwise we need to add _bg because wasm_pack always does.
let mut wasm_output_name = output_name.clone();
if std::option_env!("LEPTOS_OUTPUT_NAME").is_none() {
wasm_output_name.push_str("_bg");
}

However, this compile-time check is inadvertently done at runtime instead here

// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
// we add _bg to the wasm files if cargo-leptos doesn't set the env var LEPTOS_OUTPUT_NAME
// Otherwise we need to add _bg because wasm_pack always does. This is not the same as options.output_name, which is set regardless
let mut wasm_output_name = output_name.clone();
if std::env::var("LEPTOS_OUTPUT_NAME").is_err() {
wasm_output_name.push_str("_bg");
}

This means what you need to do is LEPTOS_OUTPUT_NAME=start-axum ./target/server/release/start-axum

Will fix in a PR in a second.

@sebadob
Copy link
Contributor Author

sebadob commented Jul 13, 2023

That was a very easy fix. With this the ENV set beforehand it works fine.

Thank you for the fast reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants