toiljs 0.0.11 → 0.0.12

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 (119) hide show
  1. package/README.md +2 -0
  2. package/build/cli/.tsbuildinfo +1 -1
  3. package/build/cli/configure.js +10 -4
  4. package/build/cli/create.js +58 -30
  5. package/build/cli/diagnostics.d.ts +55 -0
  6. package/build/cli/diagnostics.js +333 -0
  7. package/build/cli/doctor.d.ts +6 -0
  8. package/build/cli/doctor.js +249 -0
  9. package/build/cli/index.js +26 -0
  10. package/build/cli/proc.d.ts +5 -0
  11. package/build/cli/proc.js +20 -0
  12. package/build/cli/ui.d.ts +1 -0
  13. package/build/cli/ui.js +1 -0
  14. package/build/cli/update.d.ts +7 -0
  15. package/build/cli/update.js +117 -0
  16. package/build/cli/updates.d.ts +10 -0
  17. package/build/cli/updates.js +45 -0
  18. package/build/client/.tsbuildinfo +1 -1
  19. package/build/client/dev/error-overlay.js +1 -1
  20. package/build/client/head/metadata.js +3 -1
  21. package/build/client/index.d.ts +5 -1
  22. package/build/client/index.js +2 -0
  23. package/build/client/navigation/navigation.js +1 -1
  24. package/build/client/routing/Router.js +2 -2
  25. package/build/client/search/search.d.ts +26 -0
  26. package/build/client/search/search.js +101 -0
  27. package/build/client/search/use-page-search.d.ts +8 -0
  28. package/build/client/search/use-page-search.js +21 -0
  29. package/build/compiler/.tsbuildinfo +1 -1
  30. package/build/compiler/generate.js +26 -23
  31. package/build/compiler/index.d.ts +2 -0
  32. package/build/compiler/index.js +1 -0
  33. package/build/compiler/pages.d.ts +8 -0
  34. package/build/compiler/pages.js +37 -0
  35. package/build/compiler/plugin.js +3 -1
  36. package/build/compiler/prerender.d.ts +1 -0
  37. package/build/compiler/prerender.js +11 -5
  38. package/build/compiler/seo.js +10 -3
  39. package/build/io/.tsbuildinfo +1 -1
  40. package/examples/basic/client/components/Header.tsx +43 -41
  41. package/examples/basic/client/components/HoneycombBackground.tsx +223 -230
  42. package/examples/basic/client/public/index.html +18 -16
  43. package/examples/basic/client/routes/(legal)/privacy.tsx +18 -19
  44. package/examples/basic/client/routes/(legal)/terms.tsx +15 -16
  45. package/examples/basic/client/routes/about.tsx +21 -22
  46. package/examples/basic/client/routes/blog/[id].tsx +26 -18
  47. package/examples/basic/client/routes/features/actions.tsx +67 -67
  48. package/examples/basic/client/routes/features/error/index.tsx +27 -27
  49. package/examples/basic/client/routes/features/head.tsx +38 -38
  50. package/examples/basic/client/routes/features/index.tsx +83 -75
  51. package/examples/basic/client/routes/features/realtime.tsx +34 -32
  52. package/examples/basic/client/routes/features/script.tsx +31 -31
  53. package/examples/basic/client/routes/features/seo.tsx +39 -39
  54. package/examples/basic/client/routes/features/template/index.tsx +20 -20
  55. package/examples/basic/client/routes/features/template/template.tsx +16 -18
  56. package/examples/basic/client/routes/gallery/@modal/(.)photo/[id].tsx +23 -23
  57. package/examples/basic/client/routes/gallery/index.tsx +42 -42
  58. package/examples/basic/client/routes/gallery/photo/[id].tsx +18 -18
  59. package/examples/basic/client/routes/get-started.tsx +157 -84
  60. package/examples/basic/client/routes/index.tsx +137 -96
  61. package/examples/basic/client/routes/loader-demo/index.tsx +59 -52
  62. package/examples/basic/client/routes/search.tsx +61 -0
  63. package/examples/basic/client/routes/test.tsx +7 -8
  64. package/examples/basic/client/styles/main.css +624 -552
  65. package/package.json +2 -2
  66. package/presets/eslint.js +10 -3
  67. package/src/cli/configure.ts +363 -353
  68. package/src/cli/create.ts +563 -530
  69. package/src/cli/diagnostics.ts +421 -0
  70. package/src/cli/doctor.ts +318 -0
  71. package/src/cli/features.ts +166 -160
  72. package/src/cli/index.ts +242 -211
  73. package/src/cli/proc.ts +30 -0
  74. package/src/cli/ui.ts +111 -103
  75. package/src/cli/update.ts +150 -0
  76. package/src/cli/updates.ts +69 -0
  77. package/src/client/components/Image.tsx +91 -89
  78. package/src/client/dev/error-overlay.tsx +193 -197
  79. package/src/client/head/metadata.ts +94 -92
  80. package/src/client/index.ts +79 -64
  81. package/src/client/navigation/Link.tsx +94 -100
  82. package/src/client/navigation/navigation.ts +215 -218
  83. package/src/client/routing/Router.tsx +210 -193
  84. package/src/client/routing/hooks.ts +110 -114
  85. package/src/client/routing/lazy.ts +77 -81
  86. package/src/client/search/search.ts +189 -0
  87. package/src/client/search/use-page-search.ts +73 -0
  88. package/src/compiler/config.ts +173 -171
  89. package/src/compiler/fonts.ts +89 -87
  90. package/src/compiler/generate.ts +378 -373
  91. package/src/compiler/image-report.ts +88 -85
  92. package/src/compiler/index.ts +2 -0
  93. package/src/compiler/pages.ts +70 -0
  94. package/src/compiler/plugin.ts +51 -47
  95. package/src/compiler/prerender.ts +152 -130
  96. package/src/compiler/routes.ts +132 -131
  97. package/src/compiler/seo.ts +381 -356
  98. package/src/compiler/vite.ts +155 -145
  99. package/src/io/FastSet.ts +99 -96
  100. package/test/configure.test.ts +94 -90
  101. package/test/doctor.test.ts +140 -0
  102. package/test/dom/Image.test.tsx +73 -46
  103. package/test/dom/Script.test.tsx +48 -45
  104. package/test/dom/action.test.tsx +146 -129
  105. package/test/dom/error-overlay.test.tsx +44 -44
  106. package/test/dom/loader.test.tsx +2 -2
  107. package/test/dom/revalidate.test.tsx +1 -1
  108. package/test/dom/route-head.test.tsx +1 -2
  109. package/test/dom/slot.test.tsx +131 -109
  110. package/test/dom/view-transitions.test.tsx +53 -51
  111. package/test/features.test.ts +149 -142
  112. package/test/fonts.test.ts +28 -26
  113. package/test/head.test.ts +45 -35
  114. package/test/metadata.test.ts +42 -41
  115. package/test/pages.test.ts +105 -0
  116. package/test/prerender.test.ts +54 -46
  117. package/test/search.test.ts +114 -0
  118. package/test/seo.test.ts +164 -142
  119. package/test/update.test.ts +44 -0
@@ -1,218 +1,215 @@
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 {
10
- enableManualScrollRestoration,
11
- planScroll,
12
- rememberScroll,
13
- } from './scroll.js';
14
- import type { Href } from '../types.js';
15
-
16
- const listeners = new Set<() => void>();
17
- let popstateBound = false;
18
-
19
- /** `document.startViewTransition`, present only where the View Transitions API is supported. */
20
- interface ViewTransitionDocument {
21
- startViewTransition?: (callback: () => void) => unknown;
22
- }
23
- let viewTransitions = false;
24
-
25
- /** Enables animated View Transitions for navigation. Called once by `mount` from `client.viewTransitions`. */
26
- export function setViewTransitions(enabled: boolean): void {
27
- viewTransitions = enabled;
28
- }
29
-
30
- /** Whether the current navigation should animate via the View Transitions API. */
31
- function shouldViewTransition(): boolean {
32
- if (!viewTransitions || typeof document === 'undefined' || typeof window === 'undefined') {
33
- return false;
34
- }
35
- if (typeof (document as ViewTransitionDocument).startViewTransition !== 'function') return false;
36
- return !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
37
- }
38
-
39
- interface ToilHistoryState {
40
- __toilKey?: string;
41
- }
42
- let keyCounter = 0;
43
- let currentKey = 'initial';
44
- function nextKey(): string {
45
- keyCounter += 1;
46
- return `t${String(keyCounter)}`;
47
- }
48
-
49
- function runListeners(): void {
50
- for (const listener of listeners) listener();
51
- }
52
-
53
- /**
54
- * Re-renders subscribers for a location change. Normally wrapped in `startTransition` (smooth: the
55
- * current page stays while the next route loads). When View Transitions are enabled and supported,
56
- * the commit runs synchronously inside `document.startViewTransition` so the browser animates the
57
- * old and new DOM (a crossfade, or shared-element transitions via `view-transition-name`).
58
- */
59
- function notify(): void {
60
- if (shouldViewTransition()) {
61
- (document as ViewTransitionDocument).startViewTransition?.(() => {
62
- flushSync(runListeners);
63
- });
64
- } else {
65
- startTransition(runListeners);
66
- }
67
- }
68
-
69
- // Soft vs hard navigation, for intercepting routes. The initial page load (and any full refresh) is
70
- // "hard"; client navigations (`navigate` / back / forward) are "soft". `previousPath` is the path we
71
- // were on before the latest soft navigation, the route the main view keeps showing while an
72
- // intercepting route fills a slot (the modal overlay).
73
- let softNav = false;
74
- let currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
75
- let previousPath = currentPath;
76
-
77
- /** Records a transition to the live location; `soft` is false only for the initial load. */
78
- function recordTransition(soft: boolean): void {
79
- previousPath = currentPath;
80
- currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
81
- softNav = soft;
82
- }
83
-
84
- /** Whether the current location was reached by a client navigation (not an initial load / refresh). */
85
- export function isSoftNavigation(): boolean {
86
- return softNav;
87
- }
88
-
89
- /** The path the app was on before the latest navigation (what the main view keeps during an intercept). */
90
- export function previousPathname(): string {
91
- return previousPath;
92
- }
93
-
94
- // Navigation-pending tracking: a navigation is "pending" from when it starts until the new route
95
- // commits. Drives useNavigationPending() (e.g. a top loading bar).
96
- let startedTick = 0;
97
- let committedTick = 0;
98
- const pendingListeners = new Set<() => void>();
99
- function emitPending(): void {
100
- for (const listener of pendingListeners) listener();
101
- }
102
- function beginNavigation(): void {
103
- startedTick += 1;
104
- emitPending();
105
- }
106
-
107
- /** Marks the in-flight navigation as committed. Called by `Router` after each commit. */
108
- export function settleNavigation(): void {
109
- if (committedTick !== startedTick) {
110
- committedTick = startedTick;
111
- emitPending();
112
- }
113
- }
114
-
115
- /** Whether a navigation is in flight (started but not yet committed). */
116
- export function isNavigationPending(): boolean {
117
- return startedTick !== committedTick;
118
- }
119
-
120
- /** Monotonic id incremented on each navigation, used to key/revalidate per-navigation route data. */
121
- export function navigationEpoch(): number {
122
- return startedTick;
123
- }
124
-
125
- /** Subscribes to navigation-pending changes; returns an unsubscribe function. */
126
- export function subscribePending(listener: () => void): () => void {
127
- pendingListeners.add(listener);
128
- return () => {
129
- pendingListeners.delete(listener);
130
- };
131
- }
132
-
133
- /** Options for {@link navigate}. */
134
- export interface NavigateOptions {
135
- /** Replace the current history entry instead of pushing a new one. Default `false`. */
136
- readonly replace?: boolean;
137
- /** Scroll to the top of the page after navigating. Default `true`. */
138
- readonly scroll?: boolean;
139
- }
140
-
141
- /** Initializes manual scroll restoration and the initial history key. Called once by `mount`. */
142
- export function initNavigation(): void {
143
- enableManualScrollRestoration();
144
- const state = window.history.state as ToilHistoryState | null;
145
- if (state?.__toilKey) {
146
- currentKey = state.__toilKey;
147
- } else {
148
- currentKey = nextKey();
149
- window.history.replaceState({ ...state, __toilKey: currentKey }, '');
150
- }
151
- }
152
-
153
- /** Navigates to `href` without a full page reload (history push/replace + subscriber re-render). */
154
- export function navigate(href: Href, options?: NavigateOptions): void {
155
- beginNavigation();
156
- rememberScroll(currentKey);
157
- let hash = '';
158
- try {
159
- hash = new URL(href, window.location.href).hash;
160
- } catch {
161
- hash = '';
162
- }
163
- if (options?.replace) {
164
- window.history.replaceState({ __toilKey: currentKey }, '', href);
165
- } else {
166
- currentKey = nextKey();
167
- window.history.pushState({ __toilKey: currentKey }, '', href);
168
- }
169
- recordTransition(true);
170
- planScroll({ hash, toTop: options?.scroll !== false });
171
- notify();
172
- }
173
-
174
- /** Goes back one entry in history (fires `popstate`, which notifies subscribers). */
175
- export function back(): void {
176
- window.history.back();
177
- }
178
-
179
- /** Goes forward one entry in history. */
180
- export function forward(): void {
181
- window.history.forward();
182
- }
183
-
184
- /**
185
- * Re-renders the current route, bumping the navigation epoch so a revalidation of the *same* URL
186
- * re-keys its Suspense boundary (its `loading.tsx` shows while the loader re-runs) and
187
- * `useNavigationPending` reports the in-flight refetch, instead of silently freezing the old page.
188
- */
189
- export function refresh(): void {
190
- beginNavigation();
191
- notify();
192
- }
193
-
194
- /** Handles browser back/forward: restores the saved scroll for the target entry, then re-renders. */
195
- function handlePopState(event: PopStateEvent): void {
196
- beginNavigation();
197
- rememberScroll(currentKey);
198
- const state = event.state as ToilHistoryState | null;
199
- currentKey = state?.__toilKey ?? 'initial';
200
- recordTransition(true);
201
- planScroll({ restoreKey: currentKey, hash: window.location.hash, toTop: false });
202
- notify();
203
- }
204
-
205
- /**
206
- * Subscribes `listener` to location changes and returns an unsubscribe function. Browser
207
- * back/forward is wired once, on the first subscription, via a shared `popstate` handler.
208
- */
209
- export function subscribeLocation(listener: () => void): () => void {
210
- if (!popstateBound) {
211
- window.addEventListener('popstate', handlePopState);
212
- popstateBound = true;
213
- }
214
- listeners.add(listener);
215
- return () => {
216
- listeners.delete(listener);
217
- };
218
- }
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
+ const listeners = new Set<() => void>();
13
+ let popstateBound = false;
14
+
15
+ /** `document.startViewTransition`, present only where the View Transitions API is supported. */
16
+ interface ViewTransitionDocument {
17
+ startViewTransition?: (callback: () => void) => unknown;
18
+ }
19
+ let viewTransitions = false;
20
+
21
+ /** Enables animated View Transitions for navigation. Called once by `mount` from `client.viewTransitions`. */
22
+ export function setViewTransitions(enabled: boolean): void {
23
+ viewTransitions = enabled;
24
+ }
25
+
26
+ /** Whether the current navigation should animate via the View Transitions API. */
27
+ function shouldViewTransition(): boolean {
28
+ if (!viewTransitions || typeof document === 'undefined' || typeof window === 'undefined') {
29
+ return false;
30
+ }
31
+ if (typeof (document as ViewTransitionDocument).startViewTransition !== 'function')
32
+ return false;
33
+ return !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
34
+ }
35
+
36
+ interface ToilHistoryState {
37
+ __toilKey?: string;
38
+ }
39
+ let keyCounter = 0;
40
+ let currentKey = 'initial';
41
+ function nextKey(): string {
42
+ keyCounter += 1;
43
+ return `t${String(keyCounter)}`;
44
+ }
45
+
46
+ function runListeners(): void {
47
+ for (const listener of listeners) listener();
48
+ }
49
+
50
+ /**
51
+ * Re-renders subscribers for a location change. Normally wrapped in `startTransition` (smooth: the
52
+ * current page stays while the next route loads). When View Transitions are enabled and supported,
53
+ * the commit runs synchronously inside `document.startViewTransition` so the browser animates the
54
+ * old and new DOM (a crossfade, or shared-element transitions via `view-transition-name`).
55
+ */
56
+ function notify(): void {
57
+ if (shouldViewTransition()) {
58
+ (document as ViewTransitionDocument).startViewTransition?.(() => {
59
+ flushSync(runListeners);
60
+ });
61
+ } else {
62
+ startTransition(runListeners);
63
+ }
64
+ }
65
+
66
+ // Soft vs hard navigation, for intercepting routes. The initial page load (and any full refresh) is
67
+ // "hard"; client navigations (`navigate` / back / forward) are "soft". `previousPath` is the path we
68
+ // were on before the latest soft navigation, the route the main view keeps showing while an
69
+ // intercepting route fills a slot (the modal overlay).
70
+ let softNav = false;
71
+ let currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
72
+ let previousPath = currentPath;
73
+
74
+ /** Records a transition to the live location; `soft` is false only for the initial load. */
75
+ function recordTransition(soft: boolean): void {
76
+ previousPath = currentPath;
77
+ currentPath = typeof window === 'undefined' ? '/' : window.location.pathname;
78
+ softNav = soft;
79
+ }
80
+
81
+ /** Whether the current location was reached by a client navigation (not an initial load / refresh). */
82
+ export function isSoftNavigation(): boolean {
83
+ return softNav;
84
+ }
85
+
86
+ /** The path the app was on before the latest navigation (what the main view keeps during an intercept). */
87
+ export function previousPathname(): string {
88
+ return previousPath;
89
+ }
90
+
91
+ // Navigation-pending tracking: a navigation is "pending" from when it starts until the new route
92
+ // commits. Drives useNavigationPending() (e.g. a top loading bar).
93
+ let startedTick = 0;
94
+ let committedTick = 0;
95
+ const pendingListeners = new Set<() => void>();
96
+ function emitPending(): void {
97
+ for (const listener of pendingListeners) listener();
98
+ }
99
+ function beginNavigation(): void {
100
+ startedTick += 1;
101
+ emitPending();
102
+ }
103
+
104
+ /** Marks the in-flight navigation as committed. Called by `Router` after each commit. */
105
+ export function settleNavigation(): void {
106
+ if (committedTick !== startedTick) {
107
+ committedTick = startedTick;
108
+ emitPending();
109
+ }
110
+ }
111
+
112
+ /** Whether a navigation is in flight (started but not yet committed). */
113
+ export function isNavigationPending(): boolean {
114
+ return startedTick !== committedTick;
115
+ }
116
+
117
+ /** Monotonic id incremented on each navigation, used to key/revalidate per-navigation route data. */
118
+ export function navigationEpoch(): number {
119
+ return startedTick;
120
+ }
121
+
122
+ /** Subscribes to navigation-pending changes; returns an unsubscribe function. */
123
+ export function subscribePending(listener: () => void): () => void {
124
+ pendingListeners.add(listener);
125
+ return () => {
126
+ pendingListeners.delete(listener);
127
+ };
128
+ }
129
+
130
+ /** Options for {@link navigate}. */
131
+ export interface NavigateOptions {
132
+ /** Replace the current history entry instead of pushing a new one. Default `false`. */
133
+ readonly replace?: boolean;
134
+ /** Scroll to the top of the page after navigating. Default `true`. */
135
+ readonly scroll?: boolean;
136
+ }
137
+
138
+ /** Initializes manual scroll restoration and the initial history key. Called once by `mount`. */
139
+ export function initNavigation(): void {
140
+ enableManualScrollRestoration();
141
+ const state = window.history.state as ToilHistoryState | null;
142
+ if (state?.__toilKey) {
143
+ currentKey = state.__toilKey;
144
+ } else {
145
+ currentKey = nextKey();
146
+ window.history.replaceState({ ...state, __toilKey: currentKey }, '');
147
+ }
148
+ }
149
+
150
+ /** Navigates to `href` without a full page reload (history push/replace + subscriber re-render). */
151
+ export function navigate(href: Href, options?: NavigateOptions): void {
152
+ beginNavigation();
153
+ rememberScroll(currentKey);
154
+ let hash = '';
155
+ try {
156
+ hash = new URL(href, window.location.href).hash;
157
+ } catch {
158
+ hash = '';
159
+ }
160
+ if (options?.replace) {
161
+ window.history.replaceState({ __toilKey: currentKey }, '', href);
162
+ } else {
163
+ currentKey = nextKey();
164
+ window.history.pushState({ __toilKey: currentKey }, '', href);
165
+ }
166
+ recordTransition(true);
167
+ planScroll({ hash, toTop: options?.scroll !== false });
168
+ notify();
169
+ }
170
+
171
+ /** Goes back one entry in history (fires `popstate`, which notifies subscribers). */
172
+ export function back(): void {
173
+ window.history.back();
174
+ }
175
+
176
+ /** Goes forward one entry in history. */
177
+ export function forward(): void {
178
+ window.history.forward();
179
+ }
180
+
181
+ /**
182
+ * Re-renders the current route, bumping the navigation epoch so a revalidation of the *same* URL
183
+ * re-keys its Suspense boundary (its `loading.tsx` shows while the loader re-runs) and
184
+ * `useNavigationPending` reports the in-flight refetch, instead of silently freezing the old page.
185
+ */
186
+ export function refresh(): void {
187
+ beginNavigation();
188
+ notify();
189
+ }
190
+
191
+ /** Handles browser back/forward: restores the saved scroll for the target entry, then re-renders. */
192
+ function handlePopState(event: PopStateEvent): void {
193
+ beginNavigation();
194
+ rememberScroll(currentKey);
195
+ const state = event.state as ToilHistoryState | null;
196
+ currentKey = state?.__toilKey ?? 'initial';
197
+ recordTransition(true);
198
+ planScroll({ restoreKey: currentKey, hash: window.location.hash, toTop: false });
199
+ notify();
200
+ }
201
+
202
+ /**
203
+ * Subscribes `listener` to location changes and returns an unsubscribe function. Browser
204
+ * back/forward is wired once, on the first subscription, via a shared `popstate` handler.
205
+ */
206
+ export function subscribeLocation(listener: () => void): () => void {
207
+ if (!popstateBound) {
208
+ window.addEventListener('popstate', handlePopState);
209
+ popstateBound = true;
210
+ }
211
+ listeners.add(listener);
212
+ return () => {
213
+ listeners.delete(listener);
214
+ };
215
+ }