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: devserver response header add cacheControl no-cache #1692

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

Jinbao1001
Copy link
Member

@Jinbao1001 Jinbao1001 commented Nov 15, 2024

Ant Design reported that refreshing the browser would fetch old resources, and disabling browser cache is mandatory for normal development. Therefore, we force the browser to disable cache.

Summary by CodeRabbit

  • 新功能
    • 在响应静态内容时,添加了 CACHE_CONTROL: no-cacheACCESS_CONTROL_ALLOW_ORIGIN: * 头部,优化了客户端缓存行为和跨域请求处理。
  • 文档
    • 更新了头部导入语句,以支持新的缓存控制和跨域请求功能。

Copy link
Contributor

coderabbitai bot commented Nov 15, 2024

Walkthrough

此拉取请求对 crates/mako/src/dev.rs 文件中的 DevServer 实现进行了修改。主要更改是在 handle_requests 方法中添加了 CACHE_CONTROLACCESS_CONTROL_ALLOW_ORIGIN 头部,具体是在构建静态内容响应时加入 CACHE_CONTROL: no-cacheACCESS_CONTROL_ALLOW_ORIGIN: *。此外,更新了头部的导入语句,以便同时导入这两个新头部。此更改旨在增强 HTTP 响应处理,影响客户端的缓存行为和跨域请求。

Changes

文件路径 更改摘要
crates/mako/src/dev.rs handle_requests 方法中添加 CACHE_CONTROLACCESS_CONTROL_ALLOW_ORIGIN 头部,更新头部导入语句以包含这两个新头部。

Possibly related PRs

Suggested reviewers

  • stormslowly

Poem

在兔子的小窝里,
新增头部真奇妙,
缓存控制来相伴,
响应更为妥帖好。
兔子欢跳庆变化,
代码更新乐无边。 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7311f34 and 1a984f8.

📒 Files selected for processing (1)
  • crates/mako/src/dev.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/dev.rs

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

Walkthrough

This pull request addresses an issue reported by Ant Design where refreshing the browser would fetch outdated resources. To ensure proper development, the browser cache is disabled by adding a Cache-Control: no-cache header to the dev server responses.

Changes

File Summary
crates/mako/src/dev.rs Added CACHE_CONTROL to the headers and set it to no-cache to prevent browser caching of resources during development.

@@ -179,6 +179,7 @@ impl DevServer {

return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CACHE_CONTROL, "no-cache")

Choose a reason for hiding this comment

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

Ensure that adding the Cache-Control: no-cache header does not interfere with other caching mechanisms or headers that might be required for different environments or use cases. Consider making this configurable if necessary.

@Jinbao1001 Jinbao1001 requested a review from sorrycc November 15, 2024 06:42
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

🧹 Outside diff range and nitpick comments (1)
crates/mako/src/dev.rs (1)

182-182: 缓存控制头部添加正确,建议扩展应用范围!

当前实现正确地在静态内容响应中添加了 no-cache 缓存控制,这有效解决了开发过程中的资源缓存问题。

不过建议也为缓存的依赖响应添加相同的缓存控制头部,以确保一致的缓存行为。

建议在处理缓存依赖的响应时也添加相同的头部:

 return std::fs::read(abs_path).map_or(Ok(not_found_response()), |bytes| {
     Ok(hyper::Response::builder()
         .status(hyper::StatusCode::OK)
+        .header(CACHE_CONTROL, "no-cache")
         .header(CONTENT_TYPE, content_type)
         .body(hyper::Body::from(bytes))
         .unwrap())
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8b38e19 and 7311f34.

📒 Files selected for processing (1)
  • crates/mako/src/dev.rs (2 hunks)
🔇 Additional comments (1)
crates/mako/src/dev.rs (1)

13-13: 导入声明修改正确!

引入 CACHE_CONTROL 头部常量的方式符合 Rust 的导入语法规范,并且与现有的 CONTENT_TYPE 导入保持一致。

@stormslowly
Copy link
Member

@Jinbao1001 是不是也把跨域的头给加上
dev 的标配

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.28%. Comparing base (8b38e19) to head (1a984f8).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1692   +/-   ##
=======================================
  Coverage   55.28%   55.28%           
=======================================
  Files         175      175           
  Lines       17696    17696           
=======================================
  Hits         9783     9783           
  Misses       7913     7913           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@stormslowly stormslowly merged commit d9017fa into master Nov 21, 2024
21 checks passed
@stormslowly stormslowly deleted the fix/browser_cache branch November 21, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants