@useavalon/avalon 0.1.22 → 0.1.24
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/dist/mod.d.ts +50 -0
- package/dist/src/build/integration-bundler-plugin.d.ts +25 -0
- package/dist/src/build/integration-config.d.ts +44 -0
- package/dist/src/build/integration-detection-plugin.d.ts +20 -0
- package/dist/src/build/integration-resolver-plugin.d.ts +10 -0
- package/dist/src/build/island-manifest.d.ts +57 -0
- package/dist/src/build/island-types-generator.d.ts +49 -0
- package/dist/src/build/mdx-island-transform.d.ts +23 -0
- package/dist/src/build/mdx-plugin.d.ts +36 -0
- package/dist/src/build/page-island-transform.d.ts +41 -0
- package/dist/src/build/prop-extractors/index.d.ts +8 -0
- package/dist/src/build/prop-extractors/lit.d.ts +11 -0
- package/dist/src/build/prop-extractors/qwik.d.ts +9 -0
- package/dist/src/build/prop-extractors/solid.d.ts +12 -0
- package/dist/src/build/prop-extractors/svelte.d.ts +13 -0
- package/dist/src/build/prop-extractors/vue.d.ts +19 -0
- package/dist/src/build/sidecar-file-manager.d.ts +23 -0
- package/dist/src/build/sidecar-renderer.d.ts +22 -0
- package/dist/src/client/adapters/index.d.ts +15 -0
- package/dist/src/client/components.d.ts +21 -0
- package/dist/src/client/css-hmr-handler.d.ts +94 -0
- package/dist/src/client/framework-adapter.d.ts +225 -0
- package/dist/src/client/hmr-coordinator.d.ts +88 -0
- package/dist/src/components/Image.d.ts +61 -0
- package/dist/src/components/IslandErrorBoundary.d.ts +37 -0
- package/dist/src/components/IslandErrorBoundary.js +1 -1
- package/dist/src/components/LayoutDataErrorBoundary.d.ts +34 -0
- package/dist/src/components/LayoutDataErrorBoundary.js +1 -1
- package/dist/src/components/LayoutErrorBoundary.d.ts +25 -0
- package/dist/src/components/LayoutErrorBoundary.js +1 -1
- package/dist/src/components/PersistentIsland.d.ts +36 -0
- package/dist/src/components/StreamingErrorBoundary.d.ts +42 -0
- package/dist/src/components/StreamingErrorBoundary.js +1 -1
- package/dist/src/components/StreamingLayout.d.ts +83 -0
- package/dist/src/components/StreamingLayout.js +1 -1
- package/dist/src/core/components/component-analyzer.d.ts +48 -0
- package/dist/src/core/components/component-detection.d.ts +72 -0
- package/dist/src/core/components/enhanced-framework-detector.d.ts +78 -0
- package/dist/src/core/components/framework-registry.d.ts +114 -0
- package/dist/src/core/content/mdx-processor.d.ts +14 -0
- package/dist/src/core/integrations/index.d.ts +6 -0
- package/dist/src/core/integrations/loader.d.ts +35 -0
- package/dist/src/core/integrations/registry.d.ts +51 -0
- package/dist/src/core/islands/island-persistence.d.ts +74 -0
- package/dist/src/core/islands/island-state-serializer.d.ts +53 -0
- package/dist/src/core/islands/persistent-island-context.d.ts +36 -0
- package/dist/src/core/islands/use-persistent-state.d.ts +17 -0
- package/dist/src/core/layout/enhanced-layout-resolver.d.ts +73 -0
- package/dist/src/core/layout/layout-cache-manager.d.ts +66 -0
- package/dist/src/core/layout/layout-composer.d.ts +63 -0
- package/dist/src/core/layout/layout-data-loader.d.ts +146 -0
- package/dist/src/core/layout/layout-discovery.d.ts +51 -0
- package/dist/src/core/layout/layout-matcher.d.ts +77 -0
- package/dist/src/core/layout/layout-types.d.ts +94 -0
- package/dist/src/core/modules/framework-module-resolver.d.ts +85 -0
- package/dist/src/islands/component-analysis.d.ts +38 -0
- package/dist/src/islands/css-utils.d.ts +81 -0
- package/dist/src/islands/discovery/index.d.ts +16 -0
- package/dist/src/islands/discovery/registry.d.ts +141 -0
- package/dist/src/islands/discovery/resolver.d.ts +190 -0
- package/dist/src/islands/discovery/scanner.d.ts +55 -0
- package/dist/src/islands/discovery/types.d.ts +92 -0
- package/dist/src/islands/discovery/validator.d.ts +89 -0
- package/dist/src/islands/discovery/watcher.d.ts +95 -0
- package/dist/src/islands/framework-detection.d.ts +53 -0
- package/dist/src/islands/integration-loader.d.ts +139 -0
- package/dist/src/islands/island.d.ts +55 -0
- package/dist/src/islands/render-cache.d.ts +203 -0
- package/dist/src/islands/types.d.ts +63 -0
- package/dist/src/islands/universal-css-collector.d.ts +41 -0
- package/dist/src/islands/universal-head-collector.d.ts +42 -0
- package/dist/src/layout-system.d.ts +92 -592
- package/dist/src/middleware/discovery.d.ts +70 -0
- package/dist/src/middleware/executor.d.ts +52 -0
- package/dist/src/middleware/index.d.ts +43 -0
- package/dist/src/middleware/types.d.ts +89 -0
- package/dist/src/nitro/build-config.d.ts +163 -0
- package/dist/src/nitro/config.d.ts +268 -0
- package/dist/src/nitro/error-handler.d.ts +151 -0
- package/dist/src/nitro/index.d.ts +15 -0
- package/dist/src/nitro/island-manifest.d.ts +191 -0
- package/dist/src/nitro/middleware-adapter.d.ts +151 -0
- package/dist/src/nitro/renderer.d.ts +342 -0
- package/dist/src/nitro/route-discovery.d.ts +142 -0
- package/dist/src/nitro/types.d.ts +278 -0
- package/dist/src/render/collect-css.d.ts +28 -0
- package/dist/src/render/error-pages.d.ts +14 -0
- package/dist/src/render/isolated-ssr-renderer.d.ts +127 -0
- package/dist/src/render/ssr.d.ts +56 -0
- package/dist/src/schemas/api.d.ts +27 -0
- package/dist/src/schemas/core.d.ts +75 -0
- package/dist/src/schemas/index.d.ts +79 -0
- package/dist/src/schemas/index.js +1 -1
- package/dist/src/schemas/layout.d.ts +282 -0
- package/dist/src/schemas/routing/index.d.ts +181 -0
- package/dist/src/schemas/routing.d.ts +388 -0
- package/dist/src/types/as-island.d.ts +17 -0
- package/dist/src/types/layout.d.ts +177 -0
- package/dist/src/types/routing.d.ts +297 -0
- package/dist/src/types/types.d.ts +6 -0
- package/dist/src/utils/dev-logger.d.ts +84 -0
- package/dist/src/utils/fs.d.ts +41 -0
- package/dist/src/vite-plugin/auto-discover.d.ts +72 -0
- package/dist/src/vite-plugin/config.d.ts +82 -0
- package/dist/src/vite-plugin/errors.d.ts +62 -0
- package/dist/src/vite-plugin/image-optimization.d.ts +36 -0
- package/dist/src/vite-plugin/integration-activator.d.ts +48 -0
- package/dist/src/vite-plugin/island-sidecar-plugin.d.ts +17 -0
- package/dist/src/vite-plugin/module-discovery.d.ts +66 -0
- package/dist/src/vite-plugin/nitro-integration.d.ts +56 -0
- package/dist/src/vite-plugin/nitro-integration.js +1 -1
- package/dist/src/vite-plugin/plugin.d.ts +51 -0
- package/dist/src/vite-plugin/types.d.ts +281 -0
- package/dist/src/vite-plugin/validation.d.ts +93 -0
- package/package.json +33 -9
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nitro SSR Renderer Handler for Avalon
|
|
3
|
+
*
|
|
4
|
+
* This module provides the main SSR renderer for Nitro integration.
|
|
5
|
+
* It handles page rendering using Avalon's existing SSR pipeline while
|
|
6
|
+
* integrating with Nitro's h3 event handling system.
|
|
7
|
+
*
|
|
8
|
+
* The renderer acts as a catch-all handler for Nitro - it receives requests
|
|
9
|
+
* that don't match any API routes or static files, and renders the appropriate
|
|
10
|
+
* page using Avalon's SSR pipeline.
|
|
11
|
+
*
|
|
12
|
+
* Key design principle: This renderer relies on Nitro's built-in file-system
|
|
13
|
+
* routing for route matching. Custom route matching logic has been removed
|
|
14
|
+
* in favor of Nitro's native capabilities.
|
|
15
|
+
*
|
|
16
|
+
* Middleware Integration:
|
|
17
|
+
* - Global middleware runs first (handled by Nitro's middleware/ directory)
|
|
18
|
+
* - Route-scoped middleware runs after global middleware, before page rendering
|
|
19
|
+
* - If global middleware terminates, route-scoped middleware does not run
|
|
20
|
+
*
|
|
21
|
+
* Custom Error Pages:
|
|
22
|
+
* - Supports custom 404 page (src/pages/404.tsx)
|
|
23
|
+
* - Supports custom 500 page (src/pages/500.tsx)
|
|
24
|
+
* - Supports generic error page (src/pages/_error.tsx)
|
|
25
|
+
*
|
|
26
|
+
* Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 5.1, 5.3, 9.1, 9.2, 9.3, 9.4, 10.5
|
|
27
|
+
*/
|
|
28
|
+
import type { NitroRenderContext, SSRRenderOptions, SSRRenderResult, PageModule, AvalonRuntimeConfig, HttpError } from './types.ts';
|
|
29
|
+
import type { H3Event } from 'h3';
|
|
30
|
+
/**
|
|
31
|
+
* Resolved page route information
|
|
32
|
+
*/
|
|
33
|
+
export interface ResolvedPageRoute {
|
|
34
|
+
/** File path to the page module */
|
|
35
|
+
filePath: string;
|
|
36
|
+
/** Route pattern that matched */
|
|
37
|
+
pattern: string;
|
|
38
|
+
/** Extracted route parameters */
|
|
39
|
+
params: Record<string, string>;
|
|
40
|
+
/** Layout files to apply (outermost first) */
|
|
41
|
+
layouts?: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Render handler options
|
|
45
|
+
*
|
|
46
|
+
* Simplified for Nitro's catch-all pattern - route resolution is now
|
|
47
|
+
* handled by Nitro's file-system routing, so custom resolvers are optional
|
|
48
|
+
* and primarily used for development/testing scenarios.
|
|
49
|
+
*/
|
|
50
|
+
export interface RenderHandlerOptions {
|
|
51
|
+
/** Avalon runtime configuration */
|
|
52
|
+
avalonConfig: AvalonRuntimeConfig;
|
|
53
|
+
/** Whether running in development mode */
|
|
54
|
+
isDev?: boolean;
|
|
55
|
+
/** Vite dev server URL for development */
|
|
56
|
+
viteServerUrl?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Custom page resolver function (optional)
|
|
59
|
+
* In production, Nitro handles route resolution via file-system routing.
|
|
60
|
+
* This is primarily used for development with Vite's SSR module loading.
|
|
61
|
+
*/
|
|
62
|
+
resolvePageRoute?: (pathname: string, pagesDir: string) => Promise<ResolvedPageRoute | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Custom page module loader (optional)
|
|
65
|
+
* In production, modules are loaded from the build output.
|
|
66
|
+
* In development, Vite's ssrLoadModule is used.
|
|
67
|
+
*/
|
|
68
|
+
loadPageModule?: (filePath: string) => Promise<PageModule>;
|
|
69
|
+
/** Custom layout resolver */
|
|
70
|
+
resolveLayouts?: (routePath: string, config: AvalonRuntimeConfig) => Promise<string[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Enable custom error pages (404.tsx, 500.tsx, _error.tsx)
|
|
73
|
+
* When enabled, the renderer will look for custom error pages in the pages directory
|
|
74
|
+
* Requirements: 10.5
|
|
75
|
+
*/
|
|
76
|
+
enableCustomErrorPages?: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a render context from an H3 event
|
|
80
|
+
*
|
|
81
|
+
* @param event - The H3 event from Nitro
|
|
82
|
+
* @param params - Route parameters extracted from the URL
|
|
83
|
+
* @returns NitroRenderContext for use in rendering
|
|
84
|
+
*/
|
|
85
|
+
export declare function createRenderContext(event: H3Event, params?: Record<string, string>): NitroRenderContext;
|
|
86
|
+
/**
|
|
87
|
+
* Gets the request URL from an H3 event
|
|
88
|
+
*/
|
|
89
|
+
export declare function getRequestURL(event: H3Event): URL;
|
|
90
|
+
/**
|
|
91
|
+
* Converts an H3 event to a standard Request object
|
|
92
|
+
*/
|
|
93
|
+
export declare function toRequest(event: H3Event): Request;
|
|
94
|
+
/**
|
|
95
|
+
* Gets request headers from an H3 event
|
|
96
|
+
*/
|
|
97
|
+
export declare function getRequestHeaders(event: H3Event): Headers;
|
|
98
|
+
/**
|
|
99
|
+
* Sets a response header on an H3 event
|
|
100
|
+
*/
|
|
101
|
+
export declare function setResponseHeader(event: H3Event, name: string, value: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* Creates an error response
|
|
104
|
+
*/
|
|
105
|
+
export declare function createErrorResponse(error: Error | HttpError, isDev: boolean): Response;
|
|
106
|
+
/**
|
|
107
|
+
* Island hydration marker information
|
|
108
|
+
*/
|
|
109
|
+
export interface IslandMarker {
|
|
110
|
+
/** Framework identifier (react, vue, svelte, etc.) */
|
|
111
|
+
framework: string;
|
|
112
|
+
/** Source path to the island module */
|
|
113
|
+
src: string;
|
|
114
|
+
/** Serialized props for the island */
|
|
115
|
+
props?: string;
|
|
116
|
+
/** Hydration strategy (load, idle, visible, media) */
|
|
117
|
+
hydrate?: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Extracts island markers from HTML
|
|
121
|
+
* Requirements: 9.1, 9.2, 9.3
|
|
122
|
+
*
|
|
123
|
+
* @param html - The rendered HTML string
|
|
124
|
+
* @returns Array of island markers found in the HTML
|
|
125
|
+
*/
|
|
126
|
+
export declare function extractIslandMarkers(html: string): IslandMarker[];
|
|
127
|
+
/**
|
|
128
|
+
* Ensures all required hydration markers are present on an island element
|
|
129
|
+
* Requirements: 9.1, 9.2, 9.3
|
|
130
|
+
*
|
|
131
|
+
* @param element - The island element HTML string
|
|
132
|
+
* @param marker - The island marker data to ensure
|
|
133
|
+
* @returns The element with all required markers
|
|
134
|
+
*/
|
|
135
|
+
export declare function ensureHydrationMarkers(element: string, marker: Partial<IslandMarker>): string;
|
|
136
|
+
/**
|
|
137
|
+
* Injects the client hydration script into HTML
|
|
138
|
+
* Requirements: 2.6, 9.4
|
|
139
|
+
*
|
|
140
|
+
* This function:
|
|
141
|
+
* 1. Checks if there are islands that need hydration
|
|
142
|
+
* 2. Injects the client script before </body> if not already present
|
|
143
|
+
* 3. Supports both development and production script paths
|
|
144
|
+
*
|
|
145
|
+
* @param html - The rendered HTML string
|
|
146
|
+
* @param isDev - Whether running in development mode
|
|
147
|
+
* @param options - Additional injection options
|
|
148
|
+
* @returns HTML with hydration script injected
|
|
149
|
+
*/
|
|
150
|
+
export declare function injectHydrationScript(html: string, isDev: boolean, options?: {
|
|
151
|
+
/** Custom script path override */
|
|
152
|
+
scriptPath?: string;
|
|
153
|
+
/** Additional scripts to inject */
|
|
154
|
+
additionalScripts?: string[];
|
|
155
|
+
/** Whether to force injection even without islands */
|
|
156
|
+
forceInject?: boolean;
|
|
157
|
+
}): string;
|
|
158
|
+
/**
|
|
159
|
+
* Validates that hydration markers are present in the HTML
|
|
160
|
+
* Requirements: 2.3, 9.1, 9.2, 9.3
|
|
161
|
+
*
|
|
162
|
+
* @param html - The rendered HTML string
|
|
163
|
+
* @returns Object with validation results
|
|
164
|
+
*/
|
|
165
|
+
export declare function validateHydrationMarkers(html: string): {
|
|
166
|
+
hasFrameworkAttr: boolean;
|
|
167
|
+
hasSrcAttr: boolean;
|
|
168
|
+
hasPropsAttr: boolean;
|
|
169
|
+
islandCount: number;
|
|
170
|
+
islands: IslandMarker[];
|
|
171
|
+
hasClientScript: boolean;
|
|
172
|
+
isValid: boolean;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Processes HTML to ensure all hydration requirements are met
|
|
176
|
+
* Requirements: 2.3, 2.6, 9.1, 9.2, 9.3, 9.4
|
|
177
|
+
*
|
|
178
|
+
* This is a convenience function that:
|
|
179
|
+
* 1. Validates existing hydration markers
|
|
180
|
+
* 2. Injects the client script if needed
|
|
181
|
+
* 3. Returns the processed HTML
|
|
182
|
+
*
|
|
183
|
+
* @param html - The rendered HTML string
|
|
184
|
+
* @param isDev - Whether running in development mode
|
|
185
|
+
* @returns Processed HTML with all hydration requirements met
|
|
186
|
+
*/
|
|
187
|
+
export declare function processHydrationRequirements(html: string, isDev: boolean): {
|
|
188
|
+
html: string;
|
|
189
|
+
validation: ReturnType<typeof validateHydrationMarkers>;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Renders a page to HTML string (non-streaming)
|
|
193
|
+
* Requirements: 2.1, 2.2
|
|
194
|
+
*
|
|
195
|
+
* @param pageModule - The page module to render
|
|
196
|
+
* @param context - The render context
|
|
197
|
+
* @param options - Render options
|
|
198
|
+
* @returns SSR render result
|
|
199
|
+
*/
|
|
200
|
+
export declare function renderPage(pageModule: PageModule, context: NitroRenderContext, options?: SSRRenderOptions): Promise<SSRRenderResult>;
|
|
201
|
+
/**
|
|
202
|
+
* Extended streaming options with additional callbacks
|
|
203
|
+
*/
|
|
204
|
+
export interface StreamingSSROptions extends SSRRenderOptions {
|
|
205
|
+
/** Callback when shell rendering fails before streaming starts */
|
|
206
|
+
onShellError?: (error: Error) => void;
|
|
207
|
+
/** Timeout for shell ready in milliseconds */
|
|
208
|
+
shellReadyTimeout?: number;
|
|
209
|
+
/** Timeout for all content ready in milliseconds */
|
|
210
|
+
allReadyTimeout?: number;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Renders a page to a streaming response
|
|
214
|
+
* Requirements: 2.4
|
|
215
|
+
*
|
|
216
|
+
* This function implements streaming SSR with proper shell/content separation:
|
|
217
|
+
* 1. Shell (DOCTYPE, html, head, body opening) is sent first
|
|
218
|
+
* 2. onShellReady callback is invoked when shell is ready
|
|
219
|
+
* 3. Page content is streamed progressively
|
|
220
|
+
* 4. onAllReady callback is invoked when all content is complete
|
|
221
|
+
*
|
|
222
|
+
* @param pageModule - The page module to render
|
|
223
|
+
* @param context - The render context
|
|
224
|
+
* @param options - Render options including streaming callbacks
|
|
225
|
+
* @returns ReadableStream of HTML chunks
|
|
226
|
+
*/
|
|
227
|
+
export declare function renderPageStream(pageModule: PageModule, context: NitroRenderContext, options?: StreamingSSROptions): Promise<ReadableStream<Uint8Array>>;
|
|
228
|
+
/**
|
|
229
|
+
* Creates a streaming response with proper headers
|
|
230
|
+
* Requirements: 2.4
|
|
231
|
+
*
|
|
232
|
+
* @param stream - The ReadableStream to wrap
|
|
233
|
+
* @param options - Additional response options
|
|
234
|
+
* @returns Response object with streaming body
|
|
235
|
+
*/
|
|
236
|
+
export declare function createStreamingResponse(stream: ReadableStream<Uint8Array>, options?: {
|
|
237
|
+
status?: number;
|
|
238
|
+
headers?: Record<string, string>;
|
|
239
|
+
}): Response;
|
|
240
|
+
/**
|
|
241
|
+
* Creates the main Nitro renderer handler
|
|
242
|
+
*
|
|
243
|
+
* This is the catch-all handler for Nitro that renders pages not matched
|
|
244
|
+
* by API routes or static files. It integrates with Nitro's routing system:
|
|
245
|
+
*
|
|
246
|
+
* 1. Nitro's file-system routing handles API routes (api/ directory)
|
|
247
|
+
* 2. Nitro's static asset handling serves files from public/
|
|
248
|
+
* 3. This renderer catches all remaining requests for SSR page rendering
|
|
249
|
+
*
|
|
250
|
+
* Middleware execution order:
|
|
251
|
+
* 1. Global middleware (from middleware/ directory) - handled by Nitro
|
|
252
|
+
* 2. Route-scoped middleware (from _middleware.ts files) - handled here
|
|
253
|
+
* 3. Page rendering
|
|
254
|
+
*
|
|
255
|
+
* If global middleware terminates the chain, this handler is not called.
|
|
256
|
+
* If route-scoped middleware terminates, page rendering is skipped.
|
|
257
|
+
*
|
|
258
|
+
* The renderer relies on Nitro's event context for route information when
|
|
259
|
+
* available, falling back to pathname-based resolution for development.
|
|
260
|
+
*
|
|
261
|
+
* Requirements: 2.1, 2.2, 2.4, 5.1, 5.3, 10.5
|
|
262
|
+
*
|
|
263
|
+
* @param options - Render handler options
|
|
264
|
+
* @returns Handler function for Nitro
|
|
265
|
+
*/
|
|
266
|
+
export declare function createNitroRenderer(options: RenderHandlerOptions): (event: H3Event) => Promise<Response>;
|
|
267
|
+
/**
|
|
268
|
+
* Options for the Nitro catch-all renderer
|
|
269
|
+
*/
|
|
270
|
+
export interface NitroCatchAllOptions {
|
|
271
|
+
/** Avalon runtime configuration */
|
|
272
|
+
avalonConfig: AvalonRuntimeConfig;
|
|
273
|
+
/** Whether running in development mode */
|
|
274
|
+
isDev?: boolean;
|
|
275
|
+
/**
|
|
276
|
+
* Page module loader function
|
|
277
|
+
* In development, this should use Vite's ssrLoadModule
|
|
278
|
+
* In production, this imports from the build output
|
|
279
|
+
*/
|
|
280
|
+
loadPageModule: (filePath: string) => Promise<PageModule>;
|
|
281
|
+
/** Optional layout resolver */
|
|
282
|
+
resolveLayouts?: (routePath: string, config: AvalonRuntimeConfig) => Promise<string[]>;
|
|
283
|
+
/**
|
|
284
|
+
* Enable custom error pages (404.tsx, 500.tsx, _error.tsx)
|
|
285
|
+
* When enabled, the renderer will look for custom error pages in the pages directory
|
|
286
|
+
* Requirements: 10.5
|
|
287
|
+
*/
|
|
288
|
+
enableCustomErrorPages?: boolean;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Creates a Nitro catch-all renderer handler
|
|
292
|
+
*
|
|
293
|
+
* This is the recommended way to create a renderer for Nitro's catch-all pattern.
|
|
294
|
+
* It's designed to work with Nitro's file-system routing where:
|
|
295
|
+
*
|
|
296
|
+
* 1. API routes are handled by files in the api/ directory
|
|
297
|
+
* 2. Static assets are served from public/
|
|
298
|
+
* 3. This catch-all handles all remaining requests for SSR
|
|
299
|
+
*
|
|
300
|
+
* Middleware execution order:
|
|
301
|
+
* 1. Global middleware (from middleware/ directory) - handled by Nitro
|
|
302
|
+
* 2. Route-scoped middleware (from _middleware.ts files) - handled here
|
|
303
|
+
* 3. Page rendering
|
|
304
|
+
*
|
|
305
|
+
* The handler expects Nitro to provide route information via event.context:
|
|
306
|
+
* - event.context.params: Route parameters from dynamic segments
|
|
307
|
+
* - event.context.route: Optional resolved route information
|
|
308
|
+
*
|
|
309
|
+
* Usage in Nitro routes/[...slug].ts:
|
|
310
|
+
* ```ts
|
|
311
|
+
* import { createNitroCatchAllRenderer } from '@useavalon/nitro/renderer';
|
|
312
|
+
*
|
|
313
|
+
* export default createNitroCatchAllRenderer({
|
|
314
|
+
* avalonConfig: useRuntimeConfig().avalon,
|
|
315
|
+
* isDev: import.meta.dev,
|
|
316
|
+
* loadPageModule: async (filePath) => {
|
|
317
|
+
* return await import(filePath);
|
|
318
|
+
* }
|
|
319
|
+
* });
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* Requirements: 2.1, 2.2, 2.6, 5.1, 5.3, 10.5
|
|
323
|
+
*
|
|
324
|
+
* @param options - Catch-all renderer options
|
|
325
|
+
* @returns Nitro event handler function
|
|
326
|
+
*/
|
|
327
|
+
export declare function createNitroCatchAllRenderer(options: NitroCatchAllOptions): (event: H3Event) => Promise<Response>;
|
|
328
|
+
/**
|
|
329
|
+
* Re-export middleware cache clearing for hot reload support
|
|
330
|
+
*
|
|
331
|
+
* Call this function when middleware files change during development
|
|
332
|
+
* to ensure the latest version is loaded on the next request.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* // In your HMR handler
|
|
337
|
+
* if (file.endsWith('_middleware.ts')) {
|
|
338
|
+
* clearRendererMiddlewareCache();
|
|
339
|
+
* }
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export { clearMiddlewareCache as clearRendererMiddlewareCache } from '../middleware/index.ts';
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Discovery Module for Nitro
|
|
3
|
+
*
|
|
4
|
+
* This module provides minimal route discovery for Avalon's SSR pages.
|
|
5
|
+
*
|
|
6
|
+
* IMPORTANT: This module is simplified to complement Nitro's native routing:
|
|
7
|
+
* - API routes: Handled by Nitro's auto-discovery from `api/` directory
|
|
8
|
+
* - Page routes: Discovered here for SSR rendering (pages are components, not h3 handlers)
|
|
9
|
+
* - Middleware: Handled by Nitro's auto-discovery from `middleware/` directory
|
|
10
|
+
*
|
|
11
|
+
* The page discovery is needed because Avalon pages are React/Vue/Svelte components
|
|
12
|
+
* that require SSR rendering, which is different from Nitro's h3 route handlers.
|
|
13
|
+
*
|
|
14
|
+
* @module nitro/route-discovery
|
|
15
|
+
*/
|
|
16
|
+
import type { DiscoveredRoute } from "./types.ts";
|
|
17
|
+
/**
|
|
18
|
+
* Supported page file extensions
|
|
19
|
+
*/
|
|
20
|
+
export declare const PAGE_EXTENSIONS: string[];
|
|
21
|
+
/**
|
|
22
|
+
* Options for page route discovery
|
|
23
|
+
*/
|
|
24
|
+
export interface PageDiscoveryOptions {
|
|
25
|
+
/** Pages directory path (absolute or relative to project root) */
|
|
26
|
+
pagesDir: string;
|
|
27
|
+
/** Enable development mode logging */
|
|
28
|
+
developmentMode?: boolean;
|
|
29
|
+
/** Directories to exclude from scanning */
|
|
30
|
+
excludeDirectories?: string[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Result of file path to pattern conversion
|
|
34
|
+
*/
|
|
35
|
+
export interface FilePathPatternResult {
|
|
36
|
+
/** Route pattern (e.g., /users/:id) */
|
|
37
|
+
pattern: string;
|
|
38
|
+
/** Extracted parameter names */
|
|
39
|
+
params: string[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Discovers page routes from multiple directories with route prefixes
|
|
43
|
+
*
|
|
44
|
+
* This supports modular architecture where pages can be in different modules,
|
|
45
|
+
* each with their own route prefix.
|
|
46
|
+
*
|
|
47
|
+
* @param pageDirs - Array of page directories with their route prefixes
|
|
48
|
+
* @param options - Discovery options
|
|
49
|
+
* @returns Array of discovered page routes
|
|
50
|
+
*/
|
|
51
|
+
export declare function discoverPageRoutesFromMultipleDirs(pageDirs: Array<{
|
|
52
|
+
dir: string;
|
|
53
|
+
prefix: string;
|
|
54
|
+
}>, options?: Pick<PageDiscoveryOptions, "developmentMode" | "excludeDirectories">): Promise<DiscoveredRoute[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Discovers page routes from the pages directory for SSR rendering
|
|
57
|
+
*
|
|
58
|
+
* NOTE: This is specifically for page components that need SSR rendering.
|
|
59
|
+
* API routes should be placed in the `api/` directory and are auto-discovered
|
|
60
|
+
* by Nitro's native file-system routing.
|
|
61
|
+
*
|
|
62
|
+
* @param pagesDir - Path to the pages directory
|
|
63
|
+
* @param options - Discovery options
|
|
64
|
+
* @returns Array of discovered page routes
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const routes = await discoverPageRoutes('src/pages', {
|
|
69
|
+
* developmentMode: true,
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function discoverPageRoutes(pagesDir: string, options?: Pick<PageDiscoveryOptions, "developmentMode" | "excludeDirectories">): Promise<DiscoveredRoute[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Converts a file path to a route pattern
|
|
76
|
+
*
|
|
77
|
+
* Handles:
|
|
78
|
+
* - Index files (index.tsx -> /)
|
|
79
|
+
* - Dynamic segments ([param] -> :param)
|
|
80
|
+
* - Catch-all segments ([...slug] -> **)
|
|
81
|
+
* - Route groups ((group) -> removed from path)
|
|
82
|
+
*
|
|
83
|
+
* @param filePath - Relative file path from pages directory
|
|
84
|
+
* @returns Pattern and extracted parameter names
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* filePathToPattern('users/[id].tsx')
|
|
89
|
+
* // { pattern: '/users/:id', params: ['id'] }
|
|
90
|
+
*
|
|
91
|
+
* filePathToPattern('blog/[...slug].tsx')
|
|
92
|
+
* // { pattern: '/blog/**', params: ['slug'] }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function filePathToPattern(filePath: string): FilePathPatternResult;
|
|
96
|
+
/**
|
|
97
|
+
* Checks if a file is in a private folder (starts with _)
|
|
98
|
+
*
|
|
99
|
+
* @param relativePath - Relative file path
|
|
100
|
+
* @returns True if the file is private
|
|
101
|
+
*/
|
|
102
|
+
export declare function isPrivateFile(relativePath: string): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Calculates route specificity score for sorting
|
|
105
|
+
* Lower score = more specific = higher priority
|
|
106
|
+
*
|
|
107
|
+
* @param route - Discovered route
|
|
108
|
+
* @returns Specificity score
|
|
109
|
+
*/
|
|
110
|
+
export declare function calculateRouteSpecificity(route: DiscoveredRoute): number;
|
|
111
|
+
/**
|
|
112
|
+
* Sorts routes by specificity (most specific first)
|
|
113
|
+
*
|
|
114
|
+
* @param routes - Array of discovered routes
|
|
115
|
+
* @returns Sorted array of routes
|
|
116
|
+
*/
|
|
117
|
+
export declare function sortRoutesBySpecificity(routes: DiscoveredRoute[]): DiscoveredRoute[];
|
|
118
|
+
/**
|
|
119
|
+
* Validates a route pattern for correctness
|
|
120
|
+
*
|
|
121
|
+
* @param pattern - Route pattern to validate
|
|
122
|
+
* @returns Array of validation errors (empty if valid)
|
|
123
|
+
*/
|
|
124
|
+
export declare function validateRoutePattern(pattern: string): string[];
|
|
125
|
+
/**
|
|
126
|
+
* Extracts parameter names from a route pattern
|
|
127
|
+
*
|
|
128
|
+
* @param pattern - Route pattern (e.g., /users/:id/posts/:postId)
|
|
129
|
+
* @returns Array of parameter names
|
|
130
|
+
*/
|
|
131
|
+
export declare function extractParamsFromPattern(pattern: string): string[];
|
|
132
|
+
/**
|
|
133
|
+
* Checks if a route pattern matches a given URL path
|
|
134
|
+
*
|
|
135
|
+
* @param pattern - Route pattern
|
|
136
|
+
* @param path - URL path to match
|
|
137
|
+
* @returns True if the pattern matches the path
|
|
138
|
+
*/
|
|
139
|
+
export declare function matchRoutePattern(pattern: string, path: string): {
|
|
140
|
+
matches: boolean;
|
|
141
|
+
params: Record<string, string>;
|
|
142
|
+
};
|