Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: open the Unsloth docs and Next.js sites in the browser panel (#14279)

Nilayeb0a983505fe
diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.py
index c132aa0e7cd..019acb433b2 100644
--- a/studio/backend/routes/browser.py
+++ b/studio/backend/routes/browser.py
@@ -403,6 +403,18 @@ _FRAME_HTML = r"""<!doctype html>
if (!cfg.url || url.origin !== shellOrigin) return url.href;
return new URL(url.pathname + url.search + url.hash, pageUrl).href;
};
+ // Routers resolve paths against location.href or location.origin, which throw here ("about:srcdoc", "null").
+ if (cfg.url) {
+ const NativeURL = URL;
+ window.URL = new Proxy(NativeURL, {
+ construct(target, args, newTarget) {
+ const base = args.length > 1 ? String(args[1]) : "";
+ if (base === "null") args = [args[0], new NativeURL(pageUrl).origin];
+ else if (/^about:srcdoc(?=[?#]|$)/.test(base)) args = [args[0], pageUrl];
+ return Reflect.construct(target, args, newTarget);
+ },
+ });
+ }
const resolve = (href) => {
try { return toPage(new URL(href, document.baseURI)); } catch { return null; }
};
@@ -574,6 +586,7 @@ _FRAME_HTML = r"""<!doctype html>
const raw = (link.getAttribute("href") || "").trim();
if (/^javascript:/i.test(raw)) return;
const modified = event.button === 1 || event.metaKey || event.ctrlKey || event.shiftKey;
+ if (!raw.startsWith("#")) post({ type: "linkClick", url: resolve(raw) });
unlessCancelled(event, () => {
// Keep "#" links on the page; against <base> they would navigate.
if (raw.startsWith("#")) {
@@ -937,11 +950,16 @@ _FRAME_HTML = r"""<!doctype html>
if (root) return html.slice(0, at(root)) + "<head>" + tags + "</head>" + html.slice(at(root));
return "<head>" + tags + "</head>" + html;
};
+ const FOLLOW_CLICK_MS = 3000;
+ const sameOrigin = (a, b) => { try { return new URL(a).origin === new URL(b).origin; } catch { return false; } };
let page = null;
+ let clicked = null;
window.addEventListener("message", (event) => {
// Relay the page's messages; the panel only trusts this window.
if (page && event.source === page.contentWindow) {
- if (event.data && event.data.source === "unsloth-browser") parent.postMessage(event.data, "*");
+ if (!event.data || event.data.source !== "unsloth-browser") return;
+ if (event.data.type === "linkClick") clicked = { url: event.data.url, at: Date.now() };
+ else parent.postMessage(event.data, "*");
return;
}
if (event.source !== parent) return;
@@ -964,7 +982,14 @@ _FRAME_HTML = r"""<!doctype html>
// report only names the origin, so the panel can say what happened but not follow it.
let blocked = false;
document.addEventListener("securitypolicyviolation", (event) => {
- if (blocked || event.effectiveDirective !== "frame-src") return;
+ if (event.effectiveDirective !== "frame-src") return;
+ // Next.js loads its own links in full here, as it sees them as another origin: follow the click.
+ if (clicked && Date.now() - clicked.at < FOLLOW_CLICK_MS && sameOrigin(clicked.url, event.blockedURI)) {
+ parent.postMessage({ source: "unsloth-browser", type: "navigate", url: clicked.url }, "*");
+ clicked = null;
+ return;
+ }
+ if (blocked) return;
blocked = true;
parent.postMessage({ source: "unsloth-browser", type: "scriptNavigation" }, "*");
});
diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.py
index d4cdb152274..4085a13fbe7 100644
--- a/studio/backend/tests/test_browser_routes.py
+++ b/studio/backend/tests/test_browser_routes.py
@@ -145,6 +145,40 @@ def test_the_shells_base_follows_the_router_only_without_a_page_base():
)
+def test_paths_resolved_against_the_shells_location_land_on_the_page():
+ shell = browser_mod._FRAME_HTML
+ boot = shell[shell.index("const boot = ") : shell.index("const escapeAttr")]
+ assert "window.URL = new Proxy(NativeURL, {" in boot
+ assert 'if (base === "null") args = [args[0], new NativeURL(pageUrl).origin];' in boot
+ assert "else if (/^about:srcdoc(?=[?#]|$)/.test(base)) args = [args[0], pageUrl];" in boot
+ assert "return Reflect.construct(target, args, newTarget);" in boot
+ assert boot.index("window.URL = ") < boot.index("const nativeFetch")
+
+
+def test_a_clicked_link_the_page_loads_in_full_is_followed():
+ shell = browser_mod._FRAME_HTML
+ assert 'if (!raw.startsWith("#")) post({ type: "linkClick", url: resolve(raw) });' in shell
+ assert (
+ 'if (event.data.type === "linkClick") clicked = { url: event.data.url, at: Date.now() };'
+ in shell
+ )
+ assert "const FOLLOW_CLICK_MS = 3000;" in shell
+ assert (
+ "Date.now() - clicked.at < FOLLOW_CLICK_MS && sameOrigin(clicked.url, event.blockedURI)"
+ in shell
+ )
+ assert shell.index('type: "navigate", url: clicked.url') < shell.index(
+ 'type: "scriptNavigation"'
+ )
+
+
+def test_a_clicked_link_is_followed_after_an_unfollowed_script_navigation():
+ shell = browser_mod._FRAME_HTML
+ assert 'if (event.effectiveDirective !== "frame-src") return;' in shell
+ assert "if (blocked || " not in shell
+ assert shell.index('type: "navigate", url: clicked.url') < shell.index("if (blocked) return;")
+
+
def test_annotate_code_is_sent_only_when_annotating():
shell, code = browser_mod._FRAME_HTML, browser_mod._ANNOTATE_JS
# Not in the shell (so not in every page), installed once and only from the shell's own message.