From da6d20958bdc6c4b57628f542472ad93b92e367f Mon Sep 17 00:00:00 2001 From: "huanyu.why" Date: Wed, 10 Jul 2024 20:00:47 +0800 Subject: [PATCH 1/3] fix: the development server was unable to correctly consume the "publicPath" configuration (#1334) --- crates/mako/src/dev/mod.rs | 18 +++++++++++--- crates/mako/src/utils/mod.rs | 45 ++++++++++++++++++++++++++++++++++ packages/bundler-mako/index.js | 16 ++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/crates/mako/src/dev/mod.rs b/crates/mako/src/dev/mod.rs index f54233a9e..4b23e3a07 100644 --- a/crates/mako/src/dev/mod.rs +++ b/crates/mako/src/dev/mod.rs @@ -21,7 +21,7 @@ use {hyper, hyper_staticfile, hyper_tungstenite, open}; use crate::compiler::{Compiler, Context}; use crate::plugin::{PluginGenerateEndParams, PluginGenerateStats}; -use crate::utils::tokio_runtime; +use crate::utils::{process_req_url, tokio_runtime}; pub struct DevServer { root: PathBuf, @@ -124,7 +124,19 @@ impl DevServer { staticfile: hyper_staticfile::Static, txws: broadcast::Sender, ) -> Result> { - let path = req.uri().path(); + let mut path = req.uri().path().to_string(); + let public_path = &context.config.public_path; + if !public_path.is_empty() { + path = match process_req_url(public_path, &path) { + Ok(p) => p, + Err(_) => { + return Ok(hyper::Response::builder() + .status(hyper::StatusCode::BAD_REQUEST) + .body(hyper::Body::from("Bad Request")) + .unwrap()); + } + }; + } let path_without_slash_start = path.trim_start_matches('/'); let not_found_response = || { hyper::Response::builder() @@ -132,7 +144,7 @@ impl DevServer { .body(hyper::Body::empty()) .unwrap() }; - match path { + match path.as_str() { "/__/hmr-ws" => { if hyper_tungstenite::is_upgrade_request(&req) { debug!("new websocket connection"); diff --git a/crates/mako/src/utils/mod.rs b/crates/mako/src/utils/mod.rs index 787a71a6a..7a1e1debf 100644 --- a/crates/mako/src/utils/mod.rs +++ b/crates/mako/src/utils/mod.rs @@ -32,3 +32,48 @@ impl ParseRegex for Option { }) } } + +pub fn process_req_url(public_path: &str, req_url: &str) -> Result { + let public_path = format!("/{}/", public_path.trim_matches('/')); + if req_url.starts_with(&public_path) { + return Ok(req_url[public_path.len() - 1..].to_string()); + } + Ok(req_url.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_process_req_url() { + assert_eq!( + process_req_url("/public/", "/public/index.html").unwrap(), + "/index.html" + ); + assert_eq!( + process_req_url("public/", "/public/index.html").unwrap(), + "/index.html" + ); + assert_eq!( + process_req_url("/public/foo/", "/public/foo/index.html").unwrap(), + "/index.html" + ); + assert_eq!( + process_req_url("public/foo/", "/public/foo/index.html").unwrap(), + "/index.html" + ); + assert_eq!(process_req_url("/", "/index.html").unwrap(), "/index.html"); + assert_eq!( + process_req_url("/#/", "/#/index.html").unwrap(), + "/index.html" + ); + assert_eq!( + process_req_url("/公共路径/", "/公共路径/index.html").unwrap(), + "/index.html" + ); + assert_eq!( + process_req_url("公共路径/", "/公共路径/index.html").unwrap(), + "/index.html" + ); + } +} diff --git a/packages/bundler-mako/index.js b/packages/bundler-mako/index.js index 47588543f..30524ac6a 100644 --- a/packages/bundler-mako/index.js +++ b/packages/bundler-mako/index.js @@ -111,6 +111,22 @@ exports.dev = async function (opts) { const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist'); + function processReqURL(publicPath, reqURL) { + if (!publicPath.startsWith('/')) { + publicPath = '/' + publicPath; + } + if (reqURL.startsWith(publicPath)) { + return reqURL.slice(publicPath.length - 1); + } else { + return reqURL; + } + } + if (opts.config.publicPath) { + app.use((req, res, next) => { + req.url = processReqURL(opts.config.publicPath, req.url); + next(); + }); + } // serve dist files app.use(express.static(outputPath)); From ca9def2702ea0efd8572477f2408e90ebb11b7e9 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 22 Jul 2024 16:39:11 +0800 Subject: [PATCH 2/3] chore: improve --- crates/mako/src/dev/mod.rs | 2 +- crates/mako/src/utils/mod.rs | 16 ---------------- packages/bundler-mako/index.js | 16 ---------------- 3 files changed, 1 insertion(+), 33 deletions(-) diff --git a/crates/mako/src/dev/mod.rs b/crates/mako/src/dev/mod.rs index 4b23e3a07..2c21ed42a 100644 --- a/crates/mako/src/dev/mod.rs +++ b/crates/mako/src/dev/mod.rs @@ -126,7 +126,7 @@ impl DevServer { ) -> Result> { let mut path = req.uri().path().to_string(); let public_path = &context.config.public_path; - if !public_path.is_empty() { + if !public_path.is_empty() && public_path.starts_with('/') { path = match process_req_url(public_path, &path) { Ok(p) => p, Err(_) => { diff --git a/crates/mako/src/utils/mod.rs b/crates/mako/src/utils/mod.rs index 7a1e1debf..85920f13f 100644 --- a/crates/mako/src/utils/mod.rs +++ b/crates/mako/src/utils/mod.rs @@ -50,30 +50,14 @@ mod tests { process_req_url("/public/", "/public/index.html").unwrap(), "/index.html" ); - assert_eq!( - process_req_url("public/", "/public/index.html").unwrap(), - "/index.html" - ); assert_eq!( process_req_url("/public/foo/", "/public/foo/index.html").unwrap(), "/index.html" ); - assert_eq!( - process_req_url("public/foo/", "/public/foo/index.html").unwrap(), - "/index.html" - ); assert_eq!(process_req_url("/", "/index.html").unwrap(), "/index.html"); assert_eq!( process_req_url("/#/", "/#/index.html").unwrap(), "/index.html" ); - assert_eq!( - process_req_url("/公共路径/", "/公共路径/index.html").unwrap(), - "/index.html" - ); - assert_eq!( - process_req_url("公共路径/", "/公共路径/index.html").unwrap(), - "/index.html" - ); } } diff --git a/packages/bundler-mako/index.js b/packages/bundler-mako/index.js index deaabddb8..27ea2bfc4 100644 --- a/packages/bundler-mako/index.js +++ b/packages/bundler-mako/index.js @@ -111,22 +111,6 @@ exports.dev = async function (opts) { const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist'); - function processReqURL(publicPath, reqURL) { - if (!publicPath.startsWith('/')) { - publicPath = '/' + publicPath; - } - if (reqURL.startsWith(publicPath)) { - return reqURL.slice(publicPath.length - 1); - } else { - return reqURL; - } - } - if (opts.config.publicPath) { - app.use((req, res, next) => { - req.url = processReqURL(opts.config.publicPath, req.url); - next(); - }); - } // serve dist files app.use(express.static(outputPath)); From 9d9040a1a7f9e6eb3cd207e2b636442e7bd9daa7 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 22 Jul 2024 16:44:55 +0800 Subject: [PATCH 3/3] chore: update --- crates/mako/src/dev/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mako/src/dev/mod.rs b/crates/mako/src/dev/mod.rs index 2c21ed42a..141a97c59 100644 --- a/crates/mako/src/dev/mod.rs +++ b/crates/mako/src/dev/mod.rs @@ -126,7 +126,7 @@ impl DevServer { ) -> Result> { let mut path = req.uri().path().to_string(); let public_path = &context.config.public_path; - if !public_path.is_empty() && public_path.starts_with('/') { + if !public_path.is_empty() && public_path.starts_with('/') && public_path != "/" { path = match process_req_url(public_path, &path) { Ok(p) => p, Err(_) => {