Skip to content

Commit 64d1864

Browse files
manifest json for PWA (#10187)
* Serve manifest.json for PWA * Set the PWA title * add changeset * Add the pwa option to Blocks.launch() * Set icon files with 192 and 512 pixels which are necessary for PWA on Chrome * add changeset * Format * Configure the /manifest.json path for SPA and SSR * Set the default value for the name field in the manifest.json * Fix gr.Interface() not to override the default title value * Add a test case for ChatInterface.title * Set the default values to Interface.title and ChatInterface.title * Revert "Set the default values to Interface.title and ChatInterface.title" This reverts commit 3574d6b. * Revert "Add a test case for ChatInterface.title" This reverts commit 276d6f2. * Revert "Fix gr.Interface() not to override the default title value" This reverts commit 90f0efa. * Enable the PWA feature on Spaces by default --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 501adef commit 64d1864

File tree

8 files changed

+86
-1
lines changed

8 files changed

+86
-1
lines changed

.changeset/loose-kids-accept.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@self/app": minor
3+
"@self/spa": minor
4+
"gradio": minor
5+
---
6+
7+
feat:`manifest json` for PWA

gradio/blocks.py

+3
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,7 @@ def launch(
23132313
node_server_name: str | None = None,
23142314
node_port: int | None = None,
23152315
ssr_mode: bool | None = None,
2316+
pwa: bool | None = None,
23162317
_frontend: bool = True,
23172318
) -> tuple[App, str, str]:
23182319
"""
@@ -2351,6 +2352,7 @@ def launch(
23512352
enable_monitoring: Enables traffic monitoring of the app through the /monitoring endpoint. By default is None, which enables this endpoint. If explicitly True, will also print the monitoring URL to the console. If False, will disable monitoring altogether.
23522353
strict_cors: If True, prevents external domains from making requests to a Gradio server running on localhost. If False, allows requests to localhost that originate from localhost but also, crucially, from "null". This parameter should normally be True to prevent CSRF attacks but may need to be False when embedding a *locally-running Gradio app* using web components.
23532354
ssr_mode: If True, the Gradio app will be rendered using server-side rendering mode, which is typically more performant and provides better SEO, but this requires Node 20+ to be installed on the system. If False, the app will be rendered using client-side rendering mode. If None, will use GRADIO_SSR_MODE environment variable or default to False.
2355+
pwa: If True, the Gradio app will be set up as an installable PWA (Progressive Web App). If set to None (default behavior), then the PWA feature will be enabled if this Gradio app is launched on Spaces, but not otherwise.
23542356
Returns:
23552357
app: FastAPI app object that is running the demo
23562358
local_url: Locally accessible link to the demo
@@ -2533,6 +2535,7 @@ def reverse(text):
25332535
"http" if share_server_address is not None else "https"
25342536
)
25352537
self.has_launched = True
2538+
self.pwa = utils.get_space() is not None if pwa is None else pwa
25362539

25372540
self.protocol = (
25382541
"https"

gradio/routes.py

+30
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ async def conditional_routing_middleware(
371371
and not path.startswith("/static")
372372
and not path.startswith("/login")
373373
and not path.startswith("/logout")
374+
and not path.startswith("/manifest.json")
374375
):
375376
if App.app_port is None:
376377
App.app_port = request.url.port or int(
@@ -1436,6 +1437,35 @@ def robots_txt():
14361437
else:
14371438
return "User-agent: *\nDisallow: "
14381439

1440+
@app.get("/manifest.json")
1441+
def manifest_json():
1442+
if not blocks.pwa:
1443+
raise HTTPException(status_code=404)
1444+
1445+
return ORJSONResponse(
1446+
content={
1447+
# NOTE: Required members: https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Guides/Making_PWAs_installable#required_manifest_members
1448+
"name": app.get_blocks().title or "Gradio",
1449+
"icons": [
1450+
{
1451+
"src": "static/img/logo192.svg",
1452+
"sizes": "192x192",
1453+
"type": "image/svg+xml",
1454+
"purpose": "any",
1455+
},
1456+
{
1457+
"src": "static/img/logo512.svg",
1458+
"sizes": "512x512",
1459+
"type": "image/svg+xml",
1460+
"purpose": "any",
1461+
},
1462+
],
1463+
"start_url": "./",
1464+
"display": "standalone",
1465+
},
1466+
media_type="application/manifest+json",
1467+
)
1468+
14391469
@router.get("/monitoring", dependencies=[Depends(login_check)])
14401470
async def analytics_login(request: fastapi.Request):
14411471
if not blocks.enable_monitoring:

js/app/src/routes/[...catchall]/+page.svelte

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336

337337
<svelte:head>
338338
<link rel="stylesheet" href={"./theme.css?v=" + config?.theme_hash} />
339+
<link rel="manifest" href="/manifest.json" />
339340

340341
{#if config.head}
341342
{@html config.head}

js/app/vite.config.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export default defineConfig(({ mode }) => {
3737
// plugins: [],
3838
server: {
3939
port: 9876,
40-
open: "/"
40+
open: "/",
41+
proxy: {
42+
"/manifest.json": "http://localhost:7860",
43+
"^/static/.*": "http://localhost:7860"
44+
}
4145
},
4246
resolve: {
4347
conditions: ["gradio"]

js/spa/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"
8181
async
8282
></script>
83+
84+
<link rel="manifest" href="/manifest.json" />
8385
</head>
8486

8587
<body

js/spa/public/static/img/logo192.svg

+19
Loading

js/spa/public/static/img/logo512.svg

+19
Loading

0 commit comments

Comments
 (0)