toiljs 0.0.116 → 0.0.118
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 +14 -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/auth.d.ts +3 -0
- package/build/client/auth.js +19 -10
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/navigation/navigation.js +28 -3
- package/build/client/routing/Router.js +5 -2
- 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 +26 -5
- 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 +8 -7
- package/build/compiler/vite.js +4 -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/client/routes/pq.tsx +2 -1
- 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/auth/AuthController.ts +53 -14
- package/server/auth/FactorSettings.ts +41 -0
- package/server/auth/SessionEpochs.ts +58 -0
- package/server/auth/SessionRevocations.ts +13 -0
- package/server/globals/auth.ts +27 -2
- 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,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
|
-
}
|