@@ -14,9 +14,6 @@ use std::fs;
14
14
pub struct Options {
15
15
/// The location on the filesystem of your JavaScript bundle.
16
16
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 ,
20
17
/// The location on the filesystem of your Wasm bundle.
21
18
pub wasm_bundle : String ,
22
19
/// The location on the filesystem of your `index.html` file that includes the JS bundle.
@@ -43,9 +40,6 @@ async fn render_conf(
43
40
async fn js_bundle ( opts : web:: Data < Options > ) -> std:: io:: Result < NamedFile > {
44
41
NamedFile :: open ( & opts. js_bundle )
45
42
}
46
- async fn js_init ( opts : web:: Data < Options > ) -> std:: io:: Result < NamedFile > {
47
- NamedFile :: open ( & opts. js_init )
48
- }
49
43
async fn wasm_bundle ( opts : web:: Data < Options > ) -> std:: io:: Result < NamedFile > {
50
44
NamedFile :: open ( & opts. wasm_bundle )
51
45
}
@@ -62,13 +56,23 @@ pub async fn configurer<C: ConfigManager + 'static, T: TranslationsManager + 'st
62
56
// Get the index file and inject the render configuration into ahead of time
63
57
// 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
64
58
// 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)
65
60
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>"# ;
66
69
let index_with_render_cfg = index_file. replace (
67
70
"</head>" ,
68
71
// It's safe to assume that something we just deserialized will serialize again in this case
69
72
& 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
72
76
) ,
73
77
) ;
74
78
@@ -83,7 +87,6 @@ pub async fn configurer<C: ConfigManager + 'static, T: TranslationsManager + 'st
83
87
// TODO chunk JS and Wasm bundles
84
88
// These allow getting the basic app code (not including the static data)
85
89
// This contains everything in the spirit of a pseudo-SPA
86
- . route ( "/.perseus/main.js" , web:: get ( ) . to ( js_init) )
87
90
. route ( "/.perseus/bundle.js" , web:: get ( ) . to ( js_bundle) )
88
91
. route ( "/.perseus/bundle.wasm" , web:: get ( ) . to ( wasm_bundle) )
89
92
. route ( "/.perseus/render_conf.json" , web:: get ( ) . to ( render_conf) )
0 commit comments