diff --git a/crates/metassr-build/src/server/renderer/head.rs b/crates/metassr-build/src/server/renderer/head.rs index 1ec75a3..846eaac 100644 --- a/crates/metassr-build/src/server/renderer/head.rs +++ b/crates/metassr-build/src/server/renderer/head.rs @@ -54,36 +54,7 @@ impl HeadRenderer { pub fn render(&mut self, bundler: bool) -> Result { if !IS_HEAD_SCRIPT_LOADED.lock().unwrap().is_loaded() { if bundler { - let script = format!( - r#" -import Head from "{}" -import {{ renderToString }} from "react-dom/server" -import React from "react" - -export function render_head() {{ - return renderToString(); -}} - - "#, - self.path.canonicalize()?.display() - ); - - let path = self.cache_dir.insert("head.js", script.as_bytes())?; - - let name = PathBuf::from(path.clone().file_name().unwrap()) - .with_extension("") - .to_str() - .unwrap() - .to_string(); - - let fullpath = path.canonicalize()?.to_str().unwrap().to_string(); - - let target = HashMap::from([(name, fullpath)]); - - if let Err(e) = WebBundler::new(&target, &self.cache_dir.dir_path()).exec() { - return Err(anyhow!("Cannot bundling head: {e}")); - } - + self.bundle()?; // TODO: remove this line sleep(Duration::from_millis(500)); } @@ -99,4 +70,35 @@ export function render_head() {{ Ok(out) => Ok(out), } } + + fn bundle(&mut self) -> Result<()> { + let script = format!( + r#" +import Head from "{}" +import {{ renderToString }} from "react-dom/server" +import React from "react" + +export function render_head() {{ + return renderToString(); +}} + + "#, + self.path.canonicalize()?.display() + ); + + let path = self.cache_dir.insert("head.js", script.as_bytes())?; + let name = PathBuf::from(path.clone().file_name().unwrap()) + .with_extension("") + .to_str() + .unwrap() + .to_string(); + let fullpath = path.canonicalize()?.to_str().unwrap().to_string(); + + let target = HashMap::from([(name, fullpath)]); + + if let Err(e) = WebBundler::new(&target, &self.cache_dir.dir_path()).exec() { + return Err(anyhow!("Cannot bundling head: {e}")); + } + Ok(()) + } }