toiljs 0.0.116 → 0.0.117
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +16 -7
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +25 -4
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +6 -5
- package/build/compiler/vite.js +3 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
# Fetching data
|
|
2
|
-
|
|
3
|
-
Your React frontend and your toiljs backend live in one project, so toiljs generates a **typed client** that lets the browser call your server with full type safety and no hand-written `fetch` boilerplate. This page covers loading data for a page, calling the backend directly, submitting forms, and reading who is logged in.
|
|
4
|
-
|
|
5
|
-
## The generated `Server` client
|
|
6
|
-
|
|
7
|
-
When you build the server, toiljs writes a file at `shared/server.ts` that contains your `@data` classes and a typed description of every backend endpoint. Importing anything from `shared/server` attaches the runtime clients to a global called `Server`. From then on you call your backend through `Server`, fully typed, with editor autocomplete.
|
|
8
|
-
|
|
9
|
-
There are two surfaces under `Server`:
|
|
10
|
-
|
|
11
|
-
- **`Server.REST.<controller>.<route>(args)`**: a real, typed `fetch` client for your `@rest` HTTP controllers. This is the working, recommended way to call the backend today.
|
|
12
|
-
- **`Server.<service>.<method>(args)` and `Server.<remote>(args)`**: the typed RPC surface for `@service` / `@remote` functions.
|
|
13
|
-
|
|
14
|
-
Both are generated from your server code, so if you rename a route or change an argument type, the call site is a compile error until you fix it.
|
|
15
|
-
|
|
16
|
-
> **Rule: never call your own backend with raw `fetch`. Always go through `Server`.**
|
|
17
|
-
> Use `Server.REST.<controller>.<route>(args)` for `@rest` HTTP controllers, or
|
|
18
|
-
> `Server.<service>.<method>(args)` / `Server.<remote>(args)` for `@service` / `@remote`
|
|
19
|
-
> RPC. Raw `fetch` throws away the type safety, the argument/return decoding, and the
|
|
20
|
-
> binary wire format that the generated client handles for you, and it silently breaks
|
|
21
|
-
> when a route is renamed. This applies to AI assistants generating code too.
|
|
22
|
-
>
|
|
23
|
-
> ```ts
|
|
24
|
-
> // WRONG: raw fetch to your backend
|
|
25
|
-
> await fetch('/account/session', { method: 'POST', credentials: 'same-origin' });
|
|
26
|
-
>
|
|
27
|
-
> // RIGHT: the typed client (renames become compile errors, args + result are typed)
|
|
28
|
-
> await Server.REST.account.session();
|
|
29
|
-
> ```
|
|
30
|
-
>
|
|
31
|
-
> `fetch` is only for URLs that are NOT your toiljs backend (a third-party API, a CDN).
|
|
32
|
-
|
|
33
|
-
## Calling a REST endpoint
|
|
34
|
-
|
|
35
|
-
`Server.REST` mirrors your `@rest` controllers. If your backend has a `players` controller with routes on it, you call them like this:
|
|
36
|
-
|
|
37
|
-
```tsx
|
|
38
|
-
import { NewPlayer, ScoreDelta } from 'shared/server';
|
|
39
|
-
|
|
40
|
-
// POST /players with a typed @data body -> typed Promise<Player>
|
|
41
|
-
const player = await Server.REST.players.create({ body: new NewPlayer('Ada') });
|
|
42
|
-
|
|
43
|
-
// POST /players/:id/score with a path param AND a body
|
|
44
|
-
const updated = await Server.REST.players.addScore({
|
|
45
|
-
params: { id: 1 },
|
|
46
|
-
body: new ScoreDelta(5n),
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// GET /leaderboard -> typed Promise<Standings>
|
|
50
|
-
const board = await Server.REST.leaderboard.top();
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
The single argument is an object with up to four optional parts:
|
|
54
|
-
|
|
55
|
-
| Key | What it is |
|
|
56
|
-
| --- | --- |
|
|
57
|
-
| `params` | Path parameters, e.g. `{ id: 1 }` for a `/players/:id` route. |
|
|
58
|
-
| `body` | The request body, usually a `@data` class instance. |
|
|
59
|
-
| `query` | Query-string values. |
|
|
60
|
-
| `headers` | Extra request headers. |
|
|
61
|
-
|
|
62
|
-
Return values are typed and decoded for you. A route that returns a `@data` type hands you the parsed class instance. A route that returns a raw `Response` hands you the raw fetch `Response`, so you can inspect the status and headers yourself:
|
|
63
|
-
|
|
64
|
-
```tsx
|
|
65
|
-
// This route returns a Response, so you get the raw fetch Response.
|
|
66
|
-
const res = await Server.REST.players.get({ params: { id: 1 } });
|
|
67
|
-
if (!res.ok) {
|
|
68
|
-
console.log('status', res.status);
|
|
69
|
-
} else {
|
|
70
|
-
const p = await res.json();
|
|
71
|
-
}
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
`@data` classes are the typed values that cross the wire. You import them from `shared/server` and construct them normally (`new NewPlayer('Ada')`). See [Data types](../backend/data.md) for how they are defined on the server.
|
|
75
|
-
|
|
76
|
-
### Handling errors
|
|
77
|
-
|
|
78
|
-
A failed call throws. The global `parseError` helper turns any caught value into a readable message, which is handy in a `catch`:
|
|
79
|
-
|
|
80
|
-
```tsx
|
|
81
|
-
try {
|
|
82
|
-
const board = await Server.REST.leaderboard.top();
|
|
83
|
-
} catch (err) {
|
|
84
|
-
console.error(parseError(err));
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
## Loading data for a page (loaders)
|
|
89
|
-
|
|
90
|
-
Calling the backend from a button handler (as above) is fine for actions. For the data a page needs to *render*, use a route **loader** instead of a `useEffect`. A loader runs on navigation, in parallel with loading the page's code, and the page suspends (showing its `loading.tsx`) until the data is ready:
|
|
91
|
-
|
|
92
|
-
```tsx
|
|
93
|
-
// client/routes/blog/[id].tsx
|
|
94
|
-
export const loader = async ({ params }: Toil.LoaderArgs) => {
|
|
95
|
-
return Server.REST.blog.get({ params: { id: params.id } });
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export default function BlogPost() {
|
|
99
|
-
const post = Toil.useLoaderData<typeof loader>();
|
|
100
|
-
return <article><h1>{post.title}</h1></article>;
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
`useLoaderData<typeof loader>()` is fully typed from the loader's return. This keeps data fetching declarative and out of effects, and it is what lets server-rendered pages seed the browser with the server's data so hydration stays clean.
|
|
105
|
-
|
|
106
|
-
### Caching and revalidating loader data
|
|
107
|
-
|
|
108
|
-
Loader results are cached by URL. You control how long with `export const revalidate`:
|
|
109
|
-
|
|
110
|
-
| `revalidate` value | Behavior |
|
|
111
|
-
| --- | --- |
|
|
112
|
-
| `0` (default) | Re-run the loader on every navigation to the route. |
|
|
113
|
-
| a number `n` | Reuse cached data for `n` seconds, then refetch. |
|
|
114
|
-
| `false` | Cache until you invalidate it manually. |
|
|
115
|
-
|
|
116
|
-
After a mutation (say you just POSTed a new comment), refresh the current page's data with `revalidate()` or the router:
|
|
117
|
-
|
|
118
|
-
```tsx
|
|
119
|
-
const router = Toil.useRouter();
|
|
120
|
-
await Server.REST.comments.add({ body: new NewComment(text) });
|
|
121
|
-
router.revalidate(); // refetch the active route's loader
|
|
122
|
-
// or target another route: router.revalidate('/posts');
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
`router.refresh()` re-runs the current loader and clears the cache; `router.revalidate(href)` invalidates a specific route.
|
|
126
|
-
|
|
127
|
-
### Invalidating loader data by hand
|
|
128
|
-
|
|
129
|
-
`revalidate()` and the `<Toil.Form>` `revalidate` prop are both built on one lower-level primitive: `Toil.invalidateLoaderData(href?)`. It drops cached loader data so the next render refetches it, but it does **not** trigger the re-render itself (that is the difference from `revalidate()`, which does both):
|
|
130
|
-
|
|
131
|
-
- `invalidateLoaderData()` with no argument clears every route's cached data.
|
|
132
|
-
- `invalidateLoaderData('/posts')` clears just that one route's entry.
|
|
133
|
-
|
|
134
|
-
You rarely need it directly; `revalidate()` (which pairs it with a re-render) is what you usually want. Reach for the primitive when you want to mark data stale *now* but let a later navigation do the actual refetch:
|
|
135
|
-
|
|
136
|
-
```tsx
|
|
137
|
-
// After a background sync finished, mark the dashboard stale so its
|
|
138
|
-
// next visit refetches, without disturbing the page the user is on.
|
|
139
|
-
Toil.invalidateLoaderData('/dashboard');
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
Signature: `invalidateLoaderData(href?: string): void`.
|
|
143
|
-
|
|
144
|
-
## Forms
|
|
145
|
-
|
|
146
|
-
For the common "submit a form, then refresh the page's data" loop, use `Toil.Form`. It runs an async action on submit (no page reload), tracks pending and error state, and revalidates the route's loader data on success:
|
|
147
|
-
|
|
148
|
-
```tsx
|
|
149
|
-
import { NewMessage } from 'shared/server';
|
|
150
|
-
|
|
151
|
-
export default function Guestbook() {
|
|
152
|
-
const entries = Toil.useLoaderData<typeof loader>();
|
|
153
|
-
|
|
154
|
-
const sign = async (data: FormData) => {
|
|
155
|
-
const author = String(data.get('author'));
|
|
156
|
-
const message = String(data.get('message'));
|
|
157
|
-
await Server.REST.guestbook.sign({ body: new NewMessage(author, message) });
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
return (
|
|
161
|
-
<Toil.Form action={sign} resetOnSuccess>
|
|
162
|
-
{({ pending }) => (
|
|
163
|
-
<>
|
|
164
|
-
<input name="author" />
|
|
165
|
-
<input name="message" />
|
|
166
|
-
<button disabled={pending}>Sign</button>
|
|
167
|
-
</>
|
|
168
|
-
)}
|
|
169
|
-
</Toil.Form>
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
Key `Toil.Form` props:
|
|
175
|
-
|
|
176
|
-
| Prop | Default | What it does |
|
|
177
|
-
| --- | --- | --- |
|
|
178
|
-
| `action` | (required) | Runs on submit, receiving the form's `FormData`. May be async. |
|
|
179
|
-
| `revalidate` | `true` (current route) | Which loader data to refetch after a successful submit. |
|
|
180
|
-
| `resetOnSuccess` | `false` | Clear the form fields after success. |
|
|
181
|
-
| `onSuccess` / `onError` | | Callbacks for the two outcomes. |
|
|
182
|
-
|
|
183
|
-
Passing a **render function** as `children` gives you live submit state (`pending`, `error`), so you can disable the button while the request is in flight. On success, `Form` revalidates the loader, so the page's data updates automatically without a manual refetch.
|
|
184
|
-
|
|
185
|
-
## Mutations without a form: `useAction`
|
|
186
|
-
|
|
187
|
-
`Toil.Form` is convenient sugar over a more general primitive, `Toil.useAction`. Use `useAction` for any write that is not a form submit: a delete button, a "like" toggle, a "mark as read" action. It runs your async function on demand and tracks the whole lifecycle (`pending`, `error`, `data`), then revalidates the affected loader data on success so the page reflects the change, exactly like `Form` does.
|
|
188
|
-
|
|
189
|
-
You call it with the function to run and an optional options object, and it hands back a handle:
|
|
190
|
-
|
|
191
|
-
```tsx
|
|
192
|
-
export default function PostRow({ id }: { id: number }) {
|
|
193
|
-
const del = Toil.useAction(
|
|
194
|
-
(postId: number) => Server.REST.posts.remove({ params: { id: postId } }),
|
|
195
|
-
{ revalidate: true, onError: (e) => alert(parseError(e)) },
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
return (
|
|
199
|
-
<button disabled={del.pending} onClick={() => void del.run(id)}>
|
|
200
|
-
{del.pending ? 'Deleting...' : 'Delete'}
|
|
201
|
-
</button>
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
The handle it returns:
|
|
207
|
-
|
|
208
|
-
| Field | What it is |
|
|
209
|
-
| --- | --- |
|
|
210
|
-
| `run(input)` | Runs the action. Resolves to the result, or `undefined` if it threw. The error is captured in `error` rather than rejected, so a fire-and-forget `onClick` cannot leak an unhandled rejection. |
|
|
211
|
-
| `pending` | `true` while a run is in flight. Drive a spinner or a disabled button off this. |
|
|
212
|
-
| `error` | The error from the last failed run, or `undefined`. |
|
|
213
|
-
| `data` | The value the last successful run returned, or `undefined`. |
|
|
214
|
-
| `reset()` | Clear `pending` / `error` / `data` back to idle. |
|
|
215
|
-
|
|
216
|
-
The options mirror `Toil.Form`:
|
|
217
|
-
|
|
218
|
-
| Option | Default | What it does |
|
|
219
|
-
| --- | --- | --- |
|
|
220
|
-
| `revalidate` | `true` (current route) | Which loader data to refetch after a successful run: `true` (the active route), an `href` or array of hrefs (those routes), or `false` (none). |
|
|
221
|
-
| `onSuccess` | | Called with the action's return value after a successful run. |
|
|
222
|
-
| `onError` | | Called with the thrown value when the action fails. |
|
|
223
|
-
|
|
224
|
-
`Toil.Form` is just this hook wired to a `<form>`'s submit event, with the form's `FormData` passed as the input. Anything a form does, you can do by hand with `useAction`.
|
|
225
|
-
|
|
226
|
-
## Reading who is logged in
|
|
227
|
-
|
|
228
|
-
To render "logged in as ..." you need the current user. toiljs generates a `getUser()` helper (from the backend's `@user` surface) that reads the current user from a readable session cookie with no network round-trip. It is instant, which makes it perfect for display, but it is **untrusted**: a value read on the client can be forged, so never gate anything security-sensitive on it. For a trusted check, call a guarded backend route that re-verifies the signed session.
|
|
229
|
-
|
|
230
|
-
The full auth flow (post-quantum login, sessions, `getUser()`, and guarding routes) is its own guide:
|
|
231
|
-
|
|
232
|
-
- [Auth usage](../auth/usage.md): reading the session, `getUser()`, and guarding pages.
|
|
233
|
-
|
|
234
|
-
## RPC (`@service` / `@remote`)
|
|
235
|
-
|
|
236
|
-
Alongside REST, toiljs generates a typed RPC surface for `@service` methods and free `@remote` functions. These read like plain function calls with no URL:
|
|
237
|
-
|
|
238
|
-
```tsx
|
|
239
|
-
const n = await Server.ping(10); // a free @remote
|
|
240
|
-
const count = await Server.stats.playerCount(); // a @service method
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
The types come straight from your server, so `Server.ping` knows it takes a number and returns a number. Under the hood each call encodes its arguments and POSTs them to a single reserved endpoint (`/__toil_rpc`) with a compact method id, then decodes the typed result. Both the local dev server and the production edge dispatch this endpoint, so RPC works end to end.
|
|
244
|
-
|
|
245
|
-
If a call throws an "unavailable" error, it means the generated client has not attached yet: build the server (`npm run build:server`) to regenerate `shared/server.ts`, and import from `shared/server` so the client loads (see the gotchas below). See [Typed RPC](../backend/rpc.md) for the backend side and when to choose RPC over REST.
|
|
246
|
-
|
|
247
|
-
## Gotchas
|
|
248
|
-
|
|
249
|
-
- **Import from `shared/server` to attach the clients.** `Server.REST` (and the RPC client) attach when you import from `shared/server`. Importing your `@data` classes from there does it naturally; if `Server.REST.foo()` throws an "unavailable" error, make sure the server has been built (`npm run build:server`) and that `shared/server` is imported.
|
|
250
|
-
- **The server runs a fresh instance per request.** In the examples, in-memory writes are previews that do not persist. To keep data, write it to the database (see [Database](../database/README.md)). This is a backend property, but it explains why a create returns a value that is not there on the next request.
|
|
251
|
-
- **Fetch page data in a `loader`, not `useEffect`.** A loader runs in parallel with the route chunk and integrates with `loading.tsx`, suspense, caching, and SSR hydration. A `useEffect` fetch runs only after the page mounts (slower, and invisible to the server).
|
|
252
|
-
- **`getUser()` is display-only.** It is fast because it does not verify anything. Do real authorization on the server.
|
|
253
|
-
|
|
254
|
-
## Related
|
|
255
|
-
|
|
256
|
-
- [Routing](./routing.md): loaders, `loading.tsx`, and navigation.
|
|
257
|
-
- [Backend HTTP routes](../backend/rest.md): the `@rest` controllers behind `Server.REST`.
|
|
258
|
-
- [Typed RPC](../backend/rpc.md): the `@service` / `@remote` surface behind `Server`.
|
|
259
|
-
- [Data types](../backend/data.md): the `@data` classes that cross the wire.
|
|
260
|
-
- [Auth usage](../auth/usage.md): sessions and `getUser()`.
|
package/docs/frontend/images.md
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
# Images
|
|
2
|
-
|
|
3
|
-
`Toil.Image` is a drop-in replacement for `<img>` that stops layout shift, lazy-loads by default, and can fade in from a blurred placeholder. Use it for content images instead of a raw `<img>`.
|
|
4
|
-
|
|
5
|
-
## Why not just use `<img>`?
|
|
6
|
-
|
|
7
|
-
Two problems with a plain `<img>`:
|
|
8
|
-
|
|
9
|
-
1. **Layout shift.** Before the image loads, the browser does not know how tall it is, so the page has no space reserved. When the image arrives, everything below it jumps down. That jump hurts the user experience and your Core Web Vitals score (specifically CLS, Cumulative Layout Shift).
|
|
10
|
-
2. **Loading cost.** Every image loads eagerly by default, competing for bandwidth with the things the user actually needs first.
|
|
11
|
-
|
|
12
|
-
`Toil.Image` fixes both: it reserves the right amount of space up front, and it lazy-loads everything except the images you mark as high priority.
|
|
13
|
-
|
|
14
|
-
## The simplest usage
|
|
15
|
-
|
|
16
|
-
Give it a `src`, an `alt`, and a `width` + `height`. The width and height are the image's real pixel dimensions, and they are what reserve space so nothing jumps:
|
|
17
|
-
|
|
18
|
-
```tsx
|
|
19
|
-
export default function Post() {
|
|
20
|
-
return (
|
|
21
|
-
<Toil.Image
|
|
22
|
-
src="/images/diagram.png"
|
|
23
|
-
alt="How the request flows"
|
|
24
|
-
width={800}
|
|
25
|
-
height={400}
|
|
26
|
-
/>
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
`alt` is required (toiljs enforces it for accessibility). For a purely decorative image, pass `alt=""`.
|
|
32
|
-
|
|
33
|
-
There is no server-side resizing here: `Toil.Image` is a client component. Point `src` at an already-optimized image. When you import an image (see below), Vite hashes and optimizes the file for you.
|
|
34
|
-
|
|
35
|
-
## Automatic blur placeholders with `?toil`
|
|
36
|
-
|
|
37
|
-
For a nicer loading experience you can show a tiny blurred preview of the image while the full one downloads. toiljs generates that preview automatically when you import the image with a `?toil` flag:
|
|
38
|
-
|
|
39
|
-
```tsx
|
|
40
|
-
import hero from './hero.webp?toil';
|
|
41
|
-
|
|
42
|
-
export default function Landing() {
|
|
43
|
-
return <Toil.Image src={hero} alt="Product hero" placeholder="blur" />;
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
The `?toil` import does not give you a plain URL string. It gives you an object:
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
{
|
|
51
|
-
src: string; // the optimized, hashed image URL
|
|
52
|
-
width: number; // the image's intrinsic width
|
|
53
|
-
height: number; // the image's intrinsic height
|
|
54
|
-
blurDataURL: string; // a tiny base64 blurred preview, inlined
|
|
55
|
-
}
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
`Toil.Image` unpacks that object for you. So a `?toil` import auto-fills `width`, `height`, and the blur placeholder with **no extra props**: you do not repeat the dimensions, and `placeholder="blur"` just works. Explicit props still win if you pass them.
|
|
59
|
-
|
|
60
|
-
Behind the scenes, at import time toiljs uses `sharp` to downscale the image to about 24 pixels on its longest edge, blur it, and encode it as a small WebP data URI (a few hundred bytes) inlined right into your bundle. This runs in dev and in the build. Only raster images (`.png`, `.jpg`, `.webp`, `.avif`, `.gif`, `.tiff`) get a blur; SVGs and animated images are left as-is.
|
|
61
|
-
|
|
62
|
-
### The skeleton fallback
|
|
63
|
-
|
|
64
|
-
`placeholder="blur"` is never a silent no-op. If there is no `blurDataURL` (you used a string `src` and did not pass one), `Toil.Image` shows a neutral animated "skeleton shimmer" instead of a blur. Either way the placeholder needs a reserved box to paint into, so give it `width` + `height` (or use `fill`, below).
|
|
65
|
-
|
|
66
|
-
You can also pass a `blurDataURL` by hand for a plain string `src`:
|
|
67
|
-
|
|
68
|
-
```tsx
|
|
69
|
-
<Toil.Image
|
|
70
|
-
src="/images/photo.jpg"
|
|
71
|
-
alt="A photo"
|
|
72
|
-
width={1200}
|
|
73
|
-
height={800}
|
|
74
|
-
placeholder="blur"
|
|
75
|
-
blurDataURL="data:image/webp;base64,UklGR..."
|
|
76
|
-
/>
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## The real layout-shift fix: aspect ratio
|
|
80
|
-
|
|
81
|
-
The important detail: the thing that actually prevents layout shift is not the blur, it is the **reserved aspect ratio**. When you pass both `width` and `height`, `Toil.Image` sets a CSS `aspect-ratio` derived from them. That reservation survives responsive CSS like `width: 100%` (a bare `width`/`height` attribute does not survive that). So the box holds its shape while the image loads, and nothing jumps. The blur is cosmetic; the reserved size is what holds.
|
|
82
|
-
|
|
83
|
-
The takeaway: always give `width` + `height` (or `fill`). A `?toil` import supplies them for you.
|
|
84
|
-
|
|
85
|
-
## `fill`: sizing from the container
|
|
86
|
-
|
|
87
|
-
Sometimes you do not know the image's size, you want it to fill a box you control (a card, a banner). Pass `fill`:
|
|
88
|
-
|
|
89
|
-
```tsx
|
|
90
|
-
<div style={{ maxWidth: 480 }}>
|
|
91
|
-
<Toil.Image src={photo} alt="Cover" fill />
|
|
92
|
-
</div>
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
With `fill`, `Toil.Image` wraps the image in a block-level box and the image fills that box's width, scaling to its natural height. If you size the box yourself (with `width`/`height`, or a `style` like `aspectRatio: '16 / 9'`), the image *covers* that box, cropped according to `objectFit` (default `cover`):
|
|
96
|
-
|
|
97
|
-
```tsx
|
|
98
|
-
<Toil.Image
|
|
99
|
-
src={photo}
|
|
100
|
-
alt="Cover"
|
|
101
|
-
fill
|
|
102
|
-
style={{ aspectRatio: '16 / 9' }}
|
|
103
|
-
objectFit="cover"
|
|
104
|
-
/>
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
The `fill` image flows in the normal document layout (it is never absolutely positioned), so it cannot escape to cover the whole page nor collapse to zero height, two common bugs with hand-rolled fill images. With `fill`, your `className` and `style` apply to the wrapper box, not the raw `<img>`.
|
|
108
|
-
|
|
109
|
-
Use fixed `width`/`height` when you know the image's size; use `fill` when the layout decides the size.
|
|
110
|
-
|
|
111
|
-
## Priority images (above the fold)
|
|
112
|
-
|
|
113
|
-
By default every `Toil.Image` lazy-loads: the browser fetches it only as it nears the viewport. That is right for most images, but wrong for the one big image at the top of the page (your hero, your LCP element). Mark that one `priority`:
|
|
114
|
-
|
|
115
|
-
```tsx
|
|
116
|
-
<Toil.Image src={hero} alt="Hero" width={1200} height={630} priority />
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
`priority` loads the image eagerly and sets `fetchPriority="high"`, telling the browser to fetch it right away. Use it only for the important above-the-fold image; everything else should stay lazy.
|
|
120
|
-
|
|
121
|
-
## Prop reference
|
|
122
|
-
|
|
123
|
-
| Prop | Type | Default | What it does |
|
|
124
|
-
| --- | --- | --- | --- |
|
|
125
|
-
| `src` | `string \| ToilImageSource` | (required) | A URL, or a `?toil` import object. |
|
|
126
|
-
| `alt` | `string` | (required) | Alt text. Use `""` for decorative images. |
|
|
127
|
-
| `width` / `height` | `number` | from `?toil` | Intrinsic size, reserves space. |
|
|
128
|
-
| `fill` | `boolean` | `false` | Fill the container instead of using fixed size. |
|
|
129
|
-
| `objectFit` | CSS `object-fit` | `cover` (with fill) | How the image fits its box. |
|
|
130
|
-
| `priority` | `boolean` | `false` | Eager load + high fetch priority (above-the-fold). |
|
|
131
|
-
| `placeholder` | `'empty' \| 'blur'` | `'empty'` | Show a blur / skeleton while loading. |
|
|
132
|
-
| `blurDataURL` | `string` | from `?toil` | The tiny preview for `placeholder="blur"`. |
|
|
133
|
-
|
|
134
|
-
Every other standard `<img>` attribute (`className`, `style`, `onLoad`, `sizes`, `srcSet`, `id`, `data-*`) passes straight through.
|
|
135
|
-
|
|
136
|
-
## Gotchas
|
|
137
|
-
|
|
138
|
-
- **No server-side resizing.** `Toil.Image` does not shrink or convert your image at request time. Ship an appropriately sized `src` (importing it lets Vite optimize and hash it). The `?toil` blur is only a placeholder, not a resized copy.
|
|
139
|
-
- **A placeholder needs a reserved box.** `placeholder="blur"` only shows if there is a size to paint into. Give `width` + `height`, or use `fill` inside a sized parent.
|
|
140
|
-
- **`?toil` is for raster images.** SVGs and animated formats do not get a generated blur (importing them with `?toil` skips the blur step). Import an SVG normally.
|
|
141
|
-
- **`alt` is mandatory.** This is intentional. For decorative images that add no meaning, pass `alt=""` so screen readers skip them.
|
|
142
|
-
- **Do not double up dimensions with a `?toil` import.** The import already carries `width`/`height`; passing them again is redundant (though harmless, explicit props win).
|
|
143
|
-
|
|
144
|
-
## Related
|
|
145
|
-
|
|
146
|
-
- [Styling](./styling.md): importing images and CSS in general.
|
|
147
|
-
- [Metadata and SEO](./metadata.md): setting `og:image` for social-share previews.
|
|
148
|
-
- [Rendering and SSR](./rendering.md): why first-paint layout stability matters.
|