Skip to content

Fix launch JavaScript function execution - #13642

Draft
dawoodkhan82 wants to merge 2 commits into
mainfrom
codex/issue-13636-launch-js
Draft

Fix launch JavaScript function execution#13642
dawoodkhan82 wants to merge 2 commits into
mainfrom
codex/issue-13636-launch-js

Conversation

@dawoodkhan82

@dawoodkhan82 dawoodkhan82 commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Description

Restore the Gradio 5 behavior for function strings passed to Blocks.launch(js=...). The Gradio 6 frontend inserted the function expression into a script element but never called it. This adds a core executor that invokes sync and async function expressions while preserving raw JavaScript support, and runs it after the Blocks component tree is ready so Gradio's own page-title update cannot overwrite user code.

Before / after Spaces

Closes: #13636

AI Disclosure

  • I used AI (Codex) to reproduce the regression, draft the implementation and tests, and prepare this PR. I reviewed every changed line.
  • I did not use AI

🎯 PRs Should Target Issues

This PR targets #13636. No overlapping open PR was found before submission.

Testing and Formatting Your Code

  • TEST_MODE=happy-dom pnpm exec vitest run js/core/src/custom_js.test.ts --config .config/vitest.config.ts --browser.enabled=false
  • pnpm --filter @self/app build --emptyOutDir
  • pnpm --filter @self/spa build --emptyOutDir
  • One-off headless browser verification against the local reproduction: final document.title is LAUNCH-JS-RAN with no page errors (no Playwright test was added)
  • bash scripts/format_frontend.sh (formatting completed; the repository-wide type check reports 15 unrelated existing errors in js/utils, js/markdown-code, and js/preview)

Minimal reproduction (the local file is untracked and was not committed):

import gradio as gr

with gr.Blocks() as demo:
    gr.Markdown("launch(js=) repro")

demo.launch(js="""() => { document.title = "LAUNCH-JS-RAN"; }""")

@gradio-pr-bot

gradio-pr-bot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

🪼 branch checks and previews

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

Install Gradio from this PR

pip install https://huggingface.co/buckets/gradio/pypi-previews/resolve/4acd2f60d9fd674323a7177e8774a75dfb0ea338/gradio-6.20.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

Import Gradio JS Client from this PR via CDN

import { Client } from "https://huggingface.co/buckets/gradio/npm-previews/resolve/4acd2f60d9fd674323a7177e8774a75dfb0ea338/browser.js";

@gradio-pr-bot

Copy link
Copy Markdown
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/core patch
@self/app patch
@self/spa patch
gradio patch

  • Execute function strings passed to Blocks.launch(js=...)

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Restore Gradio 5-era behavior where JavaScript passed via Blocks.launch(js=...) is actually executed on the client. The previous Gradio 6 approach injected the string into a <script> element without invoking function expressions, resulting in a silent no-op for common () => { ... } usage.

Changes:

  • Move config.js execution responsibility from SPA/App shells into @gradio/core’s Blocks lifecycle (after app_tree.ready).
  • Add a shared executor (execute_custom_js) that supports function expressions (sync/async) and statement-style JS.
  • Add unit tests and a changeset bump for affected packages.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
js/spa/src/Index.svelte Removes direct <script> injection so launch(js=...) runs via core Blocks instead.
js/app/src/routes/[...catchall]/+page.svelte Removes duplicate custom-JS injection in the SvelteKit app route.
js/core/src/custom_js.ts Introduces execute_custom_js() to evaluate and invoke function-expression JS (with raw-JS fallback).
js/core/src/custom_js.test.ts Adds Vitest coverage for function-expression, async function-expression, and raw JS execution.
js/core/src/Blocks.svelte Executes js after app_tree.ready, with error logging, before setting ready and dispatching load events.
.changeset/clean-dogs-launch.md Publishes patch releases for impacted packages to ship the regression fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread js/core/src/custom_js.ts Outdated
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.

Blocks.launch(js=...) is carried in the config but never executed in Gradio 6 (silent no-op; demo.load(js=...) works)

3 participants