Skip to content

Commit 6cfe8e1

Browse files
committed
perf: ⚡️ inlined wasm load script to reduce full requests
It's now inlined directly into the HTML at server startup. BREAKING CHANGE: `js_init` no longer an option in actix web integration
1 parent 7c6f697 commit 6cfe8e1

File tree

7 files changed

+12
-24
lines changed

7 files changed

+12
-24
lines changed

examples/basic/index.html

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<!-- Importing this runs Perseus -->
8-
<script type="module" src="/.perseus/main.js" defer></script>
97
</head>
108
<body>
119
<div id="root"></div>

examples/cli/.perseus/main.js

-6
This file was deleted.

examples/cli/.perseus/server/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ async fn main() -> std::io::Result<()> {
2323
Options {
2424
index: "../index.html".to_string(), // The user must define their own `index.html` file
2525
js_bundle: "dist/pkg/perseus_cli_builder.js".to_string(),
26-
js_init: "main.js".to_string(),
2726
// Our crate has the same name, so this will be predictable
2827
wasm_bundle: "dist/pkg/perseus_cli_builder_bg.wasm".to_string(),
2928
templates_map: get_templates_map(),

examples/cli/index.html

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<!-- Importing this runs Perseus -->
8-
<script type="module" src="/.perseus/main.js" defer></script>
97
</head>
108
<body>
119
<div id="root"></div>

examples/i18n/index.html

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Perseus Example – i18n</title>
8-
<!-- Importing this runs Perseus -->
9-
<script type="module" src="/.perseus/main.js" defer></script>
108
</head>
119
<body>
1210
<div id="root"></div>

examples/showcase/index.html

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Perseus Example – Showcase</title>
8-
<!-- Importing this runs Perseus -->
9-
<script type="module" src="/.perseus/main.js" defer></script>
108
</head>
119
<body>
1210
<div id="root"></div>

packages/perseus-actix-web/src/configurer.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ use std::fs;
1414
pub struct Options {
1515
/// The location on the filesystem of your JavaScript bundle.
1616
pub js_bundle: String,
17-
/// The locales on the filesystem of the file that will invoke your JavaScript bundle. This should have something like `init()` in
18-
/// it.
19-
pub js_init: String,
2017
/// The location on the filesystem of your Wasm bundle.
2118
pub wasm_bundle: String,
2219
/// The location on the filesystem of your `index.html` file that includes the JS bundle.
@@ -43,9 +40,6 @@ async fn render_conf(
4340
async fn js_bundle(opts: web::Data<Options>) -> std::io::Result<NamedFile> {
4441
NamedFile::open(&opts.js_bundle)
4542
}
46-
async fn js_init(opts: web::Data<Options>) -> std::io::Result<NamedFile> {
47-
NamedFile::open(&opts.js_init)
48-
}
4943
async fn wasm_bundle(opts: web::Data<Options>) -> std::io::Result<NamedFile> {
5044
NamedFile::open(&opts.wasm_bundle)
5145
}
@@ -62,13 +56,23 @@ pub async fn configurer<C: ConfigManager + 'static, T: TranslationsManager + 'st
6256
// Get the index file and inject the render configuration into ahead of time
6357
// We do this by injecting a script that defines the render config as a global variable, which we put just before the close of the head
6458
// We also inject a delimiter comment that will be used to wall off the constant document head from the interpolated document head
59+
// We also inject a script to load the Wasm bundle (avoids extra trips)
6560
let index_file = fs::read_to_string(&opts.index).expect("Couldn't get HTML index file!");
61+
let load_script = r#"<script type="module">
62+
import init, { run } from "/.perseus/bundle.js";
63+
async function main() {
64+
await init("/.perseus/bundle.wasm");
65+
run();
66+
}
67+
main();
68+
</script>"#;
6669
let index_with_render_cfg = index_file.replace(
6770
"</head>",
6871
// It's safe to assume that something we just deserialized will serialize again in this case
6972
&format!(
70-
"<script>window.__PERSEUS_RENDER_CFG = '{}';</script>\n<!--PERSEUS_INTERPOLATED_HEAD_BEGINS-->\n</head>",
71-
serde_json::to_string(&render_cfg).unwrap()
73+
"<script>window.__PERSEUS_RENDER_CFG = '{}';</script>\n{}\n<!--PERSEUS_INTERPOLATED_HEAD_BEGINS-->\n</head>",
74+
serde_json::to_string(&render_cfg).unwrap(),
75+
load_script
7276
),
7377
);
7478

@@ -83,7 +87,6 @@ pub async fn configurer<C: ConfigManager + 'static, T: TranslationsManager + 'st
8387
// TODO chunk JS and Wasm bundles
8488
// These allow getting the basic app code (not including the static data)
8589
// This contains everything in the spirit of a pseudo-SPA
86-
.route("/.perseus/main.js", web::get().to(js_init))
8790
.route("/.perseus/bundle.js", web::get().to(js_bundle))
8891
.route("/.perseus/bundle.wasm", web::get().to(wasm_bundle))
8992
.route("/.perseus/render_conf.json", web::get().to(render_conf))

0 commit comments

Comments
 (0)