Add sitemap generation utility

This commit is contained in:
2026-10-14 17:41:50 +01:00
parent d9de858439
commit 632881e174
9 changed files with 289 additions and 1 deletions
+68
View File
@@ -93,6 +93,10 @@ export type BunextConfig = {
* Public environment variables to be passed to the client
*/
public_envs?: Record<string, string>;
/**
* Options for the `bunext sitemap` command
*/
sitemap?: BunextSitemapConfig;
};
export type BunextConfigMiddlewareParams = {
req: Request;
@@ -191,7 +195,19 @@ export type BunextPageModule = {
export type BunextPageServerModule = {
default?: BunextPageServerFn;
server?: BunextPageServerFn;
sitemap?: BunextPageSitemapFn;
};
/**
* Returns the sitemap entries for a page. Required for dynamic routes
* (`[slug]`) to appear in the sitemap. On a static page, the returned
* entries replace the page's default entry.
*/
export type BunextPageSitemapFn = (params: {
/**
* The page's route pattern, e.g. `/blog/[slug]`
*/
route: string;
}) => BunextSitemapAdditionalPaths | Promise<BunextSitemapAdditionalPaths>;
export type BunextHTMLProps = DetailedHTMLProps<HtmlHTMLAttributes<HTMLHtmlElement>, HTMLHtmlElement>;
export type BunextPageModuleMetaFn = (params: {
ctx: BunxRouteParams;
@@ -383,3 +399,55 @@ export type BuildDataType = {
build_date_unix: number;
build_date: string;
};
export type BunextSitemapChangeFreq = "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
export type BunextSitemapEntry = {
/**
* URL path (e.g. `/blog/my-post`) or absolute URL.
*/
path: string;
lastmod?: Date | string;
changefreq?: BunextSitemapChangeFreq;
/**
* Between `0.0` and `1.0`.
*/
priority?: number;
};
export type BunextSitemapAdditionalPaths = (string | BunextSitemapEntry)[];
export type BunextGenerateSitemapParams = {
/**
* Base URL prepended to every path. Defaults to `config.origin`,
* falling back to `http://localhost:<port>`.
*/
origin?: string;
/**
* Paths to leave out. A string matches a path exactly, or a whole
* subtree when it ends with `/*` (e.g. `/admin/*`). A RegExp is
* tested against the path.
*/
exclude?: (string | RegExp)[];
/**
* Extra entries, typically concrete URLs for dynamic routes
* (`[slug]`), which cannot be discovered from the file system. Can
* also be a function (sync or async) returning them, e.g. to load
* them from a database.
*/
additional_paths?: BunextSitemapAdditionalPaths | (() => BunextSitemapAdditionalPaths | Promise<BunextSitemapAdditionalPaths>);
/**
* Use each page file's modification time as `lastmod`. Defaults to
* `true`.
*/
use_file_lastmod?: boolean;
/**
* Append a trailing slash to every non-root path. Defaults to `false`.
*/
trailing_slash?: boolean;
default_changefreq?: BunextSitemapChangeFreq;
default_priority?: number;
};
export type BunextSitemapConfig = BunextGenerateSitemapParams & {
/**
* Output file, relative to the project root. Defaults to
* `public/sitemap.xml`, which is served at `/sitemap.xml`.
*/
output?: string;
};