Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 1, 2022
2 parents bd0231b + 8ee88e7 commit 8d5d6d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions documentation/docs/05-load.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ An instance of [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL), co

#### depends

This function declares that the `load` function has a _dependency_ on one or more URLs, which can subsequently be used with [`invalidate()`](/docs/modules#$app-navigation-invalidate) to cause `load` to rerun.
This function declares that the `load` function has a _dependency_ on one or more URLs or custom identifiers, which can subsequently be used with [`invalidate()`](/docs/modules#$app-navigation-invalidate) to cause `load` to rerun.

Most of the time you won't need this, as `fetch` calls `depends` on your behalf — it's only necessary if you're using a custom API client that bypasses `fetch`.

URLs can be absolute or relative to the page being loaded, and must be [encoded](https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding).

Custom identifiers have to be prefixed with one or more lowercase letters followed by a colon to conform to the [URI specification](https://www.rfc-editor.org/rfc/rfc3986.html)

```js
// @filename: ambient.d.ts
declare module '$lib/api' {
Expand All @@ -121,7 +123,11 @@ import * as api from '$lib/api';

/** @type {import('./$types').PageLoad} */
export async function load({ depends }) {
depends(`${api.base}/foo`, `${api.base}/bar`);
depends(
`${api.base}/foo`,
`${api.base}/bar`
'my-stuff:foo'
);

return {
foo: api.client.get('/foo'),
Expand Down
16 changes: 13 additions & 3 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,23 @@ declare module '$app/navigation' {
opts?: { replaceState?: boolean; noscroll?: boolean; keepfocus?: boolean; state?: any }
): Promise<void>;
/**
* Causes any `load` functions belonging to the currently active page to re-run if they `fetch` the resource in question. Returns a `Promise` that resolves when the page is subsequently updated.
* @param dependency The invalidated resource
* 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.
*
* If the argument is given as a `string` or `URL`, it must resolve to the same URL that was passed to `fetch` or `depends` (including query parameters).
* To create a custom identifier, use a string beginning with `[a-z]+:` (e.g. `custom:state`) — this is a valid URL.
*
* The `function` argument can be used define a custom predicate. It receives the full `URL` and causes `load` to rerun if `true` is returned.
* This can be useful if you want to invalidate based on a pattern instead of a exact match.
*
* ```js
* // Example: Match '/path' regardless of the query parameters
* invalidate((url) => url.pathname === '/path')`
* ```
* @param url The invalidated URL
*/
export function invalidate(url: string | URL | ((url: URL) => boolean)): Promise<void>;
/**
* Causes all `load` functions belonging to the currently active page to re-run. Returns a `Promise` that resolves when the page is subsequently updated.
* @param dependency The invalidated resource
*/
export function invalidateAll(): Promise<void>;
/**
Expand Down

0 comments on commit 8d5d6d0

Please sign in to comment.