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

Improve Embed and CDN handling and fix a couple of related bugs #6261

Merged
merged 7 commits into from Nov 2, 2023

Conversation

pngwn
Copy link
Member

@pngwn pngwn commented Nov 2, 2023

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

  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 Nov 2, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

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"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Nov 2, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
@gradio/preview minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Improve Embed and CDN handling and fix a couple of related bugs

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.

@abidlabs
Copy link
Member

abidlabs commented Nov 2, 2023

@aliabd you wanna review this?

@abidlabs abidlabs requested a review from aliabd November 2, 2023 19:31
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
Copy link
Member Author

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.

Copy link
Collaborator

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";
Copy link
Member Author

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.

Comment on lines +51 to +60
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);
Copy link
Member Author

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}");
Copy link
Member Author

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.

Comment on lines +154 to +166
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);
Copy link
Member Author

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.

Copy link
Member Author

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).

Comment on lines +24 to +32
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();
}

Copy link
Member Author

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.

@pngwn
Copy link
Member Author

pngwn commented Nov 2, 2023

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 js/_website/src/routes/+layout.svelte Line 77 to "http://localhost:4321/gradio.js"

The cd into js/_website and start the dev server:

cd js/_website
pnpm dev

@pngwn pngwn changed the title fix embed Improve Embed and CDN handling and fix a couple of related bugs Nov 2, 2023
Copy link
Collaborator

@aliabd aliabd left a 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!

@pngwn pngwn merged commit 8bbeca0 into main Nov 2, 2023
20 checks passed
@pngwn pngwn deleted the embed-v4-2 branch November 2, 2023 22:29
@pngwn pngwn mentioned this pull request Nov 2, 2023
pngwn added a commit that referenced this pull request Nov 3, 2023
* 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>
whitphx added a commit that referenced this pull request Mar 27, 2024
…roduced in #6261 (#7852)

* Revert the minify setting in vite.config.js which was mistakingly introduced in #6261

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
@pngwn pngwn mentioned this pull request Mar 27, 2024
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.

None yet

5 participants