Skip to content

Commit

Permalink
fix: reset invalid resources after successful invalidation (#11268)
Browse files Browse the repository at this point in the history
closes #10677
  • Loading branch information
Rich-Harris authored Dec 12, 2023
1 parent ac6c3cb commit e9b968a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-rockets-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: reset invalid resources after a successful invalidation
9 changes: 6 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function create_client(app, target) {
default_error_loader();

const container = __SVELTEKIT_EMBEDDED__ ? target : document.documentElement;

/** @type {Array<((url: URL) => boolean)>} */
const invalidated = [];

Expand Down Expand Up @@ -165,13 +166,13 @@ export function create_client(app, target) {
// Accept all invalidations as they come, don't swallow any while another invalidation
// is running because subsequent invalidations may make earlier ones outdated,
// but batch multiple synchronous invalidations.
pending_invalidate = pending_invalidate || Promise.resolve();
await pending_invalidate;
await (pending_invalidate ||= Promise.resolve());
if (!pending_invalidate) return;
pending_invalidate = null;

const url = new URL(location.href);
const intent = get_navigation_intent(url, true);

// Clear preload, it might be affected by the invalidation.
// Also solves an edge case where a preload is triggered, the navigation for it
// was then triggered and is still running while the invalidation kicks in,
Expand All @@ -184,14 +185,16 @@ export function create_client(app, target) {

if (navigation_result) {
if (navigation_result.type === 'redirect') {
return goto(new URL(navigation_result.location, url).href, {}, 1, nav_token);
await goto(new URL(navigation_result.location, url).href, {}, 1, nav_token);
} else {
if (navigation_result.props.page !== undefined) {
page = navigation_result.props.page;
}
root.$set(navigation_result.props);
}
}

invalidated.length = 0;
}

/** @param {number} index */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@
>
invalidate server
</button>

<p class="neither">neither</p>
<button
type="button"
class="neither"
on:click={() => (window.promise = invalidate('invalidate-depends:neither'))}
>
invalidate neither
</button>
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ test.describe('Invalidation', () => {
const next_shared = await page.textContent('p.shared');
expect(server).not.toBe(next_server);
expect(shared).not.toBe(next_shared);

await page.click('button.neither');
await page.evaluate(() => window.promise);
expect(await page.textContent('p.server')).toBe(next_server);
expect(await page.textContent('p.shared')).toBe(next_shared);
});

test('fetch in server load cannot be invalidated', async ({ page, app, request }) => {
Expand Down Expand Up @@ -531,6 +536,11 @@ test.describe('Invalidation', () => {
const next_shared = await page.textContent('p.shared');
expect(server).toBe(next_server);
expect(shared).not.toBe(next_shared);

await page.click('button.neither');
await page.evaluate(() => window.promise);
expect(await page.textContent('p.server')).toBe(next_server);
expect(await page.textContent('p.shared')).toBe(next_shared);
});

test('Parameter use is tracked even for routes that do not use the parameters', async ({
Expand Down

0 comments on commit e9b968a

Please sign in to comment.