Skip to content

Commit

Permalink
Revert "refactor: Unify the static server in bundler-mako and devServ…
Browse files Browse the repository at this point in the history
…er (#1468)" (#1556)

This reverts commit 895d294.
  • Loading branch information
stormslowly authored Sep 3, 2024
1 parent 8b79017 commit 025591a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
24 changes: 8 additions & 16 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ impl DevServer {
staticfile: hyper_staticfile::Static,
txws: broadcast::Sender<WsMessage>,
) -> Result<hyper::Response<Body>> {
debug!("> {} {}", req.method().to_string(), req.uri().path());

let mut path = req.uri().path().to_string();
let public_path = &context.config.public_path;
if !public_path.is_empty() && public_path.starts_with('/') && public_path != "/" {
Expand Down Expand Up @@ -173,16 +171,14 @@ impl DevServer {
// staticfile has 302 problems when modify tooooo fast in 1 second
// it will response 302 and we will get the old file
// TODO: fix the 302 problem?
if !context.config.write_to_disk {
if let Some(res) = context.get_static_content(path_without_slash_start) {
debug!("serve with context.get_static_content: {}", path);
if let Some(res) = context.get_static_content(path_without_slash_start) {
debug!("serve with context.get_static_content: {}", path);

return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
}
return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
}
// for cached dep
let abs_path = context
Expand All @@ -200,11 +196,7 @@ impl DevServer {
}

// for hmr files
debug!("< static file serve: {}", path);
let req = hyper::Request::builder()
.uri(path)
.body(hyper::Body::empty())
.unwrap();
debug!("serve with staticfile server: {}", path);
let res = staticfile.serve(req).await;
res.map_err(anyhow::Error::from)
}
Expand Down
26 changes: 18 additions & 8 deletions packages/bundler-mako/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,27 @@ exports.dev = async function (opts) {
logLevel: 'silent',
});
app.use('/__/hmr-ws', wsProxy);
app.use((req, res, next) => {
if (req.method !== 'GET' && req.method !== 'HEAD') {
return next();
}
if (req.accepts().join('').includes('html')) {
return next();

const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist');

function processReqURL(publicPath, reqURL) {
if (!publicPath.startsWith('/')) {
publicPath = '/' + publicPath;
}
wsProxy(req, res, () => {
return reqURL.startsWith(publicPath)
? reqURL.slice(publicPath.length - 1)
: 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));

if (process.env.SSU === 'true') {
// for ssu cache chunks
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 025591a

Please sign in to comment.