@useavalon/avalon 0.1.12 → 0.1.13

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 (230) hide show
  1. package/mod.ts +302 -0
  2. package/package.json +9 -17
  3. package/src/build/integration-bundler-plugin.ts +116 -0
  4. package/src/build/integration-config.ts +168 -0
  5. package/src/build/integration-detection-plugin.ts +117 -0
  6. package/src/build/integration-resolver-plugin.ts +90 -0
  7. package/src/build/island-manifest.ts +269 -0
  8. package/src/build/island-types-generator.ts +476 -0
  9. package/src/build/mdx-island-transform.ts +464 -0
  10. package/src/build/mdx-plugin.ts +98 -0
  11. package/src/build/page-island-transform.ts +598 -0
  12. package/src/build/prop-extractors/index.ts +21 -0
  13. package/src/build/prop-extractors/lit.ts +140 -0
  14. package/src/build/prop-extractors/qwik.ts +16 -0
  15. package/src/build/prop-extractors/solid.ts +125 -0
  16. package/src/build/prop-extractors/svelte.ts +194 -0
  17. package/src/build/prop-extractors/vue.ts +111 -0
  18. package/src/build/sidecar-file-manager.ts +104 -0
  19. package/src/build/sidecar-renderer.ts +30 -0
  20. package/src/client/adapters/index.ts +21 -0
  21. package/src/client/components.ts +35 -0
  22. package/src/client/css-hmr-handler.ts +344 -0
  23. package/src/client/framework-adapter.ts +462 -0
  24. package/src/client/hmr-coordinator.ts +396 -0
  25. package/src/client/hmr-error-overlay.js +533 -0
  26. package/src/client/main.js +824 -0
  27. package/src/components/Image.tsx +123 -0
  28. package/src/components/IslandErrorBoundary.tsx +145 -0
  29. package/src/components/LayoutDataErrorBoundary.tsx +141 -0
  30. package/src/components/LayoutErrorBoundary.tsx +127 -0
  31. package/src/components/PersistentIsland.tsx +52 -0
  32. package/src/components/StreamingErrorBoundary.tsx +233 -0
  33. package/src/components/StreamingLayout.tsx +538 -0
  34. package/src/core/components/component-analyzer.ts +192 -0
  35. package/src/core/components/component-detection.ts +508 -0
  36. package/src/core/components/enhanced-framework-detector.ts +500 -0
  37. package/src/core/components/framework-registry.ts +563 -0
  38. package/src/core/content/mdx-processor.ts +46 -0
  39. package/src/core/integrations/index.ts +19 -0
  40. package/src/core/integrations/loader.ts +125 -0
  41. package/src/core/integrations/registry.ts +175 -0
  42. package/src/core/islands/island-persistence.ts +325 -0
  43. package/src/core/islands/island-state-serializer.ts +258 -0
  44. package/src/core/islands/persistent-island-context.tsx +80 -0
  45. package/src/core/islands/use-persistent-state.ts +68 -0
  46. package/src/core/layout/enhanced-layout-resolver.ts +322 -0
  47. package/src/core/layout/layout-cache-manager.ts +485 -0
  48. package/src/core/layout/layout-composer.ts +357 -0
  49. package/src/core/layout/layout-data-loader.ts +516 -0
  50. package/src/core/layout/layout-discovery.ts +243 -0
  51. package/src/core/layout/layout-matcher.ts +299 -0
  52. package/src/core/layout/layout-types.ts +110 -0
  53. package/src/core/modules/framework-module-resolver.ts +273 -0
  54. package/src/islands/component-analysis.ts +213 -0
  55. package/src/islands/css-utils.ts +565 -0
  56. package/src/islands/discovery/index.ts +80 -0
  57. package/src/islands/discovery/registry.ts +340 -0
  58. package/src/islands/discovery/resolver.ts +477 -0
  59. package/src/islands/discovery/scanner.ts +386 -0
  60. package/src/islands/discovery/types.ts +117 -0
  61. package/src/islands/discovery/validator.ts +544 -0
  62. package/src/islands/discovery/watcher.ts +368 -0
  63. package/src/islands/framework-detection.ts +428 -0
  64. package/src/islands/integration-loader.ts +490 -0
  65. package/src/islands/island.tsx +565 -0
  66. package/src/islands/render-cache.ts +550 -0
  67. package/src/islands/types.ts +80 -0
  68. package/src/islands/universal-css-collector.ts +157 -0
  69. package/src/islands/universal-head-collector.ts +137 -0
  70. package/src/layout-system.ts +218 -0
  71. package/src/middleware/discovery.ts +268 -0
  72. package/src/middleware/executor.ts +315 -0
  73. package/src/middleware/index.ts +76 -0
  74. package/src/middleware/types.ts +99 -0
  75. package/src/nitro/build-config.ts +576 -0
  76. package/src/nitro/config.ts +483 -0
  77. package/src/nitro/error-handler.ts +636 -0
  78. package/src/nitro/index.ts +173 -0
  79. package/src/nitro/island-manifest.ts +584 -0
  80. package/src/nitro/middleware-adapter.ts +260 -0
  81. package/src/nitro/renderer.ts +1471 -0
  82. package/src/nitro/route-discovery.ts +439 -0
  83. package/src/nitro/types.ts +321 -0
  84. package/src/render/collect-css.ts +198 -0
  85. package/src/render/error-pages.ts +79 -0
  86. package/src/render/isolated-ssr-renderer.ts +654 -0
  87. package/src/render/ssr.ts +1030 -0
  88. package/src/schemas/api.ts +30 -0
  89. package/src/schemas/core.ts +64 -0
  90. package/src/schemas/index.ts +212 -0
  91. package/src/schemas/layout.ts +279 -0
  92. package/src/schemas/routing/index.ts +38 -0
  93. package/src/schemas/routing.ts +376 -0
  94. package/src/types/as-island.ts +20 -0
  95. package/src/types/layout.ts +285 -0
  96. package/src/types/routing.ts +555 -0
  97. package/src/types/types.ts +5 -0
  98. package/src/utils/dev-logger.ts +299 -0
  99. package/src/utils/fs.ts +151 -0
  100. package/src/vite-plugin/auto-discover.ts +551 -0
  101. package/src/vite-plugin/config.ts +266 -0
  102. package/src/vite-plugin/errors.ts +127 -0
  103. package/src/vite-plugin/image-optimization.ts +156 -0
  104. package/src/vite-plugin/integration-activator.ts +126 -0
  105. package/src/vite-plugin/island-sidecar-plugin.ts +176 -0
  106. package/src/vite-plugin/module-discovery.ts +189 -0
  107. package/src/vite-plugin/nitro-integration.ts +1354 -0
  108. package/src/vite-plugin/plugin.ts +403 -0
  109. package/src/vite-plugin/types.ts +327 -0
  110. package/src/vite-plugin/validation.ts +228 -0
  111. package/dist/mod.js +0 -1
  112. package/dist/src/build/integration-bundler-plugin.js +0 -1
  113. package/dist/src/build/integration-config.js +0 -1
  114. package/dist/src/build/integration-detection-plugin.js +0 -1
  115. package/dist/src/build/integration-resolver-plugin.js +0 -1
  116. package/dist/src/build/island-manifest.js +0 -1
  117. package/dist/src/build/island-types-generator.js +0 -5
  118. package/dist/src/build/mdx-island-transform.js +0 -2
  119. package/dist/src/build/mdx-plugin.js +0 -1
  120. package/dist/src/build/page-island-transform.js +0 -3
  121. package/dist/src/build/prop-extractors/index.js +0 -1
  122. package/dist/src/build/prop-extractors/lit.js +0 -1
  123. package/dist/src/build/prop-extractors/qwik.js +0 -1
  124. package/dist/src/build/prop-extractors/solid.js +0 -1
  125. package/dist/src/build/prop-extractors/svelte.js +0 -1
  126. package/dist/src/build/prop-extractors/vue.js +0 -1
  127. package/dist/src/build/sidecar-file-manager.js +0 -1
  128. package/dist/src/build/sidecar-renderer.js +0 -6
  129. package/dist/src/client/adapters/index.js +0 -1
  130. package/dist/src/client/components.js +0 -1
  131. package/dist/src/client/css-hmr-handler.js +0 -1
  132. package/dist/src/client/framework-adapter.js +0 -13
  133. package/dist/src/client/hmr-coordinator.js +0 -1
  134. package/dist/src/client/hmr-error-overlay.js +0 -214
  135. package/dist/src/client/main.js +0 -39
  136. package/dist/src/components/Image.js +0 -1
  137. package/dist/src/components/IslandErrorBoundary.js +0 -1
  138. package/dist/src/components/LayoutDataErrorBoundary.js +0 -1
  139. package/dist/src/components/LayoutErrorBoundary.js +0 -1
  140. package/dist/src/components/PersistentIsland.js +0 -1
  141. package/dist/src/components/StreamingErrorBoundary.js +0 -1
  142. package/dist/src/components/StreamingLayout.js +0 -29
  143. package/dist/src/core/components/component-analyzer.js +0 -1
  144. package/dist/src/core/components/component-detection.js +0 -5
  145. package/dist/src/core/components/enhanced-framework-detector.js +0 -1
  146. package/dist/src/core/components/framework-registry.js +0 -1
  147. package/dist/src/core/content/mdx-processor.js +0 -1
  148. package/dist/src/core/integrations/index.js +0 -1
  149. package/dist/src/core/integrations/loader.js +0 -1
  150. package/dist/src/core/integrations/registry.js +0 -1
  151. package/dist/src/core/islands/island-persistence.js +0 -1
  152. package/dist/src/core/islands/island-state-serializer.js +0 -1
  153. package/dist/src/core/islands/persistent-island-context.js +0 -1
  154. package/dist/src/core/islands/use-persistent-state.js +0 -1
  155. package/dist/src/core/layout/enhanced-layout-resolver.js +0 -1
  156. package/dist/src/core/layout/layout-cache-manager.js +0 -1
  157. package/dist/src/core/layout/layout-composer.js +0 -1
  158. package/dist/src/core/layout/layout-data-loader.js +0 -1
  159. package/dist/src/core/layout/layout-discovery.js +0 -1
  160. package/dist/src/core/layout/layout-matcher.js +0 -1
  161. package/dist/src/core/layout/layout-types.js +0 -1
  162. package/dist/src/core/modules/framework-module-resolver.js +0 -1
  163. package/dist/src/islands/component-analysis.js +0 -1
  164. package/dist/src/islands/css-utils.js +0 -17
  165. package/dist/src/islands/discovery/index.js +0 -1
  166. package/dist/src/islands/discovery/registry.js +0 -1
  167. package/dist/src/islands/discovery/resolver.js +0 -2
  168. package/dist/src/islands/discovery/scanner.js +0 -1
  169. package/dist/src/islands/discovery/types.js +0 -1
  170. package/dist/src/islands/discovery/validator.js +0 -18
  171. package/dist/src/islands/discovery/watcher.js +0 -1
  172. package/dist/src/islands/framework-detection.js +0 -1
  173. package/dist/src/islands/integration-loader.js +0 -1
  174. package/dist/src/islands/island.js +0 -1
  175. package/dist/src/islands/render-cache.js +0 -1
  176. package/dist/src/islands/types.js +0 -1
  177. package/dist/src/islands/universal-css-collector.js +0 -5
  178. package/dist/src/islands/universal-head-collector.js +0 -2
  179. package/dist/src/layout-system.js +0 -1
  180. package/dist/src/middleware/discovery.js +0 -1
  181. package/dist/src/middleware/executor.js +0 -1
  182. package/dist/src/middleware/index.js +0 -1
  183. package/dist/src/middleware/types.js +0 -1
  184. package/dist/src/nitro/build-config.js +0 -1
  185. package/dist/src/nitro/config.js +0 -1
  186. package/dist/src/nitro/error-handler.js +0 -198
  187. package/dist/src/nitro/index.js +0 -1
  188. package/dist/src/nitro/island-manifest.js +0 -2
  189. package/dist/src/nitro/middleware-adapter.js +0 -1
  190. package/dist/src/nitro/renderer.js +0 -183
  191. package/dist/src/nitro/route-discovery.js +0 -1
  192. package/dist/src/nitro/types.js +0 -1
  193. package/dist/src/render/collect-css.js +0 -3
  194. package/dist/src/render/error-pages.js +0 -48
  195. package/dist/src/render/isolated-ssr-renderer.js +0 -1
  196. package/dist/src/render/ssr.js +0 -90
  197. package/dist/src/schemas/api.js +0 -1
  198. package/dist/src/schemas/core.js +0 -1
  199. package/dist/src/schemas/index.js +0 -1
  200. package/dist/src/schemas/layout.js +0 -1
  201. package/dist/src/schemas/routing/index.js +0 -1
  202. package/dist/src/schemas/routing.js +0 -1
  203. package/dist/src/types/as-island.js +0 -1
  204. package/dist/src/types/layout.js +0 -1
  205. package/dist/src/types/routing.js +0 -1
  206. package/dist/src/types/types.js +0 -1
  207. package/dist/src/utils/dev-logger.js +0 -12
  208. package/dist/src/utils/fs.js +0 -1
  209. package/dist/src/vite-plugin/auto-discover.js +0 -1
  210. package/dist/src/vite-plugin/config.js +0 -1
  211. package/dist/src/vite-plugin/errors.js +0 -1
  212. package/dist/src/vite-plugin/image-optimization.js +0 -45
  213. package/dist/src/vite-plugin/integration-activator.js +0 -1
  214. package/dist/src/vite-plugin/island-sidecar-plugin.js +0 -1
  215. package/dist/src/vite-plugin/module-discovery.js +0 -1
  216. package/dist/src/vite-plugin/nitro-integration.js +0 -42
  217. package/dist/src/vite-plugin/plugin.js +0 -1
  218. package/dist/src/vite-plugin/types.js +0 -1
  219. package/dist/src/vite-plugin/validation.js +0 -2
  220. /package/{dist/src → src}/client/types/framework-runtime.d.ts +0 -0
  221. /package/{dist/src → src}/client/types/vite-hmr.d.ts +0 -0
  222. /package/{dist/src → src}/client/types/vite-virtual-modules.d.ts +0 -0
  223. /package/{dist/src → src}/layout-system.d.ts +0 -0
  224. /package/{dist/src → src}/types/image.d.ts +0 -0
  225. /package/{dist/src → src}/types/index.d.ts +0 -0
  226. /package/{dist/src → src}/types/island-jsx.d.ts +0 -0
  227. /package/{dist/src → src}/types/island-prop.d.ts +0 -0
  228. /package/{dist/src → src}/types/mdx.d.ts +0 -0
  229. /package/{dist/src → src}/types/urlpattern.d.ts +0 -0
  230. /package/{dist/src → src}/types/vite-env.d.ts +0 -0
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Image Component for Avalon
3
+ *
4
+ * A responsive image component that works with vite-imagetools to provide
5
+ * optimized images with automatic srcset generation.
6
+ *
7
+ * Usage:
8
+ * ```tsx
9
+ * import { Image } from '@useavalon/avalon/client';
10
+ * import heroSrc from './hero.jpg?w=400;800;1200&format=webp&as=srcset';
11
+ *
12
+ * <Image
13
+ * src={heroSrc}
14
+ * alt="Hero image"
15
+ * sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1200px"
16
+ * />
17
+ * ```
18
+ *
19
+ * Or with the simpler single-image approach:
20
+ * ```tsx
21
+ * import heroSrc from './hero.jpg?w=800&format=webp';
22
+ *
23
+ * <Image src={heroSrc} alt="Hero image" width={800} height={600} />
24
+ * ```
25
+ */
26
+
27
+ import type { JSX } from "preact";
28
+
29
+ export interface ImageProps {
30
+ /**
31
+ * Image source - can be:
32
+ * - A string URL (single image)
33
+ * - A srcset string from ?as=srcset (contains " Xw" width descriptors)
34
+ * - An object with src/srcset/width/height from vite-imagetools
35
+ */
36
+ src: string | { src: string; srcset?: string; width?: number; height?: number };
37
+
38
+ /** Alt text for accessibility (required) */
39
+ alt: string;
40
+
41
+ /** Sizes attribute for responsive images (required when using srcset) */
42
+ sizes?: string;
43
+
44
+ /** Loading strategy */
45
+ loading?: "lazy" | "eager";
46
+
47
+ /** Decoding hint */
48
+ decoding?: "async" | "sync" | "auto";
49
+
50
+ /** Optional width (auto-detected from srcset if available) */
51
+ width?: number | string;
52
+
53
+ /** Optional height (auto-detected from srcset if available) */
54
+ height?: number | string;
55
+
56
+ /** CSS class name */
57
+ className?: string;
58
+
59
+ /** Inline styles */
60
+ style?: string | Record<string, string | number>;
61
+ }
62
+
63
+ /**
64
+ * Check if a string looks like a srcset (contains width descriptors like "400w")
65
+ */
66
+ function isSrcsetString(value: string): boolean {
67
+ return /\s\d+w/.test(value);
68
+ }
69
+
70
+ /**
71
+ * Responsive image component with built-in optimization support
72
+ */
73
+ export function Image({
74
+ src,
75
+ alt,
76
+ sizes,
77
+ loading = "lazy",
78
+ decoding = "async",
79
+ width,
80
+ height,
81
+ className,
82
+ style,
83
+ }: Readonly<ImageProps>): JSX.Element {
84
+ let imgSrc: string | undefined;
85
+ let srcSet: string | undefined;
86
+ let autoWidth: number | undefined;
87
+ let autoHeight: number | undefined;
88
+
89
+ if (typeof src === "object" && src !== null) {
90
+ // Object from vite-imagetools (e.g., ?as=metadata or custom output)
91
+ imgSrc = src.src;
92
+ srcSet = src.srcset;
93
+ autoWidth = src.width;
94
+ autoHeight = src.height;
95
+ } else if (typeof src === "string") {
96
+ if (isSrcsetString(src)) {
97
+ // srcset string from ?as=srcset - use first URL as fallback src
98
+ srcSet = src;
99
+ const firstUrl = src.split(",")[0]?.trim().split(" ")[0];
100
+ imgSrc = firstUrl;
101
+ } else {
102
+ // Regular URL string
103
+ imgSrc = src;
104
+ }
105
+ }
106
+
107
+ return (
108
+ <img
109
+ src={imgSrc}
110
+ srcSet={srcSet}
111
+ sizes={sizes}
112
+ alt={alt}
113
+ loading={loading}
114
+ decoding={decoding}
115
+ width={width ?? autoWidth}
116
+ height={height ?? autoHeight}
117
+ className={className}
118
+ style={style}
119
+ />
120
+ );
121
+ }
122
+
123
+ export default Image;
@@ -0,0 +1,145 @@
1
+ import { Component, ComponentChildren, ComponentType } from 'preact';
2
+ import type { LayoutErrorInfo } from '../types/layout.ts';
3
+
4
+ export interface IslandErrorBoundaryProps {
5
+ children: ComponentChildren;
6
+ islandId: string;
7
+ onError?: (error: Error, errorInfo: LayoutErrorInfo) => void;
8
+ fallback?: (error: Error, islandId: string) => ComponentChildren;
9
+ isolateError?: boolean;
10
+ }
11
+
12
+ export interface IslandErrorBoundaryState {
13
+ hasError: boolean;
14
+ error: Error | null;
15
+ errorInfo: LayoutErrorInfo | null;
16
+ }
17
+
18
+ /**
19
+ * Specialized error boundary for island components
20
+ * Provides error isolation to prevent island errors from affecting the main layout
21
+ */
22
+ export class IslandErrorBoundary extends Component<IslandErrorBoundaryProps, IslandErrorBoundaryState> {
23
+ constructor(props: IslandErrorBoundaryProps) {
24
+ super(props);
25
+ this.state = { hasError: false, error: null, errorInfo: null };
26
+ }
27
+
28
+ static override getDerivedStateFromError(error: Error): Partial<IslandErrorBoundaryState> {
29
+ return { hasError: true, error };
30
+ }
31
+
32
+ override componentDidCatch(error: Error, errorInfo: { componentStack?: string }): void {
33
+ const layoutErrorInfo: LayoutErrorInfo = {
34
+ layoutPath: `island:${this.props.islandId}`,
35
+ errorType: 'island',
36
+ timestamp: Date.now(),
37
+ componentStack: errorInfo.componentStack,
38
+ errorBoundary: 'IslandErrorBoundary',
39
+ };
40
+
41
+ this.setState({ errorInfo: layoutErrorInfo });
42
+
43
+ if (this.props.onError) {
44
+ this.props.onError(error, layoutErrorInfo);
45
+ }
46
+
47
+ const isDevelopment = typeof Deno !== 'undefined' && Deno.env.get('NODE_ENV') === 'development';
48
+ if (isDevelopment) {
49
+ console.error(`Island Error [${this.props.islandId}]:`, error);
50
+ }
51
+
52
+ if (!this.props.isolateError) {
53
+ throw error;
54
+ }
55
+ }
56
+
57
+ private handleRemoveIsland = (): void => {
58
+ const islandElement = document.querySelector(`[data-island-id="${this.props.islandId}"]`);
59
+ if (islandElement) {
60
+ islandElement.remove();
61
+ }
62
+ };
63
+
64
+ private handleReloadIsland = (): void => {
65
+ this.setState({ hasError: false, error: null, errorInfo: null });
66
+ };
67
+
68
+ private renderFallback(): ComponentChildren {
69
+ const { error } = this.state;
70
+ const { fallback, islandId } = this.props;
71
+
72
+ if (fallback && error) {
73
+ return fallback(error, islandId);
74
+ }
75
+
76
+ const isDevelopment = typeof Deno !== 'undefined' && Deno.env.get('NODE_ENV') === 'development';
77
+
78
+ return (
79
+ <div class="island-error-boundary" data-island-error={islandId}>
80
+ <div class="island-error-container">
81
+ <div class="island-error-header">
82
+ <span class="island-error-icon">⚠️</span>
83
+ <span class="island-error-title">Island Error</span>
84
+ </div>
85
+
86
+ <p class="island-error-message">
87
+ An error occurred in island "{islandId}". The rest of the page should work normally.
88
+ </p>
89
+
90
+ <div class="island-error-actions">
91
+ <button onClick={this.handleReloadIsland} class="island-reload-button">
92
+ Reload Island
93
+ </button>
94
+ <button onClick={this.handleRemoveIsland} class="island-remove-button">
95
+ Remove Island
96
+ </button>
97
+ </div>
98
+
99
+ {isDevelopment && error && (
100
+ <details class="island-error-details">
101
+ <summary>Error Details (Development)</summary>
102
+ <div class="island-error-info">
103
+ <p><strong>Island ID:</strong> {islandId}</p>
104
+ <p><strong>Error:</strong> {error.message}</p>
105
+ </div>
106
+ <pre class="island-error-stack">{error.stack}</pre>
107
+ </details>
108
+ )}
109
+ </div>
110
+ </div>
111
+ );
112
+ }
113
+
114
+ render() {
115
+ if (this.state.hasError) {
116
+ return this.renderFallback();
117
+ }
118
+ return this.props.children;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Higher-order component to wrap islands with error boundaries
124
+ */
125
+ export function withIslandErrorBoundary<P extends object>(
126
+ WrappedComponent: ComponentType<P>,
127
+ islandId: string,
128
+ options?: {
129
+ fallback?: (error: Error, islandId: string) => ComponentChildren;
130
+ isolateError?: boolean;
131
+ onError?: (error: Error, errorInfo: LayoutErrorInfo) => void;
132
+ }
133
+ ) {
134
+ return function IslandWithErrorBoundary(props: P) {
135
+ return (
136
+ <IslandErrorBoundary
137
+ islandId={islandId}
138
+ fallback={options?.fallback}
139
+ isolateError={options?.isolateError ?? true}
140
+ onError={options?.onError}>
141
+ <WrappedComponent {...props} />
142
+ </IslandErrorBoundary>
143
+ );
144
+ };
145
+ }
@@ -0,0 +1,141 @@
1
+ import { Component, ComponentChildren } from 'preact';
2
+ import type { LayoutErrorInfo, LayoutContext, LayoutData } from '../types/layout.ts';
3
+
4
+ export interface LayoutDataErrorBoundaryProps {
5
+ children: ComponentChildren;
6
+ layoutPath: string;
7
+ context: LayoutContext;
8
+ onError?: (error: Error, errorInfo: LayoutErrorInfo) => void;
9
+ fallbackData?: LayoutData;
10
+ retryLoader?: () => Promise<LayoutData>;
11
+ }
12
+
13
+ export interface LayoutDataErrorBoundaryState {
14
+ hasError: boolean;
15
+ error: Error | null;
16
+ errorInfo: LayoutErrorInfo | null;
17
+ retryCount: number;
18
+ isRetrying: boolean;
19
+ fallbackData: LayoutData | null;
20
+ }
21
+
22
+ /**
23
+ * Specialized error boundary for layout data loading errors
24
+ * Provides specific handling for data loader failures with retry and fallback mechanisms
25
+ */
26
+ export class LayoutDataErrorBoundary extends Component<LayoutDataErrorBoundaryProps, LayoutDataErrorBoundaryState> {
27
+ private maxRetries = 3;
28
+
29
+ constructor(props: LayoutDataErrorBoundaryProps) {
30
+ super(props);
31
+ this.state = {
32
+ hasError: false,
33
+ error: null,
34
+ errorInfo: null,
35
+ retryCount: 0,
36
+ isRetrying: false,
37
+ fallbackData: props.fallbackData || null,
38
+ };
39
+ }
40
+
41
+ static override getDerivedStateFromError(error: Error): Partial<LayoutDataErrorBoundaryState> {
42
+ return { hasError: true, error };
43
+ }
44
+
45
+ override componentDidCatch(error: Error, errorInfo: { componentStack?: string }): void {
46
+ const layoutErrorInfo: LayoutErrorInfo = {
47
+ layoutPath: this.props.layoutPath,
48
+ errorType: 'loader',
49
+ timestamp: Date.now(),
50
+ componentStack: errorInfo.componentStack,
51
+ errorBoundary: 'LayoutDataErrorBoundary',
52
+ };
53
+
54
+ this.setState({ errorInfo: layoutErrorInfo });
55
+
56
+ if (this.props.onError) {
57
+ this.props.onError(error, layoutErrorInfo);
58
+ }
59
+ }
60
+
61
+ private handleRetry = async (): Promise<void> => {
62
+ if (this.state.retryCount >= this.maxRetries || !this.props.retryLoader) {
63
+ return;
64
+ }
65
+
66
+ this.setState({ isRetrying: true });
67
+
68
+ try {
69
+ const data = await this.props.retryLoader();
70
+ this.setState({
71
+ hasError: false,
72
+ error: null,
73
+ errorInfo: null,
74
+ retryCount: this.state.retryCount + 1,
75
+ isRetrying: false,
76
+ fallbackData: data,
77
+ });
78
+ } catch (retryError) {
79
+ this.setState({
80
+ retryCount: this.state.retryCount + 1,
81
+ isRetrying: false,
82
+ error: retryError instanceof Error ? retryError : new Error(String(retryError)),
83
+ });
84
+ }
85
+ };
86
+
87
+ private handleUseFallback = (): void => {
88
+ if (this.state.fallbackData) {
89
+ this.setState({ hasError: false, error: null, errorInfo: null });
90
+ }
91
+ };
92
+
93
+ private renderErrorUI(): ComponentChildren {
94
+ const { error, retryCount, isRetrying, fallbackData } = this.state;
95
+ const canRetry = retryCount < this.maxRetries && this.props.retryLoader;
96
+ const hasFallback = fallbackData !== null;
97
+ const isDevelopment = typeof Deno !== 'undefined' && Deno.env.get('NODE_ENV') === 'development';
98
+
99
+ return (
100
+ <div class="layout-data-error-boundary">
101
+ <div class="error-container">
102
+ <h3>Data Loading Error</h3>
103
+ <p>Failed to load data for layout: {this.props.layoutPath}</p>
104
+
105
+ <div class="error-actions">
106
+ {canRetry && (
107
+ <button onClick={this.handleRetry} disabled={isRetrying} class="retry-button">
108
+ {isRetrying ? 'Retrying...' : `Retry (${this.maxRetries - retryCount} left)`}
109
+ </button>
110
+ )}
111
+
112
+ {hasFallback && (
113
+ <button onClick={this.handleUseFallback} class="fallback-button">
114
+ Use Cached Data
115
+ </button>
116
+ )}
117
+ </div>
118
+
119
+ {isDevelopment && error && (
120
+ <details class="error-details">
121
+ <summary>Error Details (Development)</summary>
122
+ <div class="error-info">
123
+ <p><strong>Error:</strong> {error.message}</p>
124
+ <p><strong>Layout:</strong> {this.props.layoutPath}</p>
125
+ <p><strong>Retry Count:</strong> {retryCount}</p>
126
+ </div>
127
+ <pre class="error-stack">{error.stack}</pre>
128
+ </details>
129
+ )}
130
+ </div>
131
+ </div>
132
+ );
133
+ }
134
+
135
+ render() {
136
+ if (this.state.hasError) {
137
+ return this.renderErrorUI();
138
+ }
139
+ return this.props.children;
140
+ }
141
+ }
@@ -0,0 +1,127 @@
1
+ import { Component, ComponentChildren } from 'preact';
2
+ import type { LayoutErrorInfo, ErrorRecoveryStrategy } from '../types/layout.ts';
3
+
4
+ export interface LayoutErrorBoundaryProps {
5
+ children: ComponentChildren;
6
+ fallback?: (error: Error, retry: () => void) => ComponentChildren;
7
+ onError?: (error: Error, errorInfo: LayoutErrorInfo) => void;
8
+ recoveryStrategy?: ErrorRecoveryStrategy;
9
+ layoutPath?: string;
10
+ errorType?: 'component' | 'loader' | 'rendering' | 'island';
11
+ }
12
+
13
+ export interface LayoutErrorBoundaryState {
14
+ hasError: boolean;
15
+ error: Error | null;
16
+ errorInfo: LayoutErrorInfo | null;
17
+ retryCount: number;
18
+ }
19
+
20
+ export class LayoutErrorBoundary extends Component<LayoutErrorBoundaryProps, LayoutErrorBoundaryState> {
21
+ private maxRetries = 3;
22
+
23
+ constructor(props: LayoutErrorBoundaryProps) {
24
+ super(props);
25
+ this.state = {
26
+ hasError: false,
27
+ error: null,
28
+ errorInfo: null,
29
+ retryCount: 0,
30
+ };
31
+ }
32
+
33
+ static override getDerivedStateFromError(error: Error): Partial<LayoutErrorBoundaryState> {
34
+ return {
35
+ hasError: true,
36
+ error,
37
+ };
38
+ }
39
+
40
+ override componentDidCatch(error: Error, errorInfo: any): void {
41
+ const layoutErrorInfo: LayoutErrorInfo = {
42
+ layoutPath: this.props.layoutPath || 'unknown',
43
+ errorType: this.props.errorType || 'component',
44
+ timestamp: Date.now(),
45
+ componentStack: errorInfo.componentStack,
46
+ errorBoundary: this.constructor.name,
47
+ };
48
+
49
+ this.setState({
50
+ errorInfo: layoutErrorInfo,
51
+ });
52
+
53
+ // Call onError callback if provided
54
+ if (this.props.onError) {
55
+ this.props.onError(error, layoutErrorInfo);
56
+ }
57
+
58
+ // Log error in development mode
59
+ if (typeof Deno !== 'undefined' && Deno.env.get('NODE_ENV') === 'development') {
60
+ console.error('Layout Error Boundary caught an error:', error);
61
+ console.error('Error Info:', layoutErrorInfo);
62
+ console.error('Component Stack:', errorInfo.componentStack);
63
+ }
64
+ }
65
+
66
+ private handleRetry = (): void => {
67
+ if (this.state.retryCount < this.maxRetries) {
68
+ this.setState({
69
+ hasError: false,
70
+ error: null,
71
+ errorInfo: null,
72
+ retryCount: this.state.retryCount + 1,
73
+ });
74
+ }
75
+ };
76
+
77
+ private renderFallback(): ComponentChildren {
78
+ const { error } = this.state;
79
+ const { fallback } = this.props;
80
+
81
+ if (fallback && error) {
82
+ return fallback(error, this.handleRetry);
83
+ }
84
+
85
+ // Default fallback UI
86
+ return (
87
+ <div class="layout-error-boundary">
88
+ <div class="error-container">
89
+ <h2>Something went wrong</h2>
90
+ <p>An error occurred while rendering this layout.</p>
91
+ {this.state.retryCount < this.maxRetries && (
92
+ <button onClick={this.handleRetry} class="retry-button">
93
+ Try Again ({this.maxRetries - this.state.retryCount} attempts left)
94
+ </button>
95
+ )}
96
+ {typeof Deno !== 'undefined' && Deno.env.get('NODE_ENV') === 'development' && (
97
+ <details class="error-details">
98
+ <summary>Error Details (Development)</summary>
99
+ <pre>{error?.stack}</pre>
100
+ {this.state.errorInfo && (
101
+ <div>
102
+ <p>
103
+ <strong>Layout Path:</strong> {this.state.errorInfo.layoutPath}
104
+ </p>
105
+ <p>
106
+ <strong>Error Type:</strong> {this.state.errorInfo.errorType}
107
+ </p>
108
+ <p>
109
+ <strong>Timestamp:</strong> {new Date(this.state.errorInfo.timestamp).toISOString()}
110
+ </p>
111
+ </div>
112
+ )}
113
+ </details>
114
+ )}
115
+ </div>
116
+ </div>
117
+ );
118
+ }
119
+
120
+ render() {
121
+ if (this.state.hasError) {
122
+ return this.renderFallback();
123
+ }
124
+
125
+ return this.props.children;
126
+ }
127
+ }
@@ -0,0 +1,52 @@
1
+ /** @jsxImportSource preact */
2
+ import type { ComponentChildren } from 'preact';
3
+ import { PersistentIslandProvider } from '../core/islands/persistent-island-context.tsx';
4
+ import { defaultIslandPersistence, type IslandPersistence } from '../core/islands/island-persistence.ts';
5
+
6
+ interface PersistentIslandProps {
7
+ /** Unique ID used as the storage key for this island's state */
8
+ persistentId: string;
9
+ /** The island component(s) to wrap with persistence context */
10
+ children: ComponentChildren;
11
+ /** Custom persistence instance (defaults to sessionStorage-backed) */
12
+ persistence?: IslandPersistence;
13
+ }
14
+
15
+ /**
16
+ * PersistentIsland — provides automatic state persistence context to child islands.
17
+ *
18
+ * Wrap any island with this component and use `usePersistentIslandContext()` inside
19
+ * the island to get `saveState`, `loadState`, and `clearState` functions.
20
+ *
21
+ * Usage in a page:
22
+ * ```tsx
23
+ * <PersistentIsland persistentId="my-counter" island={{ condition: 'on:client' }}>
24
+ * <MyCounter />
25
+ * </PersistentIsland>
26
+ * ```
27
+ *
28
+ * Usage inside the island:
29
+ * ```tsx
30
+ * import { usePersistentIslandContext } from '@useavalon/avalon';
31
+ *
32
+ * function MyCounter() {
33
+ * const { saveState, loadState, clearState } = usePersistentIslandContext();
34
+ * // ...
35
+ * }
36
+ * ```
37
+ */
38
+ export function PersistentIsland({
39
+ persistentId,
40
+ children,
41
+ persistence = defaultIslandPersistence,
42
+ }: Readonly<PersistentIslandProps>) {
43
+ return (
44
+ <PersistentIslandProvider persistentId={persistentId} persistence={persistence}>
45
+ <div data-persistent-id={persistentId}>
46
+ {children}
47
+ </div>
48
+ </PersistentIslandProvider>
49
+ );
50
+ }
51
+
52
+ export default PersistentIsland;