Skip to content

fix: handle about: and data: protocols when embedded in iframes/webviews#15812

Open
PranavAgarkar07 wants to merge 2 commits into
sveltejs:mainfrom
PranavAgarkar07:fix/handle-non-hierarchical-protocol
Open

fix: handle about: and data: protocols when embedded in iframes/webviews#15812
PranavAgarkar07 wants to merge 2 commits into
sveltejs:mainfrom
PranavAgarkar07:fix/handle-non-hierarchical-protocol

Conversation

@PranavAgarkar07
Copy link
Copy Markdown

Related: #13490
Fixes: #13226

The problem

When a SvelteKit app is embedded in an <iframe srcdoc> or loaded via a data: URL (common in webviews and embedded widgets), hydration crashes with:

Uncaught TypeError: Failed to construct 'URL': Invalid URL

SvelteKit generates an inline script that runs new URL('.', location) to compute the base path. When location.protocol is about: (srcdoc iframes) or data: (data URL iframes), these are non-hierarchical protocols that cannot be used as a URL base — the browser throws immediately.

Two fixes

render.js — wrap the generated base_expression in a try/catch so a non-hierarchical protocol falls back to the statically-known base path instead of crashing:

// before
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;

// after
base_expression = `(() => { try { return new URL(${s(base)}, location).pathname.slice(0, -1) } catch { return ${s(paths.base || '')} } })()`;

The try/catch is future-proof — it handles any non-hierarchical protocol without enumerating them.

utils.jsis_external_url was treating about: and data: URLs as external (their origin is "null"), causing all link clicks inside an embedded app to trigger full-page navigations instead of client-side routing:

if (url.protocol === 'about:' || url.protocol === 'data:') {
    return false;
}

Why not #13490?

PR #13490 has a bug in the render.js change: the fallback branch returns a raw URL object instead of a .pathname string, so base becomes [object URL]. This PR fixes both issues correctly.

Checklist

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 7, 2026

🦋 Changeset detected

Latest commit: d7c9dee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread packages/kit/src/runtime/server/page/render.js Outdated
@PranavAgarkar07 PranavAgarkar07 force-pushed the fix/handle-non-hierarchical-protocol branch from 35fe57e to 7281def Compare May 8, 2026 08:35
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.

Handle cases when location.protocol is about: or data:

1 participant