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
Improve Embed and CDN handling and fix a couple of related bugs #6261
Conversation
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-builds.s3.amazonaws.com/61deec0279f5dc613ee363ea7c3fcb0eca586b3a/gradio-4.0.2-py3-none-any.whl Install Gradio Python Client from this PR pip install "gradio-client @ git+https://github.com/gradio-app/gradio@61deec0279f5dc613ee363ea7c3fcb0eca586b3a#subdirectory=client/python" |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
Maintainers or the PR author can modify the PR title to modify this entry.
|
@aliabd you wanna review this? |
GRADIO_VERSION=$new_version pnpm build:cdn | ||
aws s3 cp gradio/templates/cdn "s3://gradio/${new_version}/" --recursive --region us-west-2 | ||
cp gradio/templates/cdn/index.html gradio/templates/frontend/share.html | ||
aws s3 cp gradio/templates/frontend "s3://gradio/${new_version}/" --recursive --region us-west-2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we use the same build for everything now, we only need to build once and we can upload the files to the server from the templates/frontend
dir. It contains everything we need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice
mode === "production:cdn" || | ||
mode === "production:local" || | ||
mode === "production:website"; | ||
const production = mode === "production"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont need all of these modes anymore.
const transformed_html = | ||
(bundle["index.html"].source as string).substring(0, script?.range[0]) + | ||
`<script type="module" crossorigin src="${cdn_base}/${version}/gradio.js"></script>` + | ||
(bundle["index.html"].source as string).substring( | ||
script?.range[1], | ||
source.length | ||
); | ||
|
||
const share_html_location = join(config.dir, "share.html"); | ||
writeFileSync(share_html_location, transformed_html); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for the share.html
rather than copying it over like we used to, we created it directly from the index.html
. What we are doing here is prepending the cdn_base
to the scripts src. This is the only time we need to do this.
return ` | ||
${make_script} | ||
make_script("${script}"); | ||
return `import("${script}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno why i chose the hard route previsouly.
The problem we always had was that anything that is requested from an html tag will always treat its own location as the base url from which to resolve files. With import
it will use the files own location.
This gradio.js
file will be request like: https://aws-etc.com/4.0.0/gradio.js
so when we request./assets/index-asdasdasd.js
it will be considered relative to that gradio.js
url.
When we add a script to the page the 'file' in that case is the HTML file, which isn't where the cdn assets live.
const share_html_location = join(config.dir, "share.html"); | ||
const share_html = readFileSync(share_html_location, "utf8"); | ||
const share_tree = parse(share_html); | ||
const node = Array.from( | ||
share_tree.querySelectorAll("link[rel=stylesheet]") | ||
).find((node) => /.*\/index(.*?)\.css/.test(node.attributes.href)); | ||
|
||
if (!node) return; | ||
const transformed_html = | ||
share_html.substring(0, node.range[0]) + | ||
share_html.substring(node.range[1], share_html.length); | ||
|
||
writeFileSync(share_html_location, transformed_html); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are just stripping out the css file for the share.html
, to prevent network errors. The CSS is loaded by the JS later anyway, so its no big deal.
js/app/src/css.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was actually the real problem. The CSS assets can only be loaded via a style tag, so we have the issues i mentioned earlier where the base URL was always the location of the HTML file. I fixed this by constructing a new URL based on the location of the file. This seems to work both for remote links (cdn) and for local (normal gradio).
let _res: (value?: unknown) => void; | ||
let pending = new Promise((res) => { | ||
_res = res; | ||
}); | ||
async function get_index(): Promise<void> { | ||
IndexComponent = (await import("./Index.svelte")).default; | ||
_res(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated to the build changed but fixes a race condition I noticed.
This can be tested locally. You need to build gradio: pnpm build Start a local server to serve the gradio files: pnpm preview:cdn-server Then change The cd into cd js/_website
pnpm dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @pngwn! tested locally and everything works great!
* fix embed * add changeset * fixes * remove final references to cdn * add changeset * add changeset --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Description
Please include a concise summary, in clear English, of the changes in this pull request. If it closes an issue, please mention it here.
Closes: #(issue)
🎯 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
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
You may need to run the linters:
bash scripts/format_backend.sh
andbash scripts/format_frontend.sh