Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add invalidateAll option to goto #7407

Merged
merged 2 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-wombats-repeat.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[feat] add invalidateAll option to goto
18 changes: 14 additions & 4 deletions packages/kit/src/runtime/client/client.js
Expand Up @@ -164,13 +164,19 @@ export function create_client({ target, base, trailing_slash }) {

/**
* @param {string | URL} url
* @param {{ noscroll?: boolean; replaceState?: boolean; keepfocus?: boolean; state?: any }} opts
* @param {{ noscroll?: boolean; replaceState?: boolean; keepfocus?: boolean; state?: any; invalidateAll?: boolean }} opts
* @param {string[]} redirect_chain
* @param {{}} [nav_token]
*/
async function goto(
url,
{ noscroll = false, replaceState = false, keepfocus = false, state = {} },
{
noscroll = false,
replaceState = false,
keepfocus = false,
state = {},
invalidateAll = false
},
redirect_chain,
nav_token
) {
Expand All @@ -188,7 +194,11 @@ export function create_client({ target, base, trailing_slash }) {
replaceState
},
nav_token,
accepted: () => {},
accepted: () => {
if (invalidateAll) {
force_invalidation = true;
}
},
blocked: () => {},
type: 'goto'
});
Expand Down Expand Up @@ -1212,7 +1222,7 @@ export function create_client({ target, base, trailing_slash }) {
post_update();
}
} else if (result.type === 'redirect') {
goto(result.location, {}, []);
goto(result.location, { invalidateAll: true }, []);
} else {
/** @type {Record<string, any>} */
const props = {
Expand Down
@@ -1,10 +1,15 @@
<script>
import { invalidateAll } from '$app/navigation';
import { invalidateAll, goto } from '$app/navigation';

/** @type {import('./$types').PageData} */
export let data;
</script>

<h1>a: {data.a}, b: {data.b}</h1>

<button on:click={invalidateAll}>invalidate</button>
<button class="invalidateall" on:click={invalidateAll}>invalidate</button>

<button
class="goto"
on:click={() => goto('/load/invalidation/forced?test', { invalidateAll: true })}>goto</button
>
19 changes: 17 additions & 2 deletions packages/kit/test/apps/basics/test/client.test.js
Expand Up @@ -755,17 +755,32 @@ test.describe.serial('Invalidation', () => {
await page.goto('/load/invalidation/forced');
expect(await page.textContent('h1')).toBe('a: 0, b: 1');

await page.click('button');
await page.click('button.invalidateall');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(200); // apparently necessary
expect(await page.textContent('h1')).toBe('a: 2, b: 3');

await page.click('button');
await page.click('button.invalidateall');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(200);
expect(await page.textContent('h1')).toBe('a: 4, b: 5');
});

test('server-only load functions are re-run following goto with forced invalidation', async ({
page,
request
}) => {
await request.get('/load/invalidation/forced/reset');

await page.goto('/load/invalidation/forced');
expect(await page.textContent('h1')).toBe('a: 0, b: 1');

await page.click('button.goto');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(200); // apparently necessary
expect(await page.textContent('h1')).toBe('a: 2, b: 3');
});

test('multiple invalidations run concurrently', async ({ page, request }) => {
await page.goto('/load/invalidation/multiple');
expect(await page.textContent('p')).toBe('layout: 0, page: 0');
Expand Down
28 changes: 23 additions & 5 deletions packages/kit/types/ambient.d.ts
Expand Up @@ -182,14 +182,32 @@ declare module '$app/navigation' {
* Returns a Promise that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified `url`.
*
* @param url Where to navigate to. Note that if you've set [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths) and the URL is root-relative, you need to prepend the base path if you want to navigate within the app.
* @param opts.replaceState If `true`, will replace the current `history` entry rather than creating a new one with `pushState`
* @param opts.noscroll If `true`, the browser will maintain its scroll position rather than scrolling to the top of the page after navigation
* @param opts.keepfocus If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body
* @param opts.state The state of the new/updated history entry
* @param opts Options related to the navigation
*/
export function goto(
url: string | URL,
opts?: { replaceState?: boolean; noscroll?: boolean; keepfocus?: boolean; state?: any }
opts?: {
/**
* If `true`, will replace the current `history` entry rather than creating a new one with `pushState`
*/
replaceState?: boolean;
/**
* If `true`, the browser will maintain its scroll position rather than scrolling to the top of the page after navigation
*/
noscroll?: boolean;
/**
* If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body
*/
keepfocus?: boolean;
/**
* The state of the new/updated history entry
*/
state?: any;
/**
* If `true`, all `load` functions of the page will be rerun. See https://kit.svelte.dev/docs/load#invalidation for more info on invalidation.
*/
invalidateAll?: boolean;
}
): Promise<void>;
/**
* Causes any `load` functions belonging to the currently active page to re-run if they depend on the `url` in question, via `fetch` or `depends`. Returns a `Promise` that resolves when the page is subsequently updated.
Expand Down