Skip to content

Commit a77877f

Browse files
baojiantingclasstimetablegradio-pr-botabidlabs
authored
First time loading performance optimization (#8571)
* performance: add gradio_api_info * format code * fix ts check * fix ts check * add changeset * add changeset * format globals.d.ts --------- Co-authored-by: jianting.bjt <[email protected]> Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Abubakar Abid <[email protected]>
1 parent a1c21cb commit a77877f

File tree

7 files changed

+47
-27
lines changed

7 files changed

+47
-27
lines changed

.changeset/stale-crabs-return.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@gradio/app": patch
3+
"@gradio/client": patch
4+
"gradio": patch
5+
---
6+
7+
feat:First time loading performance optimization

client/js/src/globals.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Config } from "./types";
1+
import { ApiData, ApiInfo, Config } from "./types";
22

33
declare global {
44
interface Window {
55
__gradio_mode__: "app" | "website";
66
gradio_config: Config;
7+
gradio_api_info: ApiInfo<ApiData> | { api: ApiInfo<ApiData> };
78
__is_colab__: boolean;
89
__gradio_space__: string | null;
910
}

client/js/src/utils/view_api.ts

+24-23
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,33 @@ export async function view_api(this: Client): Promise<any> {
2626

2727
try {
2828
let response: Response;
29-
30-
if (semiver(config?.version || "2.0.0", "3.30") < 0) {
31-
response = await this.fetch(SPACE_FETCHER_URL, {
32-
method: "POST",
33-
body: JSON.stringify({
34-
serialize: false,
35-
config: JSON.stringify(config)
36-
}),
37-
headers,
38-
credentials: "include"
39-
});
29+
let api_info: ApiInfo<ApiData> | { api: ApiInfo<ApiData> };
30+
if (typeof window !== "undefined" && window.gradio_api_info) {
31+
api_info = window.gradio_api_info;
4032
} else {
41-
const url = join_urls(config.root, API_INFO_URL);
42-
response = await this.fetch(url, {
43-
headers,
44-
credentials: "include"
45-
});
46-
}
33+
if (semiver(config?.version || "2.0.0", "3.30") < 0) {
34+
response = await this.fetch(SPACE_FETCHER_URL, {
35+
method: "POST",
36+
body: JSON.stringify({
37+
serialize: false,
38+
config: JSON.stringify(config)
39+
}),
40+
headers,
41+
credentials: "include"
42+
});
43+
} else {
44+
const url = join_urls(config.root, API_INFO_URL);
45+
response = await this.fetch(url, {
46+
headers,
47+
credentials: "include"
48+
});
49+
}
4750

48-
if (!response.ok) {
49-
throw new Error(BROKEN_CONNECTION_MSG);
51+
if (!response.ok) {
52+
throw new Error(BROKEN_CONNECTION_MSG);
53+
}
54+
api_info = await response.json();
5055
}
51-
52-
let api_info = (await response.json()) as
53-
| ApiInfo<ApiData>
54-
| { api: ApiInfo<ApiData> };
5556
if ("api" in api_info) {
5657
api_info = api_info.api;
5758
}

globals.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { ApiData, ApiInfo } from "client/js/src/types";
12
declare global {
23
interface Window {
34
__gradio_mode__: "app" | "website";
45
__gradio_space__: string | null;
56
launchGradio: Function;
67
launchGradioFromSpaces: Function;
78
gradio_config: Config;
9+
gradio_api_info: ApiInfo<ApiData> | { api: ApiInfo<ApiData> };
810
scoped_css_attach: (link: HTMLLinkElement) => void;
911
__is_colab__: boolean;
1012
parentIFrame?: {

gradio/routes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,14 @@ def main(request: fastapi.Request, user: str = Depends(get_current_user)):
400400
template = (
401401
"frontend/share.html" if blocks.share else "frontend/index.html"
402402
)
403+
gradio_api_info = api_info(False)
403404
return templates.TemplateResponse(
404405
template,
405-
{"request": request, "config": config},
406+
{
407+
"request": request,
408+
"config": config,
409+
"gradio_api_info": gradio_api_info,
410+
},
406411
)
407412
except TemplateNotFound as err:
408413
if blocks.share:

js/app/build_plugins.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export function inject_ejs(): Plugin {
99
name: "inject-ejs",
1010
enforce: "post",
1111
transformIndexHtml: (html) => {
12-
return html.replace(
12+
const replace_gradio_info_info_html = html.replace(
13+
/%gradio_api_info%/,
14+
`<script>window.gradio_api_info = {{ gradio_api_info | toorjson }};</script>`
15+
);
16+
return replace_gradio_info_info_html.replace(
1317
/%gradio_config%/,
1418
`<script>window.gradio_config = {{ config | toorjson }};</script>`
1519
);

js/app/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
);
6969
</script>
7070

71-
%gradio_config%
71+
%gradio_config%%gradio_api_info%
7272

7373
<link rel="preconnect" href="https://fonts.googleapis.com" />
7474
<link

0 commit comments

Comments
 (0)