/** * Static Site Generation (SSG) utilities. * * This module provides tools for generating static HTML files at build time, * supporting both full-site and selective page generation. * * @example Basic SSG * ```typescript * import { generateStaticSite } from '@composable-svelte/core/ssr'; * import App from './App.svelte'; * import { appReducer } from './reducer'; * * await generateStaticSite(App, { * routes: [ * { path: '/' }, * { path: '/about' } * ], * outDir: './dist', * baseURL: 'https://example.com' * }, { * reducer: appReducer, * dependencies: {} * }); * ``` * * @example Dynamic Routes * ```typescript * const posts = await loadPosts(); * * await generateStaticSite(App, { * routes: [ * { path: '/' }, * { * path: '/posts/:id', * paths: posts.map(p => `/posts/${p.id}`), * getServerProps: async (path) => { * const id = parseInt(path.split('/').pop()!); * return await loadPost(id); * } * } * ], * outDir: './dist' * }, { * reducer: appReducer * }); * ``` * * @module ssr/ssg */ import { type RenderOptions } from './render.js'; import type { Reducer } from '../types.js'; /** * Svelte component type (compatible with Svelte 5). */ type SvelteComponent = any; /** * Configuration for a single SSG route. */ export interface SSGRoute { /** * Path pattern for the route. * Examples: '/', '/about', '/posts/:id' */ path: string; /** * For dynamic routes, provide the actual paths to generate. * Can be an array of paths or a function that returns paths. * * Example for `/posts/:id`: * ```typescript * paths: ['/posts/1', '/posts/2', '/posts/3'] * // or * paths: async () => { * const posts = await loadPosts(); * return posts.map(p => `/posts/${p.id}`); * } * ``` */ paths?: string[] | (() => Promise); /** * Optional function to load data for this route. * Called for each path during generation. * * @param path - The actual path being generated (e.g., '/posts/1') * @returns Data to include in initial state */ getServerProps?: (path: string) => Promise; } /** * Configuration for static site generation. */ export interface SSGConfig { /** * Routes to pre-render. * Each route can be static or dynamic. */ routes: SSGRoute[]; /** * Output directory for generated HTML files. * Default: './dist' */ outDir?: string; /** * Base URL for the site (used for canonical links). * Example: 'https://example.com' */ baseURL?: string; /** * Whether to generate a 404 page. * Default: true */ generate404?: boolean; /** * Custom 404 page state. * If provided, a 404.html will be generated with this state. */ notFoundState?: unknown; /** * Callback called before generating each page. * Useful for logging progress. */ onPageGenerated?: (path: string, outPath: string) => void; } /** * Result of static site generation. */ export interface SSGResult { /** * Number of pages successfully generated. */ pagesGenerated: number; /** * List of generated file paths (relative to outDir). */ generatedFiles: string[]; /** * Any errors that occurred during generation. */ errors: Array<{ path: string; error: Error; }>; /** * Total time taken (in milliseconds). */ duration: number; } /** * Options for SSG generation. */ export interface SSGGenerateOptions { /** * The reducer for the application. */ reducer: Reducer; /** * Dependencies to inject into the store (typically empty for SSG). */ dependencies?: Dependencies; /** * Rendering options (title, scripts, etc.). */ renderOptions?: RenderOptions; /** * Function to compute initial state for a given path. * Called before getServerProps. * * @param path - The path being generated * @returns Initial state for this path */ getInitialState?: (path: string) => Promise | State; } /** * Generate static HTML files for all configured routes. * * This is the main entry point for static site generation. * * @template State - Application state type * @template Action - Action type * @template Dependencies - Dependencies type * * @param Component - Svelte component to render * @param config - SSG configuration * @param options - Generation options (reducer, dependencies, etc.) * @returns Result with generated files and statistics * * @example * ```typescript * const result = await generateStaticSite(App, { * routes: [ * { path: '/' }, * { path: '/about' }, * { * path: '/posts/:id', * paths: ['/posts/1', '/posts/2'], * getServerProps: async (path) => ({ * post: await loadPost(path) * }) * } * ], * outDir: './dist', * baseURL: 'https://example.com' * }, { * reducer: appReducer, * dependencies: {}, * getInitialState: () => ({ posts: [], selectedPost: null }) * }); * * console.log(`Generated ${result.pagesGenerated} pages`); * ``` */ export declare function generateStaticSite(Component: SvelteComponent, config: SSGConfig, options: SSGGenerateOptions): Promise; /** * Generate a single static HTML page. * * This is useful if you want to generate individual pages * outside of the full site generation flow. * * @template State - Application state type * @template Action - Action type * @template Dependencies - Dependencies type * * @param Component - Svelte component to render * @param path - URL path for this page (e.g., '/posts/1') * @param options - Generation options * @returns Path to generated file (relative to outDir) * * @example * ```typescript * const outPath = await generateStaticPage(App, '/posts/1', { * initialState: { post: await loadPost(1) }, * reducer: appReducer, * dependencies: {}, * outDir: './dist' * }); * ``` */ export declare function generateStaticPage(Component: SvelteComponent, path: string, options: { initialState: State; reducer: Reducer; dependencies?: Dependencies; renderOptions?: RenderOptions; outDir: string; }): Promise; export {}; //# sourceMappingURL=ssg.d.ts.map