Skip to content

Commit

Permalink
Should fix 4177 navigation to pages with iframe with relative urls ar…
Browse files Browse the repository at this point in the history
…e broken (#4191)

* .

* add test

* Update .changeset/sour-needles-compete.md

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
PH4NTOMiki and Rich-Harris authored Mar 4, 2022
1 parent 7f958dd commit d34298e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-needles-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Update history immediately before updating DOM
18 changes: 10 additions & 8 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function create_client({ target, session, base, trailing_slash }) {
* @param {import('./types').NavigationIntent} intent
* @param {string[]} redirect_chain
* @param {boolean} no_cache
* @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean}} [opts]
* @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean, details: { replaceState: boolean, state: any } | null}} [opts]
*/
async function update(intent, redirect_chain, no_cache, opts) {
const current_token = (token = {});
Expand Down Expand Up @@ -244,6 +244,13 @@ export function create_client({ target, session, base, trailing_slash }) {

updating = true;

if (opts && opts.details) {
const { details } = opts;
const change = details.replaceState ? 0 : 1;
details.state[INDEX_KEY] = current_history_index += change;
history[details.replaceState ? 'replaceState' : 'pushState'](details.state, '', intent.url);
}

if (started) {
current = navigation_result.state;

Expand Down Expand Up @@ -871,7 +878,8 @@ export function create_client({ target, session, base, trailing_slash }) {

await update(intent, redirect_chain, false, {
scroll,
keepfocus
keepfocus,
details
});

navigating--;
Expand All @@ -885,12 +893,6 @@ export function create_client({ target, session, base, trailing_slash }) {

stores.navigating.set(null);
}

if (details) {
const change = details.replaceState ? 0 : 1;
details.state[INDEX_KEY] = current_history_index += change;
history[details.replaceState ? 'replaceState' : 'pushState'](details.state, '', intent.url);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/iframes/nested/parent">parent</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello from the child</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<iframe title="Child content" src="./child" />
9 changes: 9 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,15 @@ test.describe.parallel('Routing', () => {
await page.evaluate('window.fulfil_navigation && window.fulfil_navigation()');
expect(await page.url()).toBe(`${baseURL}/routing/cancellation/b`);
});

test('Relative paths are relative to the current URL', async ({ page, clicknav }) => {
await page.goto('/iframes');
await clicknav('[href="/iframes/nested/parent"]');

expect(await page.frameLocator('iframe').locator('h1').textContent()).toBe(
'Hello from the child'
);
});
});

test.describe.parallel('Session', () => {
Expand Down

0 comments on commit d34298e

Please sign in to comment.