Skip to content

Commit

Permalink
feat: use CDN_PKG_PATH at build time to set alternate base URL for …
Browse files Browse the repository at this point in the history
…JS/WASM bundles (#2281) (#2283)
  • Loading branch information
zoomiti authored Feb 16, 2024
1 parent 23bc892 commit 95bd9cc
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions integrations/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use futures::{Stream, StreamExt};
use leptos::{nonce::use_nonce, use_context, RuntimeId};
use leptos_config::LeptosOptions;
use leptos_meta::MetaContext;
use std::borrow::Cow;

extern crate tracing;

Expand Down Expand Up @@ -55,7 +56,9 @@ pub fn html_parts_separated(
options: &LeptosOptions,
meta: Option<&MetaContext>,
) -> (String, &'static str) {
let pkg_path = &options.site_pkg_dir;
let pkg_path = option_env!("CDN_PKG_PATH")
.map(Cow::from)
.unwrap_or_else(|| format!("/{}", options.site_pkg_dir).into());
let output_name = &options.output_name;
let nonce = use_nonce();
let nonce = nonce
Expand Down Expand Up @@ -107,8 +110,8 @@ pub fn html_parts_separated(
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
{head}
<link rel="modulepreload" href="/{pkg_path}/{output_name}.js"{nonce}>
<link rel="preload" href="/{pkg_path}/{wasm_output_name}.wasm" as="fetch" type="application/wasm" crossorigin=""{nonce}>
<link rel="modulepreload" href="{pkg_path}/{output_name}.js"{nonce}>
<link rel="preload" href="{pkg_path}/{wasm_output_name}.wasm" as="fetch" type="application/wasm" crossorigin=""{nonce}>
<script type="module"{nonce}>
function idle(c) {{
if ("requestIdleCallback" in window) {{
Expand All @@ -118,9 +121,9 @@ pub fn html_parts_separated(
}}
}}
idle(() => {{
import('/{pkg_path}/{output_name}.js')
import('{pkg_path}/{output_name}.js')
.then(mod => {{
mod.default('/{pkg_path}/{wasm_output_name}.wasm').then({import_callback});
mod.default('{pkg_path}/{wasm_output_name}.wasm').then({import_callback});
}})
}});
</script>
Expand Down

0 comments on commit 95bd9cc

Please sign in to comment.