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/blur-during-navigation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: blur active element before component update during navigation so that blur/focusout handlers fire while old component data is still valid
10 changes: 10 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,16 @@ async function navigate({
navigation_result.props.page.url = url;
}

// Remove focus before updating the component tree, so that blur/focusout
// handlers fire while the old component's data is still valid (#14575)
if (
!keepfocus &&
document.activeElement instanceof HTMLElement &&
document.activeElement !== document.body
) {
document.activeElement.blur();
}

const fork = load_cache_fork && (await load_cache_fork);

if (fork) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Other page</h1>
<a href="/accessibility/blur-during-navigation/page-with-input">Back</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load() {
return { message: 'hello' };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let { data } = $props();

function handleBlur() {
// Without the fix, data was already nulled when blur fired during
// navigation, causing "Cannot read properties of undefined".
// We write to window so the result survives component teardown.
/** @type {any} */ (window).__blur_test_result = data.message;
}
</script>

<h1>Blur test</h1>
<input id="blur-input" onblur={handleBlur} />
<a href="/accessibility/blur-during-navigation/other">Go to other</a>
19 changes: 19 additions & 0 deletions packages/kit/test/apps/basics/test/cross-platform/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
test.describe.configure({ mode: 'parallel' });

test.describe('a11y', () => {
test('resets focus', async ({ page, clicknav, browserName }) => {

Check warning on line 12 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, dev)

flaky test: resets focus

retries: 2
const tab = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';

await page.goto('/accessibility/a');
Expand All @@ -32,7 +32,7 @@
expect(await page.evaluate(() => document.documentElement.getAttribute('tabindex'))).toBe(null);
});

test('applies autofocus after a navigation', async ({ page, clicknav }) => {

Check warning on line 35 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, dev)

flaky test: applies autofocus after a navigation

retries: 2
await page.goto('/accessibility/autofocus/a');

await clicknav('[href="/accessibility/autofocus/b"]', {
Expand Down Expand Up @@ -122,6 +122,25 @@
).toBe('BODY');
expect(await page.evaluate(() => document.activeElement?.nodeName)).toBe('BODY');
});

test('blur handler can access data during navigation', async ({ page, app }) => {
const errors = /** @type {string[]} */ ([]);
page.on('pageerror', (err) => errors.push(err.message));

await page.goto('/accessibility/blur-during-navigation/page-with-input');

// Focus the input
await page.locator('#blur-input').focus();

// Navigate away — this triggers blur on the focused input.
// Without the fix, data would be nulled before blur fired, causing a TypeError.
await app.goto('/accessibility/blur-during-navigation/other');

expect(errors).toEqual([]);

// The blur handler should have been able to read data.message
expect(await page.evaluate(() => /** @type {any} */ (window).__blur_test_result)).toBe('hello');
});
});

test.describe('Navigation lifecycle functions', () => {
Expand Down Expand Up @@ -183,7 +202,7 @@
expect(await page.innerHTML('pre')).toBe('1 true leave');
});

test('beforeNavigate is not triggered on redirect', async ({ page, baseURL }) => {

Check warning on line 205 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit (24, ubuntu-latest, chromium, current)

flaky test: beforeNavigate is not triggered on redirect

retries: 2
await page.goto('/navigation-lifecycle/before-navigate/prevent-navigation');

await page.click('[href="/navigation-lifecycle/before-navigate/redirect"]');
Expand Down Expand Up @@ -322,7 +341,7 @@
expect(afterNav.type).toBe('enter');
});

test('scroll state is provided on link navigation', async ({ page, clicknav, scroll_to }) => {

Check warning on line 344 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, build)

flaky test: scroll state is provided on link navigation

retries: 2
await page.goto('/navigation-lifecycle/scroll-state/a');
await scroll_to(0, 500);

Expand Down Expand Up @@ -490,7 +509,7 @@
expect(await in_view('#go-to-element')).toBe(true);
});

test('scrolling to url-supplied anchor respects scroll-margin', async ({ page, clicknav }) => {

Check warning on line 512 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, build)

flaky test: scrolling to url-supplied anchor respects scroll-margin

retries: 2
await page.goto('/anchor');
await clicknav('#to-scroll-margin');
expect(
Expand Down Expand Up @@ -589,7 +608,7 @@
else expect(await in_view('#go-to-element')).toBe(true);
});

test('app-supplied scroll and focus work on direct page load', async ({ page, in_view }) => {

Check warning on line 611 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit (24, ubuntu-latest, chromium, beta)

flaky test: app-supplied scroll and focus work on direct page load

retries: 2
await page.goto('/use-action/focus-and-scroll');
expect(await in_view('#input')).toBe(true);
await expect(page.locator('#input')).toBeFocused();
Expand Down Expand Up @@ -731,7 +750,7 @@
);
});

test('client-side error from load()', async ({ page }) => {

Check warning on line 753 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit (24, ubuntu-latest, chromium, beta)

flaky test: client-side error from load()

retries: 2
await page.goto('/errors/load-error-client');

expect(await page.textContent('footer')).toBe('Custom layout');
Expand Down Expand Up @@ -761,7 +780,7 @@
);
});

test('Root error falls back to error.html (expected error)', async ({ page, clicknav }) => {

Check warning on line 783 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, build)

flaky test: Root error falls back to error.html (expected error)

retries: 2
await page.goto('/errors/error-html');
await clicknav('button:text-is("Expected")');

Expand Down Expand Up @@ -812,7 +831,7 @@
}
});

test('prefetches data programmatically', async ({ baseURL, page, app }) => {

Check warning on line 834 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, build)

flaky test: prefetches data programmatically

retries: 2
await page.goto('/routing/a');

/** @type {string[]} */
Expand Down Expand Up @@ -1177,7 +1196,7 @@
});

test.describe('cookies', () => {
test('etag forwards cookies', async ({ page }) => {

Check warning on line 1199 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (24, windows-latest, chromium, build)

flaky test: etag forwards cookies

retries: 2
await page.goto('/cookies/forwarded-in-etag');
await expect(page.locator('p')).toHaveText('foo=bar');
await page.locator('button').click();
Expand Down
Loading