Skip to content

Commit 2d1ca2d

Browse files
committed
perf(i18n): added experimental wasm caching
This should make locale redirection better for most users, though it's feature-gated for now, because we need real-world metrics.
1 parent 832e269 commit 2d1ca2d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/perseus/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ standalone = []
4949
# This feature enables Sycamore hydration by default (Sycamore hydration feature is always activated though)
5050
# This is not enabled by default due to some remaining bugs (also, default features in Perseus can't be disabled without altering `.perseus/`)
5151
hydrate = []
52+
# This feature enables the preloading of the Wasm bundle for locale redirections, which in theory improves UX
53+
# For now, this is experimental until it can be tested in the wild (local testing of this is extremely difficult for UX, we need real world metrics)
54+
preload-wasm-on-redirect = []

packages/perseus/src/html_shell.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub struct HtmlShell<'a> {
3232
content: Cow<'a, str>,
3333
/// The ID of the element into which we'll interpolate content.
3434
root_id: String,
35+
/// The path prefix to use.
36+
#[cfg_attr(not(feature = "preload-wasm-on-redirect"), allow(dead_code))]
37+
path_prefix: String,
3538
}
3639
impl<'a> HtmlShell<'a> {
3740
/// Initializes the HTML shell by interpolating necessary scripts into it and adding the render configuration.
@@ -87,6 +90,7 @@ impl<'a> HtmlShell<'a> {
8790
scripts_after_boundary: Vec::new(),
8891
content: "".into(),
8992
root_id: root_id.into(),
93+
path_prefix: path_prefix.into(),
9094
}
9195
}
9296

@@ -155,8 +159,19 @@ impl<'a> HtmlShell<'a> {
155159

156160
self.head_after_boundary.push(dumb_redirect.into());
157161
self.scripts_after_boundary.push(js_redirect.into());
158-
// TODO Interpolate a preload of the Wasm bundle after the interpolation boundary
159-
// TODO Do we need any content in here?
162+
#[cfg(feature = "preload-wasm-on-redirect")]
163+
{
164+
// Interpolate a preload of the Wasm bundle
165+
// This forces the browser to get the bundle before loading the page, which makes the time users spend on a blank screen much shorter
166+
// We have no leading `/` here because of the `<base>` interpolation
167+
// Note that this has to come before the code that actually loads the Wasm bundle
168+
// The aim of this is to make the time loading increase so that the time blanking decreases
169+
let wasm_preload = format!(
170+
r#"<link rel="preload" href="{path_prefix}/.perseus/bundle.wasm" as="fetch" />"#,
171+
path_prefix = self.path_prefix
172+
);
173+
self.head_before_boundary.push(wasm_preload.into());
174+
}
160175

161176
self
162177
}

0 commit comments

Comments
 (0)