Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-non-hierarchical-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: handle `about:` and `data:` protocols when embedded in iframes or webviews
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ export function create_updated_store() {
* @param {boolean} hash_routing
*/
export function is_external_url(url, base, hash_routing) {
// about: and data: are non-hierarchical protocols used in embedded webviews
// and iframes with srcdoc — treat them as internal so navigation works
if (url.protocol === 'about:' || url.protocol === 'data:') {
return false;
}

if (url.origin !== origin || !url.pathname.startsWith(base)) {
return true;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ export async function render_response({
base = segments.map(() => '..').join('/') || '.';

// resolve e.g. '../..' against current location, then remove trailing slash
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;
// the try/catch handles non-hierarchical protocols like about: and data:
// (e.g. when embedded in <iframe srcdoc> or data: URLs)
base_expression = `(() => { try { return new URL(${s(base)}, location).pathname.slice(0, -1) } catch { return ${s(paths.base || '')} } })()`;

if (!paths.assets || (paths.assets[0] === '/' && paths.assets !== SVELTE_KIT_ASSETS)) {
assets = base;
}
} else if (options.hash_routing) {
// we have to assume that we're in the right place
base_expression = "new URL('.', location).pathname.slice(0, -1)";
// the try/catch handles non-hierarchical protocols like about: and data:
base_expression =
"(() => { try { return new URL('.', location).pathname.slice(0, -1) } catch { return '' } })()";
}
}

Expand Down
Loading