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

Add ETag to /custom_component route to control browser caching #8170

Merged
merged 3 commits into from
Apr 30, 2024

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Apr 29, 2024

Description

Setting an ETag in the response of /custom_component that's equal to the hash of the requested file. I think this is preferable than setting a max-age because it should minimize the chance of a stale cache. During development, it's possible to build frequently to see if latest changes work outside the development server ( I do that atleast) so the max-age would be hard to set in this case.

Closes: #8168

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 29, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/6fff2d0cc8cabcfb835df9dbc2e71cf2cc4e6ff8/gradio-4.28.3-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@6fff2d0cc8cabcfb835df9dbc2e71cf2cc4e6ff8#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 29, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Add ETag to /custom_component route to control browser caching

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

key = f"{id}-{type}-{file_name}"

if key not in app.custom_component_hashes:
app.custom_component_hashes[key] = hashlib.md5(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We only need to compute the hash once as changing the frontend files is not supported after the server starts. This route is not used for dev mode.

@freddyaboulton freddyaboulton added the t: fix A change that implements a fix label Apr 29, 2024
@freddyaboulton freddyaboulton marked this pull request as ready for review April 29, 2024 21:00
Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

I think this is preferable than setting a max-age because it should minimize the chance of a stale cache.

This makes sense, cache is always busted when devtools are open but that cant be guaranteed, just an old habit of mine.

Looks good. Thanks for taking care of this @freddyaboulton!

@freddyaboulton
Copy link
Collaborator Author

Thanks for the review @pngwn !!

@freddyaboulton freddyaboulton merged commit 08b4e61 into main Apr 30, 2024
10 checks passed
@freddyaboulton freddyaboulton deleted the 8186-custom-component-browser-caching branch April 30, 2024 14:22
@pngwn pngwn mentioned this pull request Apr 30, 2024
dawoodkhan82 pushed a commit that referenced this pull request May 6, 2024
* Add code

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t: fix A change that implements a fix
Projects
None yet
3 participants