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.
Files changed (325) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +16 -7
  3. package/build/backend/.tsbuildinfo +1 -1
  4. package/build/cli/.tsbuildinfo +1 -1
  5. package/build/cli/index.js +2375 -258
  6. package/build/client/.tsbuildinfo +1 -1
  7. package/build/client/dev/devtools.js +1 -1
  8. package/build/client/dev/error-overlay.js +1 -1
  9. package/build/client/routing/mount.js +1 -1
  10. package/build/compiler/.tsbuildinfo +1 -1
  11. package/build/compiler/auth-emails.js +7 -3
  12. package/build/compiler/emails.js +1 -1
  13. package/build/compiler/index.d.ts +1 -0
  14. package/build/compiler/index.js +25 -4
  15. package/build/compiler/pages.js +2 -3
  16. package/build/compiler/prerender.d.ts +3 -8
  17. package/build/compiler/prerender.js +88 -127
  18. package/build/compiler/ssg.js +3 -4
  19. package/build/compiler/template-build.js +3 -4
  20. package/build/compiler/toil-docs.generated.js +6 -5
  21. package/build/compiler/vite.js +3 -6
  22. package/build/devserver/.tsbuildinfo +1 -1
  23. package/build/devserver/db/database.js +7 -3
  24. package/build/devserver/db/types.js +1 -1
  25. package/build/devserver/production.js +4 -3
  26. package/build/devserver/runtime/crypto.d.ts +1 -1
  27. package/build/io/.tsbuildinfo +1 -1
  28. package/build/logger/.tsbuildinfo +1 -1
  29. package/build/shared/.tsbuildinfo +1 -1
  30. package/examples/basic/client/routes/analytics.tsx +7 -5
  31. package/examples/basic/server/tsconfig.json +1 -3
  32. package/package.json +45 -44
  33. package/presets/no-uint8array-tostring.js +120 -153
  34. package/presets/oxlint.d.ts +5 -0
  35. package/presets/oxlint.js +224 -0
  36. package/server/runtime/tsconfig.json +1 -3
  37. package/tsconfig.json +2 -11
  38. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  39. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  40. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  41. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  42. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  43. package/.github/changelog-config.json +0 -45
  44. package/.github/dependabot.yml +0 -27
  45. package/.github/workflows/ci.yml +0 -222
  46. package/.vscode/settings.json +0 -10
  47. package/docs/README.md +0 -107
  48. package/docs/auth/README.md +0 -104
  49. package/docs/auth/configuration.md +0 -105
  50. package/docs/auth/emails.md +0 -92
  51. package/docs/auth/extending.md +0 -202
  52. package/docs/auth/how-it-works.md +0 -138
  53. package/docs/auth/usage.md +0 -188
  54. package/docs/backend/README.md +0 -143
  55. package/docs/backend/data.md +0 -351
  56. package/docs/backend/rest.md +0 -402
  57. package/docs/backend/rpc.md +0 -226
  58. package/docs/background/README.md +0 -114
  59. package/docs/background/daemons.md +0 -276
  60. package/docs/background/derive.md +0 -179
  61. package/docs/cli/README.md +0 -379
  62. package/docs/concepts/ai-guide.md +0 -132
  63. package/docs/concepts/config.md +0 -416
  64. package/docs/concepts/decorators.md +0 -127
  65. package/docs/concepts/security.md +0 -108
  66. package/docs/concepts/tiers.md +0 -166
  67. package/docs/concepts/types.md +0 -216
  68. package/docs/database/README.md +0 -143
  69. package/docs/database/capacity.md +0 -350
  70. package/docs/database/counters.md +0 -174
  71. package/docs/database/documents.md +0 -277
  72. package/docs/database/events.md +0 -307
  73. package/docs/database/membership.md +0 -246
  74. package/docs/database/setup.md +0 -155
  75. package/docs/database/unique.md +0 -216
  76. package/docs/database/views.md +0 -246
  77. package/docs/frontend/README.md +0 -104
  78. package/docs/frontend/components.md +0 -286
  79. package/docs/frontend/data-fetching.md +0 -260
  80. package/docs/frontend/images.md +0 -148
  81. package/docs/frontend/metadata.md +0 -344
  82. package/docs/frontend/navigation.md +0 -296
  83. package/docs/frontend/rendering.md +0 -236
  84. package/docs/frontend/routing.md +0 -346
  85. package/docs/frontend/scripts.md +0 -118
  86. package/docs/frontend/search.md +0 -191
  87. package/docs/frontend/styling.md +0 -147
  88. package/docs/frontend/toil-global.md +0 -216
  89. package/docs/getting-started/README.md +0 -81
  90. package/docs/getting-started/create-project.md +0 -131
  91. package/docs/getting-started/deploy.md +0 -101
  92. package/docs/getting-started/first-app.md +0 -215
  93. package/docs/getting-started/installation.md +0 -124
  94. package/docs/getting-started/migrating.md +0 -125
  95. package/docs/getting-started/project-structure.md +0 -163
  96. package/docs/introduction/README.md +0 -66
  97. package/docs/introduction/design-principles.md +0 -81
  98. package/docs/introduction/distributed.md +0 -91
  99. package/docs/introduction/how-it-works.md +0 -121
  100. package/docs/introduction/hyperscale.md +0 -70
  101. package/docs/introduction/modern-stack.md +0 -85
  102. package/docs/introduction/vs-other-frameworks.md +0 -64
  103. package/docs/introduction/why-toil.md +0 -99
  104. package/docs/llms.txt +0 -95
  105. package/docs/realtime/README.md +0 -102
  106. package/docs/realtime/channels.md +0 -211
  107. package/docs/realtime/streams.md +0 -369
  108. package/docs/services/README.md +0 -60
  109. package/docs/services/analytics.md +0 -279
  110. package/docs/services/caching.md +0 -175
  111. package/docs/services/cookies.md +0 -235
  112. package/docs/services/crypto.md +0 -209
  113. package/docs/services/email.md +0 -289
  114. package/docs/services/environment.md +0 -141
  115. package/docs/services/ratelimit.md +0 -174
  116. package/docs/services/time.md +0 -85
  117. package/eslint.config.js +0 -57
  118. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  119. package/presets/eslint.js +0 -88
  120. package/src/backend/index.ts +0 -204
  121. package/src/cli/configure.ts +0 -363
  122. package/src/cli/create.ts +0 -924
  123. package/src/cli/db.ts +0 -158
  124. package/src/cli/diagnostics.ts +0 -665
  125. package/src/cli/doctor.ts +0 -982
  126. package/src/cli/features.ts +0 -170
  127. package/src/cli/index.ts +0 -326
  128. package/src/cli/notify.ts +0 -120
  129. package/src/cli/proc.ts +0 -56
  130. package/src/cli/ui.ts +0 -169
  131. package/src/cli/update.ts +0 -248
  132. package/src/cli/updates.ts +0 -92
  133. package/src/cli/validate.ts +0 -31
  134. package/src/cli/version-check.ts +0 -125
  135. package/src/client/auth.ts +0 -869
  136. package/src/client/channel/channel.ts +0 -146
  137. package/src/client/components/Form.tsx +0 -65
  138. package/src/client/components/Image.tsx +0 -160
  139. package/src/client/components/Script.tsx +0 -113
  140. package/src/client/components/Slot.tsx +0 -21
  141. package/src/client/dev/devtools.tsx +0 -1089
  142. package/src/client/dev/error-overlay.tsx +0 -225
  143. package/src/client/errors.ts +0 -11
  144. package/src/client/head/head.ts +0 -182
  145. package/src/client/head/metadata.ts +0 -110
  146. package/src/client/index.ts +0 -130
  147. package/src/client/navigation/Link.tsx +0 -108
  148. package/src/client/navigation/NavLink.tsx +0 -86
  149. package/src/client/navigation/navigation.ts +0 -235
  150. package/src/client/navigation/prefetch.ts +0 -169
  151. package/src/client/navigation/scroll.ts +0 -53
  152. package/src/client/routing/Router.tsx +0 -216
  153. package/src/client/routing/action.ts +0 -122
  154. package/src/client/routing/error-boundary.tsx +0 -43
  155. package/src/client/routing/hooks.ts +0 -140
  156. package/src/client/routing/lazy.ts +0 -77
  157. package/src/client/routing/loader.ts +0 -381
  158. package/src/client/routing/match.ts +0 -47
  159. package/src/client/routing/mount.tsx +0 -90
  160. package/src/client/routing/params-context.ts +0 -10
  161. package/src/client/routing/slot-context.ts +0 -7
  162. package/src/client/rpc.ts +0 -97
  163. package/src/client/search/search.ts +0 -189
  164. package/src/client/search/use-page-search.ts +0 -73
  165. package/src/client/ssr/markers.tsx +0 -172
  166. package/src/client/stream/client.ts +0 -237
  167. package/src/client/types.ts +0 -73
  168. package/src/compiler/auth-emails.ts +0 -312
  169. package/src/compiler/config.ts +0 -369
  170. package/src/compiler/docs.ts +0 -127
  171. package/src/compiler/email-preview.ts +0 -314
  172. package/src/compiler/emails.ts +0 -404
  173. package/src/compiler/fonts.ts +0 -89
  174. package/src/compiler/generate.ts +0 -471
  175. package/src/compiler/image-blur.ts +0 -42
  176. package/src/compiler/image-report.ts +0 -88
  177. package/src/compiler/index.ts +0 -1362
  178. package/src/compiler/pages.ts +0 -49
  179. package/src/compiler/plugin.ts +0 -391
  180. package/src/compiler/prerender.ts +0 -263
  181. package/src/compiler/routes.ts +0 -170
  182. package/src/compiler/seo.ts +0 -415
  183. package/src/compiler/sri.ts +0 -144
  184. package/src/compiler/ssg.ts +0 -177
  185. package/src/compiler/ssr-codegen.ts +0 -85
  186. package/src/compiler/template-build.ts +0 -676
  187. package/src/compiler/template.ts +0 -262
  188. package/src/compiler/toil-docs.generated.ts +0 -77
  189. package/src/compiler/vite.ts +0 -245
  190. package/src/devserver/analytics/index.ts +0 -317
  191. package/src/devserver/config/dotenv.ts +0 -102
  192. package/src/devserver/config/env.ts +0 -30
  193. package/src/devserver/config/ratelimit.ts +0 -123
  194. package/src/devserver/daemon/catalog.ts +0 -120
  195. package/src/devserver/daemon/cron.ts +0 -87
  196. package/src/devserver/daemon/host.ts +0 -134
  197. package/src/devserver/daemon/index.ts +0 -369
  198. package/src/devserver/daemon/runtime.ts +0 -48
  199. package/src/devserver/db/catalog.ts +0 -109
  200. package/src/devserver/db/database.ts +0 -1834
  201. package/src/devserver/db/derives.ts +0 -121
  202. package/src/devserver/db/index.ts +0 -20
  203. package/src/devserver/db/routeKinds.ts +0 -191
  204. package/src/devserver/db/types.ts +0 -168
  205. package/src/devserver/email/caps.ts +0 -0
  206. package/src/devserver/email/config.ts +0 -141
  207. package/src/devserver/email/console.ts +0 -348
  208. package/src/devserver/email/index.ts +0 -155
  209. package/src/devserver/email/providers.ts +0 -131
  210. package/src/devserver/email/status.ts +0 -23
  211. package/src/devserver/email/validate.ts +0 -37
  212. package/src/devserver/email/wire.ts +0 -55
  213. package/src/devserver/http/cache.ts +0 -0
  214. package/src/devserver/http/envelope.ts +0 -157
  215. package/src/devserver/http/proxy.ts +0 -172
  216. package/src/devserver/http/runtime.ts +0 -387
  217. package/src/devserver/index.ts +0 -43
  218. package/src/devserver/production-ipc.ts +0 -63
  219. package/src/devserver/production-worker.ts +0 -83
  220. package/src/devserver/production.ts +0 -718
  221. package/src/devserver/runtime/crypto.ts +0 -587
  222. package/src/devserver/runtime/host.ts +0 -427
  223. package/src/devserver/runtime/module.ts +0 -442
  224. package/src/devserver/server.ts +0 -251
  225. package/src/devserver/ssr.ts +0 -208
  226. package/src/devserver/stream/catalog.ts +0 -106
  227. package/src/devserver/stream/host.ts +0 -42
  228. package/src/devserver/stream/index.ts +0 -301
  229. package/src/devserver/stream/manager.ts +0 -167
  230. package/src/devserver/stream/router.ts +0 -101
  231. package/src/devserver/stream/wire.ts +0 -58
  232. package/src/devserver/stream/ws.ts +0 -78
  233. package/src/devserver/wasm/sections.ts +0 -59
  234. package/src/devserver/wasm/surface.ts +0 -115
  235. package/src/io/FastMap.ts +0 -151
  236. package/src/io/FastSet.ts +0 -113
  237. package/src/io/codec.ts +0 -362
  238. package/src/io/index.ts +0 -10
  239. package/src/io/lengths.ts +0 -14
  240. package/src/io/types.ts +0 -19
  241. package/src/logger/index.ts +0 -22
  242. package/src/shared/index.ts +0 -46
  243. package/test/analytics-dev.test.ts +0 -113
  244. package/test/assembly/aspect-shim.ts +0 -33
  245. package/test/assembly/cookie.spec.ts +0 -303
  246. package/test/assembly/example.spec.ts +0 -22
  247. package/test/assembly/ssr.spec.ts +0 -95
  248. package/test/assembly.test.ts +0 -59
  249. package/test/auth-emails.test.ts +0 -118
  250. package/test/built-ssr.test.ts +0 -148
  251. package/test/channel.test.ts +0 -21
  252. package/test/config-host-port.test.ts +0 -59
  253. package/test/configure.test.ts +0 -94
  254. package/test/daemon-build.test.ts +0 -278
  255. package/test/daemon-catalog.test.ts +0 -349
  256. package/test/daemon-emulation.test.ts +0 -256
  257. package/test/db.test.ts +0 -0
  258. package/test/devserver-database.test.ts +0 -1102
  259. package/test/devserver-email.test.ts +0 -306
  260. package/test/devserver-pqauth.test.ts +0 -93
  261. package/test/devserver-secrets.test.ts +0 -63
  262. package/test/devserver.test.ts +0 -259
  263. package/test/doctor.test.ts +0 -298
  264. package/test/dom/Image.test.tsx +0 -144
  265. package/test/dom/Link.test.tsx +0 -67
  266. package/test/dom/NavLink.test.tsx +0 -37
  267. package/test/dom/Script.test.tsx +0 -48
  268. package/test/dom/action.test.tsx +0 -146
  269. package/test/dom/error-overlay.test.tsx +0 -44
  270. package/test/dom/loader.test.tsx +0 -121
  271. package/test/dom/navigation.test.ts +0 -59
  272. package/test/dom/revalidate.test.tsx +0 -38
  273. package/test/dom/route-head.test.tsx +0 -56
  274. package/test/dom/router-loading.test.tsx +0 -44
  275. package/test/dom/scroll.test.ts +0 -56
  276. package/test/dom/slot.test.tsx +0 -131
  277. package/test/dom/use-metadata.test.tsx +0 -58
  278. package/test/dom/view-transitions.test.tsx +0 -53
  279. package/test/dynamic-routing.test.ts +0 -132
  280. package/test/email-console.test.ts +0 -86
  281. package/test/email-preview.test.ts +0 -73
  282. package/test/emails.test.ts +0 -58
  283. package/test/errors.test.ts +0 -21
  284. package/test/example-guestbook.test.ts +0 -123
  285. package/test/features.test.ts +0 -149
  286. package/test/fixtures/bignum-wire/spec.ts +0 -25
  287. package/test/fixtures/daemon-app.ts +0 -65
  288. package/test/fixtures/stream-echo.ts +0 -26
  289. package/test/fixtures/stream-gate.ts +0 -28
  290. package/test/fixtures/stream-trap.ts +0 -18
  291. package/test/fixtures/stream-typed.ts +0 -41
  292. package/test/fonts.test.ts +0 -28
  293. package/test/global-setup.ts +0 -17
  294. package/test/head.test.ts +0 -42
  295. package/test/image-blur.test.ts +0 -50
  296. package/test/io.test.ts +0 -117
  297. package/test/metadata.test.ts +0 -40
  298. package/test/navlink.test.ts +0 -28
  299. package/test/pages.test.ts +0 -105
  300. package/test/placeholder.test.ts +0 -9
  301. package/test/pqauth-e2e.test.ts +0 -216
  302. package/test/pqauth-email.test.ts +0 -801
  303. package/test/prerender.test.ts +0 -116
  304. package/test/prettier-plugin.test.ts +0 -46
  305. package/test/routes.test.ts +0 -76
  306. package/test/rpc-bignum-wire.test.ts +0 -164
  307. package/test/rpc-dispatch.test.ts +0 -132
  308. package/test/rpc-kinds.test.ts +0 -18
  309. package/test/rpc.test.ts +0 -66
  310. package/test/search.test.ts +0 -114
  311. package/test/seo.test.ts +0 -175
  312. package/test/slot-layouts.test.ts +0 -69
  313. package/test/sri.test.ts +0 -129
  314. package/test/ssg.test.ts +0 -36
  315. package/test/ssr-hydration.test.tsx +0 -122
  316. package/test/ssr-render.test.ts +0 -197
  317. package/test/ssr-template.test.tsx +0 -482
  318. package/test/stream-emulation.test.ts +0 -479
  319. package/test/ui.test.ts +0 -40
  320. package/test/update.test.ts +0 -58
  321. package/test/validate.test.ts +0 -42
  322. package/test/version-check.test.ts +0 -130
  323. package/tests/data-parity/generated-parity.ts +0 -99
  324. package/tests/data-parity/parity.ts +0 -80
  325. package/tests/data-parity/spec.ts +0 -46
@@ -1,108 +0,0 @@
1
- import type { ComponentPropsWithRef, FocusEvent, MouseEvent, PointerEvent, ReactNode } from 'react';
2
-
3
- import { navigate } from './navigation.js';
4
- import { prefetch } from './prefetch.js';
5
- import type { Href } from '../types.js';
6
-
7
- /**
8
- * Props for {@link Link}: every standard `<a>` attribute (`rel`, `target`, `download`,
9
- * `referrerPolicy`, `hrefLang`, `className`, `style`, `ref`, `data-*`, `aria-*`, event handlers …)
10
- * plus toil's `replace` and `prefetch` controls. `href` is required and typed to the project's routes.
11
- */
12
- export interface LinkProps extends Omit<ComponentPropsWithRef<'a'>, 'href'> {
13
- /** Destination. Same-origin hrefs navigate client-side; external / `target` / `download` / `#hash` use the browser. */
14
- href: Href;
15
- /** Replace the current history entry instead of pushing a new one. Default `false`. */
16
- replace?: boolean;
17
- /** Scroll to top after navigating. Default `true`. */
18
- scroll?: boolean;
19
- /** Prefetch the route chunk on hover/focus. Default `true`; `false` opts this link out. */
20
- prefetch?: boolean;
21
- }
22
-
23
- /** True for cross-origin, opaque (`mailto:` / `tel:`), or otherwise non-same-origin hrefs. */
24
- function isExternalHref(href: string): boolean {
25
- try {
26
- return new URL(href, window.location.href).origin !== window.location.origin;
27
- } catch {
28
- return true;
29
- }
30
- }
31
-
32
- /**
33
- * Whether `href` is a usable client-navigation target. A data-driven href (`` `routeMap[key]` ``)
34
- * can resolve to `undefined` / `null` / `''` at runtime when the target page doesn't exist; the
35
- * resulting anchor is inert, so we must not intercept its click, calling `href.startsWith` /
36
- * `navigate` on a missing href would throw. `Href` is always a string at the type level, hence the
37
- * `unknown` parameter, the guard is purely a runtime safety net.
38
- */
39
- function isNavigableHref(href: unknown): href is string {
40
- return typeof href === 'string' && href !== '';
41
- }
42
-
43
- /**
44
- * Client-side navigation link. Forwards all anchor attributes to the underlying `<a>`, and
45
- * prefetches the target route's chunk on hover/focus. Intercepts only plain same-origin clicks ,
46
- * modified clicks, `target=_blank`, `download`, in-page `#hash`, and external URLs fall through to
47
- * native browser behavior.
48
- */
49
- export function Link(props: LinkProps): ReactNode {
50
- const {
51
- href,
52
- replace = false,
53
- scroll = true,
54
- prefetch: prefetchProp = true,
55
- onClick,
56
- onPointerEnter,
57
- onFocus,
58
- children,
59
- ...rest
60
- } = props;
61
-
62
- const handleClick = (event: MouseEvent<HTMLAnchorElement>): void => {
63
- onClick?.(event);
64
- if (
65
- event.defaultPrevented ||
66
- // A missing href (the target page doesn't exist) makes the anchor inert: leave the click
67
- // to the browser instead of intercepting it. Checked before `startsWith` so it can't throw.
68
- !isNavigableHref(href) ||
69
- event.button !== 0 ||
70
- event.metaKey ||
71
- event.ctrlKey ||
72
- event.shiftKey ||
73
- event.altKey ||
74
- (rest.target !== undefined && rest.target !== '_self') ||
75
- rest.download !== undefined ||
76
- href.startsWith('#') ||
77
- isExternalHref(href)
78
- ) {
79
- return;
80
- }
81
- event.preventDefault();
82
- navigate(href, { replace, scroll });
83
- };
84
-
85
- const warm = (): void => {
86
- if (prefetchProp && isNavigableHref(href)) prefetch(href);
87
- };
88
- const handlePointerEnter = (event: PointerEvent<HTMLAnchorElement>): void => {
89
- onPointerEnter?.(event);
90
- warm();
91
- };
92
- const handleFocus = (event: FocusEvent<HTMLAnchorElement>): void => {
93
- onFocus?.(event);
94
- warm();
95
- };
96
-
97
- return (
98
- <a
99
- {...rest}
100
- {...(prefetchProp ? {} : { 'data-no-prefetch': '' })}
101
- href={href}
102
- onClick={handleClick}
103
- onPointerEnter={handlePointerEnter}
104
- onFocus={handleFocus}>
105
- {children}
106
- </a>
107
- );
108
- }
@@ -1,86 +0,0 @@
1
- import type { CSSProperties, ReactNode } from 'react';
2
-
3
- import { useLocation } from '../routing/hooks.js';
4
- import { Link, type LinkProps } from './Link.js';
5
-
6
- /** State passed to `NavLink`'s function-form `className` / `style` / `children`. */
7
- export interface NavLinkState {
8
- readonly isActive: boolean;
9
- }
10
-
11
- /**
12
- * Props for {@link NavLink}: all {@link LinkProps}, but `className` / `style` / `children` may also
13
- * be functions of the active state.
14
- */
15
- export interface NavLinkProps extends Omit<LinkProps, 'className' | 'style' | 'children'> {
16
- className?: string | ((state: NavLinkState) => string | undefined);
17
- style?: CSSProperties | ((state: NavLinkState) => CSSProperties | undefined);
18
- children?: ReactNode | ((state: NavLinkState) => ReactNode);
19
- /** Match `href` exactly; without it, sub-paths are also active. Default `false`. */
20
- end?: boolean;
21
- /** Class added when active (used with a string `className`). Default `"active"`. */
22
- activeClassName?: string;
23
- }
24
-
25
- function normalizePath(p: string): string {
26
- return p.length > 1 ? p.replace(/\/+$/, '') : p;
27
- }
28
-
29
- /**
30
- * Whether a link to `linkPath` is active for `currentPath`. Exact when `end`; otherwise a parent
31
- * path is active for its sub-paths (and `/` is active everywhere, matching React Router).
32
- */
33
- export function matchActive(linkPath: string, currentPath: string, end: boolean): boolean {
34
- const link = normalizePath(linkPath);
35
- const current = normalizePath(currentPath);
36
- if (current === link) return true;
37
- if (end) return false;
38
- if (link === '/') return true;
39
- return current.startsWith(link + '/');
40
- }
41
-
42
- /**
43
- * A {@link Link} that knows whether it points at the current location. Applies an active class
44
- * (default `"active"`) and `aria-current="page"` when active; `className` / `style` / `children`
45
- * may be functions of `{ isActive }`. Inherits Link's full anchor API and prefetching.
46
- */
47
- export function NavLink(props: NavLinkProps): ReactNode {
48
- const {
49
- href,
50
- className,
51
- style,
52
- children,
53
- end = false,
54
- activeClassName = 'active',
55
- ...rest
56
- } = props;
57
- const pathname = useLocation();
58
-
59
- let linkPath = href;
60
- try {
61
- linkPath = new URL(href, window.location.href).pathname;
62
- } catch {
63
- linkPath = href;
64
- }
65
- const isActive = matchActive(linkPath, pathname, end);
66
- const state: NavLinkState = { isActive };
67
-
68
- const resolvedClassName =
69
- typeof className === 'function'
70
- ? className(state)
71
- : [className, isActive ? activeClassName : undefined].filter(Boolean).join(' ') ||
72
- undefined;
73
- const resolvedStyle = typeof style === 'function' ? style(state) : style;
74
- const resolvedChildren = typeof children === 'function' ? children(state) : children;
75
-
76
- return (
77
- <Link
78
- {...rest}
79
- href={href}
80
- className={resolvedClassName}
81
- style={resolvedStyle}
82
- aria-current={isActive ? 'page' : undefined}>
83
- {resolvedChildren}
84
- </Link>
85
- );
86
- }
@@ -1,235 +0,0 @@
1
- /**
2
- * History-based navigation core. Owns the location subscribers, the single `popstate` handler, and
3
- * the per-entry history keys used for scroll restoration. Consumed by `useLocation` (to re-render),
4
- * `Link` / `navigate` (to change location), and `Router` (which calls `applyScroll` after commit).
5
- */
6
- import { startTransition } from 'react';
7
- import { flushSync } from 'react-dom';
8
-
9
- import { enableManualScrollRestoration, planScroll, rememberScroll } from './scroll.js';
10
- import type { Href } from '../types.js';
11
-
12
- /**
13
- * Asserts a runtime-computed string is a valid {@link Href}, the escape hatch for hrefs built from
14
- * data (e.g. `` `/${category}/${slug}` ``) that can't be checked against the static route union.
15
- * Use at the call site: `<Link href={href(path)} />`, `navigate(href(path))`.
16
- */
17
- export const href = (path: string): Href => path as Href;
18
-
19
- const listeners = new Set<() => void>();
20
- let popstateBound = false;
21
-
22
- /** `document.startViewTransition`, present only where the View Transitions API is supported. */
23
- interface ViewTransitionDocument {
24
- startViewTransition?: (callback: () => void) => unknown;
25
- }
26
- let viewTransitions = false;
27
-
28
- /** Enables animated View Transitions for navigation. Called once by `mount` from `client.viewTransitions`. */
29
- export function setViewTransitions(enabled: boolean): void {
30
- viewTransitions = enabled;
31
- }
32
-
33
- // Whether to wrap navigations in a React transition. Off by default: a navigation commits eagerly, so
34
- // a route's `loading.tsx` shows immediately while its loader runs. On, the current page is kept
35
- // visible until the next route is ready (smoother, but no loading state). Set from `client.transitions`.
36
- let navTransitions = false;
37
-
38
- /** Enables React-transition (keep-current-page) navigation. Called once by `mount` from `client.transitions`. */
39
- export function setTransitions(enabled: boolean): void {
40
- navTransitions = enabled;
41
- }
42
-
43
- /** Whether the current navigation should animate via the View Transitions API. */
44
- function shouldViewTransition(): boolean {
45
- if (!viewTransitions || typeof document === 'undefined' || typeof window === 'undefined') {
46
- return false;
47
- }
48
- if (typeof (document as ViewTransitionDocument).startViewTransition !== 'function')
49
- return false;
50
- return !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
51
- }
52
-
53
- interface ToilHistoryState {
54
- __toilKey?: string;
55
- }
56
- let keyCounter = 0;
57
- let currentKey = 'initial';
58
- function nextKey(): string {
59
- keyCounter += 1;
60
- return `t${String(keyCounter)}`;
61
- }
62
-
63
- function runListeners(): void {
64
- for (const listener of listeners) listener();
65
- }
66
-
67
- /**
68
- * Re-renders subscribers for a location change. When View Transitions are enabled and supported, the
69
- * commit runs synchronously inside `document.startViewTransition` so the browser animates the old and
70
- * new DOM. Otherwise, with `client.transitions` on, it's wrapped in `startTransition` (the current
71
- * page stays while the next route loads); by default it commits eagerly, so a route's `loading.tsx`
72
- * shows right away instead of holding the previous page.
73
- */
74
- function notify(): void {
75
- if (shouldViewTransition()) {
76
- (document as ViewTransitionDocument).startViewTransition?.(() => {
77
- flushSync(runListeners);
78
- });
79
- } else if (navTransitions) {
80
- startTransition(runListeners);
81
- } else {
82
- runListeners();
83
- }
84
- }
85
-
86
- // Soft vs hard navigation, for intercepting routes. The initial page load (and any full refresh) is
87
- // "hard"; client navigations (`navigate` / back / forward) are "soft". `previousPath` is the path we
88
- // were on before the latest soft navigation, the route the main view keeps showing while an
89
- // intercepting route fills a slot (the modal overlay).
90
- let softNav = false;
91
- let currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
92
- let previousPath = currentPath;
93
-
94
- /** Records a transition to the live location; `soft` is false only for the initial load. */
95
- function recordTransition(soft: boolean): void {
96
- previousPath = currentPath;
97
- currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
98
- softNav = soft;
99
- }
100
-
101
- /** Whether the current location was reached by a client navigation (not an initial load / refresh). */
102
- export function isSoftNavigation(): boolean {
103
- return softNav;
104
- }
105
-
106
- /** The path the app was on before the latest navigation (what the main view keeps during an intercept). */
107
- export function previousPathname(): string {
108
- return previousPath;
109
- }
110
-
111
- // Navigation-pending tracking: a navigation is "pending" from when it starts until the new route
112
- // commits. Drives useNavigationPending() (e.g. a top loading bar).
113
- let startedTick = 0;
114
- let committedTick = 0;
115
- const pendingListeners = new Set<() => void>();
116
- function emitPending(): void {
117
- for (const listener of pendingListeners) listener();
118
- }
119
- function beginNavigation(): void {
120
- startedTick += 1;
121
- emitPending();
122
- }
123
-
124
- /** Marks the in-flight navigation as committed. Called by `Router` after each commit. */
125
- export function settleNavigation(): void {
126
- if (committedTick !== startedTick) {
127
- committedTick = startedTick;
128
- emitPending();
129
- }
130
- }
131
-
132
- /** Whether a navigation is in flight (started but not yet committed). */
133
- export function isNavigationPending(): boolean {
134
- return startedTick !== committedTick;
135
- }
136
-
137
- /** Monotonic id incremented on each navigation, used to key/revalidate per-navigation route data. */
138
- export function navigationEpoch(): number {
139
- return startedTick;
140
- }
141
-
142
- /** Subscribes to navigation-pending changes; returns an unsubscribe function. */
143
- export function subscribePending(listener: () => void): () => void {
144
- pendingListeners.add(listener);
145
- return () => {
146
- pendingListeners.delete(listener);
147
- };
148
- }
149
-
150
- /** Options for {@link navigate}. */
151
- export interface NavigateOptions {
152
- /** Replace the current history entry instead of pushing a new one. Default `false`. */
153
- readonly replace?: boolean;
154
- /** Scroll to the top of the page after navigating. Default `true`. */
155
- readonly scroll?: boolean;
156
- }
157
-
158
- /** Initializes manual scroll restoration and the initial history key. Called once by `mount`. */
159
- export function initNavigation(): void {
160
- enableManualScrollRestoration();
161
- const state = window.history.state as ToilHistoryState | null;
162
- if (state?.__toilKey) {
163
- currentKey = state.__toilKey;
164
- } else {
165
- currentKey = nextKey();
166
- window.history.replaceState({ ...state, __toilKey: currentKey }, '');
167
- }
168
- }
169
-
170
- /** Navigates to `href` without a full page reload (history push/replace + subscriber re-render). */
171
- export function navigate(href: Href, options?: NavigateOptions): void {
172
- beginNavigation();
173
- rememberScroll(currentKey);
174
- let hash = '';
175
- try {
176
- hash = new URL(href, window.location.href).hash;
177
- } catch {
178
- hash = '';
179
- }
180
- if (options?.replace) {
181
- window.history.replaceState({ __toilKey: currentKey }, '', href);
182
- } else {
183
- currentKey = nextKey();
184
- window.history.pushState({ __toilKey: currentKey }, '', href);
185
- }
186
- recordTransition(true);
187
- planScroll({ hash, toTop: options?.scroll !== false });
188
- notify();
189
- }
190
-
191
- /** Goes back one entry in history (fires `popstate`, which notifies subscribers). */
192
- export function back(): void {
193
- window.history.back();
194
- }
195
-
196
- /** Goes forward one entry in history. */
197
- export function forward(): void {
198
- window.history.forward();
199
- }
200
-
201
- /**
202
- * Re-renders the current route, bumping the navigation epoch so a revalidation of the *same* URL
203
- * re-keys its Suspense boundary (its `loading.tsx` shows while the loader re-runs) and
204
- * `useNavigationPending` reports the in-flight refetch, instead of silently freezing the old page.
205
- */
206
- export function refresh(): void {
207
- beginNavigation();
208
- notify();
209
- }
210
-
211
- /** Handles browser back/forward: restores the saved scroll for the target entry, then re-renders. */
212
- function handlePopState(event: PopStateEvent): void {
213
- beginNavigation();
214
- rememberScroll(currentKey);
215
- const state = event.state as ToilHistoryState | null;
216
- currentKey = state?.__toilKey ?? 'initial';
217
- recordTransition(true);
218
- planScroll({ restoreKey: currentKey, hash: window.location.hash, toTop: false });
219
- notify();
220
- }
221
-
222
- /**
223
- * Subscribes `listener` to location changes and returns an unsubscribe function. Browser
224
- * back/forward is wired once, on the first subscription, via a shared `popstate` handler.
225
- */
226
- export function subscribeLocation(listener: () => void): () => void {
227
- if (!popstateBound) {
228
- window.addEventListener('popstate', handlePopState);
229
- popstateBound = true;
230
- }
231
- listeners.add(listener);
232
- return () => {
233
- listeners.delete(listener);
234
- };
235
- }
@@ -1,169 +0,0 @@
1
- import { prefetchRouteData } from '../routing/loader.js';
2
- import { matchRoute } from '../routing/match.js';
3
- import type { RouteDef } from '../types.js';
4
-
5
- declare global {
6
- interface Navigator {
7
- /** Non-standard but widely shipped; used to skip prefetch on data-saver / slow links. */
8
- readonly connection?: {
9
- readonly saveData?: boolean;
10
- readonly effectiveType?: string;
11
- };
12
- }
13
- }
14
-
15
- let routeTable: RouteDef[] = [];
16
- const warmed = new WeakSet<RouteDef>();
17
- const dataWarmed = new Set<string>();
18
- let io: IntersectionObserver | null = null;
19
- let mo: MutationObserver | null = null;
20
-
21
- /** Resolves a same-origin `href` to a registered route, or `null` for external/unknown targets. */
22
- function routeForHref(href: string): RouteDef | null {
23
- let url: URL;
24
- try {
25
- url = new URL(href, window.location.href);
26
- } catch {
27
- return null;
28
- }
29
- if (url.origin !== window.location.origin) return null;
30
- for (const route of routeTable) {
31
- if (matchRoute(route.pattern, url.pathname)) return route;
32
- }
33
- return null;
34
- }
35
-
36
- /**
37
- * Warms a route's lazy chunk by triggering its loader once. Best-effort: each route loads at most
38
- * once, and a failed load is forgotten (so the real navigation can retry and surface the error).
39
- */
40
- function warm(route: RouteDef): void {
41
- if (warmed.has(route)) return;
42
- warmed.add(route);
43
- void route.load().catch(() => {
44
- warmed.delete(route);
45
- });
46
- }
47
-
48
- /**
49
- * Prefetches the route chunk for an internal `href` so a later navigation resolves instantly.
50
- * No-op for external, unknown, or already-prefetched targets, safe to call from anywhere,
51
- * including before an imperative {@link navigate} (e.g. `prefetch('/dashboard')` on hover/intent).
52
- */
53
- export function prefetch(href: string): void {
54
- const route = routeForHref(href);
55
- if (route) warm(route);
56
- }
57
-
58
- /**
59
- * Prefetches both the chunk and the loader data for an internal `href`, so a later navigation
60
- * commits synchronously (no suspense, no held-page wait). Called on link hover / focus intent, where
61
- * running the route's loader early is worth it. No-op for external/unknown/already-warmed targets.
62
- */
63
- export function prefetchData(href: string): void {
64
- let url: URL;
65
- try {
66
- url = new URL(href, window.location.href);
67
- } catch {
68
- return;
69
- }
70
- if (url.origin !== window.location.origin) return;
71
- const key = url.pathname + url.search;
72
- if (dataWarmed.has(key)) return;
73
- for (const route of routeTable) {
74
- const params = matchRoute(route.pattern, url.pathname);
75
- if (params) {
76
- dataWarmed.add(key);
77
- warm(route);
78
- prefetchRouteData(route, params, url.pathname, url.search);
79
- return;
80
- }
81
- }
82
- }
83
-
84
- /** Anchors to skip even when internal: new-tab, downloads, or an explicit `data-no-prefetch` opt-out. */
85
- function isPrefetchable(a: HTMLAnchorElement): boolean {
86
- if (a.target && a.target !== '_self') return false;
87
- if (a.hasAttribute('download')) return false;
88
- if (a.dataset.noPrefetch !== undefined) return false;
89
- return true;
90
- }
91
-
92
- /** Observes an anchor for viewport entry if it points at a known internal route. */
93
- function observeAnchor(a: HTMLAnchorElement): void {
94
- if (!io || !isPrefetchable(a) || !routeForHref(a.href)) return;
95
- io.observe(a);
96
- }
97
-
98
- /** Finds and observes every `<a href>` under `root`. */
99
- function scan(root: ParentNode): void {
100
- root.querySelectorAll('a[href]').forEach((el) => {
101
- if (el instanceof HTMLAnchorElement) observeAnchor(el);
102
- });
103
- }
104
-
105
- /** Skip prefetching on data-saver mode or 2g-class connections, where bandwidth is precious. */
106
- function shouldSkipForConnection(): boolean {
107
- const c = navigator.connection;
108
- if (!c) return false;
109
- return c.saveData === true || c.effectiveType === 'slow-2g' || c.effectiveType === '2g';
110
- }
111
-
112
- /**
113
- * Starts idle-time prefetching of internal links. As each `<a>` pointing at a known route scrolls
114
- * into view (or near it, 200px margin) its chunk is warmed once; links added later by client
115
- * navigation are picked up via a MutationObserver. Called by {@link mount}; runs once per app.
116
- */
117
- export function startPrefetcher(routes: RouteDef[]): void {
118
- routeTable = routes;
119
- if (
120
- typeof window === 'undefined' ||
121
- typeof IntersectionObserver === 'undefined' ||
122
- typeof MutationObserver === 'undefined' ||
123
- io
124
- ) {
125
- return;
126
- }
127
- if (shouldSkipForConnection()) return;
128
-
129
- io = new IntersectionObserver(
130
- (entries) => {
131
- for (const entry of entries) {
132
- if (!entry.isIntersecting) continue;
133
- const a = entry.target;
134
- if (a instanceof HTMLAnchorElement) {
135
- io?.unobserve(a);
136
- prefetch(a.href);
137
- }
138
- }
139
- },
140
- { rootMargin: '200px' },
141
- );
142
-
143
- mo = new MutationObserver((mutations) => {
144
- for (const m of mutations) {
145
- for (const node of m.addedNodes) {
146
- if (node instanceof HTMLAnchorElement) observeAnchor(node);
147
- else if (node instanceof Element) scan(node);
148
- }
149
- }
150
- });
151
-
152
- // Hover / focus intent: warm the chunk *and* run the loader for the target so the click commits
153
- // synchronously. Delegated on the document so links added later are covered automatically.
154
- const onIntent = (event: Event): void => {
155
- const target = event.target;
156
- if (!(target instanceof Element)) return;
157
- const a = target.closest('a[href]');
158
- if (a instanceof HTMLAnchorElement && isPrefetchable(a) && a.href) prefetchData(a.href);
159
- };
160
- document.addEventListener('pointerover', onIntent, { passive: true });
161
- document.addEventListener('focusin', onIntent);
162
-
163
- const begin = (): void => {
164
- scan(document);
165
- mo?.observe(document.body, { childList: true, subtree: true });
166
- };
167
- if (typeof requestIdleCallback === 'function') requestIdleCallback(begin);
168
- else setTimeout(begin, 200);
169
- }
@@ -1,53 +0,0 @@
1
- /**
2
- * Manual scroll management for client navigation: scroll to top on push navigations, restore the
3
- * saved position on back/forward, and honor in-page `#hash` targets. Positions are keyed by
4
- * history entry; {@link applyScroll} runs once after the navigation commits.
5
- */
6
- const positions = new Map<string, number>();
7
-
8
- interface ScrollPlan {
9
- readonly restore: number | null;
10
- readonly hash: string;
11
- readonly toTop: boolean;
12
- }
13
- let plan: ScrollPlan | null = null;
14
-
15
- /** Switches off the browser's automatic scroll restoration so the router can manage it. */
16
- export function enableManualScrollRestoration(): void {
17
- if (typeof window !== 'undefined' && 'scrollRestoration' in window.history) {
18
- window.history.scrollRestoration = 'manual';
19
- }
20
- }
21
-
22
- /** Saves the current scroll position for a history key (called before leaving an entry). */
23
- export function rememberScroll(key: string): void {
24
- positions.set(key, window.scrollY);
25
- }
26
-
27
- /** Plans what {@link applyScroll} should do after the next navigation commits. */
28
- export function planScroll(opts: { restoreKey?: string; hash: string; toTop: boolean }): void {
29
- plan = {
30
- restore: opts.restoreKey !== undefined ? (positions.get(opts.restoreKey) ?? 0) : null,
31
- hash: opts.hash,
32
- toTop: opts.toTop,
33
- };
34
- }
35
-
36
- /** Applies the pending scroll plan once: hash target, else restored position, else top. */
37
- export function applyScroll(): void {
38
- const current = plan;
39
- plan = null;
40
- if (!current) return;
41
- if (current.hash) {
42
- const el = document.getElementById(decodeURIComponent(current.hash.slice(1)));
43
- if (el) {
44
- el.scrollIntoView();
45
- return;
46
- }
47
- }
48
- if (current.restore !== null) {
49
- window.scrollTo(0, current.restore);
50
- return;
51
- }
52
- if (current.toTop) window.scrollTo(0, 0);
53
- }