Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dev server support "publicPath" #1398

Merged
merged 4 commits into from
Jul 22, 2024
Merged

Conversation

whyer11
Copy link
Contributor

@whyer11 whyer11 commented Jul 10, 2024

Fixed the issue where the development server was unable to correctly consume the publicPath configuration.

Close #1424
Close #1153
Close #1334

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能
    • 增加了处理请求URL的功能,根据指定的公共路径(public path)处理传入的请求,以提高静态文件服务的灵活性。
    • 引入了对请求处理失败的错误处理,提供BAD_REQUEST状态反馈。

Copy link
Contributor

coderabbitai bot commented Jul 10, 2024

Walkthrough

这些更改主要涉及在处理请求 URL 时引入了新的函数 process_req_urlprocessReqURL。这一功能在 mako crate 和 bundler-mako 包中进行了实现,使得在根据配置的 public_path 处理请求 URL 时更加灵活和一致,同时增强了错误处理能力,解决了开发环境中静态资源 MIME 类型不正确的问题。

Changes

文件路径 更改摘要
crates/mako/src/dev/mod.rs 修改了 DevServer 实现中的请求路径处理方式,新增了错误处理。
crates/mako/src/utils/mod.rs 新增了公共函数 process_req_url 及其单元测试,用于处理请求 URL。
packages/bundler-mako/index.js 新增了函数 processReqURL 用于调整请求 URL,增强了中间件功能。

Assessment against linked issues

Objective Addressed Explanation
解决静态资源 MIME 类型不正确的问题 (#1424)
dev server 支持 publicPath 配置 (#1153)

Poem

🐰
URL 的旅途,蜿蜒且漫长,
新增的函数,让路径更妥当。
代码如诗,逻辑如梦,
在这数据的海洋,我们共翱翔。
🌈


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9027639 and e55756d.

Files selected for processing (3)
  • crates/mako/src/dev/mod.rs (2 hunks)
  • crates/mako/src/utils/mod.rs (1 hunks)
  • packages/bundler-mako/index.js (1 hunks)
Additional comments not posted (2)
crates/mako/src/utils/mod.rs (1)

50-81: 测试用例: 通过

测试用例覆盖了多种情况,验证了 process_req_url 函数的行为。测试用例全面且正确。

packages/bundler-mako/index.js (1)

127-132: 中间件: 通过

中间件正确地使用了 processReqURL 来调整基于公共路径配置的请求URL。

crates/mako/src/utils/mod.rs Outdated Show resolved Hide resolved
crates/mako/src/dev/mod.rs Outdated Show resolved Hide resolved
packages/bundler-mako/index.js Outdated Show resolved Hide resolved
@whyer11 whyer11 linked an issue Jul 10, 2024 that may be closed by this pull request
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e55756d and 8630d60.

Files selected for processing (2)
  • crates/mako/src/dev/mod.rs (2 hunks)
  • crates/mako/src/utils/mod.rs (1 hunks)
Additional comments not posted (4)
crates/mako/src/utils/mod.rs (2)

36-42: 功能实现良好!

process_req_url 函数正确地处理了基于 public_path 的请求 URL,并且考虑了各种情况。


44-79: 单元测试覆盖全面!

单元测试涵盖了各种场景,确保了 process_req_url 函数的可靠性。

crates/mako/src/dev/mod.rs (2)

127-139: 功能集成良好!

DevServer::handle_requests 中正确地集成了 process_req_url 函数,并且适当地处理了错误。


130-138: 错误处理适当!

process_req_url 失败时,错误处理返回 BAD_REQUEST 响应,确保了无效路径的正确管理。

@whyer11 whyer11 force-pushed the fix/devserver_support_publicpath branch from 8630d60 to b6dd925 Compare July 10, 2024 12:58
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8630d60 and b6dd925.

Files selected for processing (3)
  • crates/mako/src/dev/mod.rs (2 hunks)
  • crates/mako/src/utils/mod.rs (1 hunks)
  • packages/bundler-mako/index.js (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • crates/mako/src/dev/mod.rs
  • crates/mako/src/utils/mod.rs
  • packages/bundler-mako/index.js

packages/bundler-mako/index.js Outdated Show resolved Hide resolved
@whyer11 whyer11 force-pushed the fix/devserver_support_publicpath branch from b6dd925 to da6d209 Compare July 12, 2024 02:56
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b6dd925 and da6d209.

Files selected for processing (3)
  • crates/mako/src/dev/mod.rs (2 hunks)
  • crates/mako/src/utils/mod.rs (1 hunks)
  • packages/bundler-mako/index.js (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • crates/mako/src/utils/mod.rs
  • packages/bundler-mako/index.js
Additional comments not posted (1)
crates/mako/src/dev/mod.rs (1)

127-139: 建议改进: 增加错误处理

在调用 process_req_url 时,建议增加错误处理,以防止潜在的路径处理错误导致服务器崩溃。

let path = match process_req_url(public_path, &path) {
    Ok(p) => p,
    Err(e) => {
        eprintln!("Error processing request URL: {:?}", e);
        return Ok(hyper::Response::builder()
            .status(hyper::StatusCode::BAD_REQUEST)
            .body(hyper::Body::from("Bad Request"))
            .unwrap());
    }
};

whyer11

This comment was marked as abuse.

@whyer11 whyer11 requested a review from sorrycc July 17, 2024 05:45
Copy link
Member

@sorrycc sorrycc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重新看了下 pr,感觉和我的理解还是有些不同。

1、mod.rs 里只在 public_path 是以 / 开头时才做 process_req_url 的处理
2、基于 1,process_req_url 很多边界场景就不需要处理了,包括中文处理的用例应该是不需要的
3、packages/bundler-mako/index.js 里的处理感觉增加了复杂性,好像没有必要的,或者可以列下在哪个具体场景下是需要在这边处理而不能放到 mako/dev/mod.rs 里处理

@sorrycc
Copy link
Member

sorrycc commented Jul 19, 2024

重新看了下 pr,感觉和我的理解还是有些不同。

1、mod.rs 里只在 public_path 是以 / 开头时才做 process_req_url 的处理 2、基于 1,process_req_url 很多边界场景就不需要处理了,包括中文处理的用例应该是不需要的 3、packages/bundler-mako/index.js 里的处理感觉增加了复杂性,好像没有必要的,或者可以列下在哪个具体场景下是需要在这边处理而不能放到 mako/dev/mod.rs 里处理

@stormslowly 讨论了下,按这个逻辑来。至于 writeToDisk 的场景,后续增加 compiling 状态来决定是否 pending request 来解决。

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between da6d209 and d58d293.

Files selected for processing (1)
  • packages/bundler-mako/index.js (1 hunks)

Comment on lines 114 to 123
function processReqURL(publicPath, reqURL) {
if (!publicPath.startsWith('/')) {
publicPath = '/' + publicPath;
}
if (reqURL.startsWith(publicPath)) {
return reqURL.slice(publicPath.length - 1);
} else {
return reqURL;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改进: 优化 processReqURL 函数

目前的逻辑可以进一步简化和优化,以提高代码的可读性和处理边界情况的能力。

function processReqURL(publicPath, reqURL) {
  if (!publicPath.startsWith('/')) {
    publicPath = '/' + publicPath;
  }
  publicPath = publicPath.replace(/\/+$/, '') + '/';
  if (reqURL.startsWith(publicPath)) {
    return reqURL.slice(publicPath.length - 1);
  }
  return reqURL;
}

这样可以确保 publicPath 总是以一个斜杠开头和结尾,并且处理了多个结尾斜杠的情况。

Comment on lines 124 to 128
if (opts.config.publicPath) {
app.use((req, res, next) => {
req.url = processReqURL(opts.config.publicPath, req.url);
next();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改进: 提高中间件的可读性

目前的中间件逻辑可以通过解构 opts.config 来提高可读性。

const { publicPath } = opts.config;
if (publicPath) {
  app.use((req, res, next) => {
    req.url = processReqURL(publicPath, req.url);
    next();
  });
}

这样可以使代码更加简洁和易读。

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d58d293 and 9d9040a.

Files selected for processing (2)
  • crates/mako/src/dev/mod.rs (2 hunks)
  • crates/mako/src/utils/mod.rs (1 hunks)
Additional comments not posted (1)
crates/mako/src/utils/mod.rs (1)

44-62: 测试模块通过!

测试模块覆盖了不同的情况,确保函数按预期运行。测试编写良好且全面。

Comment on lines +36 to +41
pub fn process_req_url(public_path: &str, req_url: &str) -> Result<String> {
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改进: 优化字符串处理逻辑

目前的逻辑虽然正确,但可以使用标准库提供的方法来简化字符串的处理。例如,可以使用 strip_prefixstrip_suffix 方法来处理路径。

pub fn process_req_url(public_path: &str, req_url: &str) -> Result<String> {
    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())
}

Comment on lines +127 to +138
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 != "/" {
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());
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改进: 增加错误处理

在调用 process_req_url 时,建议增加错误处理,以防止潜在的路径处理错误导致服务器崩溃。

let path = match process_req_url(public_path, &path) {
    Ok(p) => p,
    Err(e) => {
        eprintln!("Error processing request URL: {:?}", e);
        return Ok(hyper::Response::builder()
            .status(hyper::StatusCode::BAD_REQUEST)
            .body(hyper::Body::from("Bad Request"))
            .unwrap());
    }
};

@sorrycc sorrycc merged commit 3f20b78 into master Jul 22, 2024
8 checks passed
@sorrycc sorrycc deleted the fix/devserver_support_publicpath branch July 22, 2024 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants