htmx-router
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -13,3 +13,3 @@ import { Parameterized, Parameterizer, ParameterShaper } from "./util/parameters.js"; | ||
}; | ||
export declare function loader(ctx: RouteContext<typeof parameters>): Promise<Response | null>; | ||
export declare function loader(ctx: RouteContext<typeof parameters>): Promise<(Response | BodyInit) | null>; | ||
export declare const action: typeof loader; |
@@ -19,3 +19,3 @@ import type { RenderFunction, RouteContext } from "./index.js"; | ||
}; | ||
export declare function loader(ctx: RouteContext<typeof parameters>): Promise<Response | null>; | ||
export declare function loader(ctx: RouteContext<typeof parameters>): Promise<(Response | BodyInit) | null>; | ||
export declare const action: typeof loader; |
import { ParameterShaper } from '../util/parameters.js'; | ||
import { RouteContext } from "../router.js"; | ||
import { Cookies } from '../cookies.js'; | ||
type Rendered = Response | BodyInit; | ||
export declare class GenericContext { | ||
@@ -12,5 +13,6 @@ request: Request; | ||
url: URL; | ||
render: (res: JSX.Element, headers: Headers) => Promise<Response> | Response; | ||
render: (res: JSX.Element, headers: Headers) => Promise<Rendered> | Rendered; | ||
constructor(request: GenericContext["request"], url: GenericContext["url"], renderer: GenericContext["render"]); | ||
shape<T extends ParameterShaper>(shape: T): RouteContext<T>; | ||
} | ||
export {}; |
@@ -1,3 +0,3 @@ | ||
export declare function navigate(href: string, pushUrl?: boolean): Promise<void>; | ||
export declare function navigate(href: string, pushHistory?: boolean): Promise<void>; | ||
export declare function revalidate(): Promise<void>; | ||
export declare function htmxAppend(href: string, verb?: string): Promise<void>; |
@@ -7,5 +7,8 @@ function htmx() { | ||
} | ||
export async function navigate(href, pushUrl = true) { | ||
const driver = (typeof document === "object" ? document.createElement("a") : null); | ||
export async function navigate(href, pushHistory = true) { | ||
if (typeof window !== "object") | ||
return; | ||
if (!driver) | ||
return; | ||
const url = new URL(href, window.location.href); | ||
@@ -16,8 +19,15 @@ if (url.host !== window.location.host) { | ||
} | ||
// Perform an HTMX GET request similar to hx-boost | ||
await htmx().ajax("GET", href, { | ||
target: 'body', | ||
swap: 'outerHTML', | ||
history: pushUrl | ||
}); | ||
driver.setAttribute("hx-boost", "true"); | ||
driver.setAttribute("href", url.href); | ||
if (pushHistory) { | ||
driver.setAttribute("hx-push-url", "true"); | ||
driver.removeAttribute("hx-replace-url"); | ||
} | ||
else { | ||
driver.setAttribute("hx-replace-url", "true"); | ||
driver.removeAttribute("hx-push-url"); | ||
} | ||
document.body.appendChild(driver); | ||
htmx().process(driver); | ||
driver.click(); | ||
} | ||
@@ -30,5 +40,4 @@ export function revalidate() { | ||
target: 'body', | ||
swap: 'beforeend', | ||
history: false | ||
swap: 'beforeend' | ||
}); | ||
} |
{ | ||
"name": "htmx-router", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A lightweight SSR framework with server+client islands", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
export declare function text(text: string, init?: ResponseInit): Response; | ||
export declare function html(text: string, init?: ResponseInit): Response; | ||
export type TypedResponse<T> = Omit<Response, "json"> & { | ||
@@ -3,0 +4,0 @@ json(): Promise<T>; |
@@ -10,2 +10,11 @@ export function text(text, init) { | ||
} | ||
export function html(text, init) { | ||
init ||= {}; | ||
init.statusText ||= "ok"; | ||
init.status ||= 200; | ||
const res = new Response(text, init); | ||
res.headers.set("Content-Type", "text/html; charset=UTF-8"); | ||
res.headers.set("X-Caught", "true"); | ||
return res; | ||
} | ||
export function json(data, init) { | ||
@@ -12,0 +21,0 @@ init ||= {}; |
@@ -117,2 +117,4 @@ import { ServerOnlyWarning } from "./internal/util.js"; | ||
if (res instanceof Response) { | ||
if (res.ok) | ||
return res; | ||
if (100 <= res.status && res.status <= 399) | ||
@@ -131,3 +133,6 @@ return res; | ||
return null; | ||
return await this.index.resolve(ctx); | ||
const res = await this.index.resolve(ctx); | ||
if (res instanceof Response) | ||
return res; | ||
return new Response(res, { headers: ctx.headers }); | ||
} | ||
@@ -157,3 +162,5 @@ async resolveNext(fragments, ctx) { | ||
: null; | ||
return res; | ||
if (res instanceof Response) | ||
return res; | ||
return new Response(res, { headers: ctx.headers }); | ||
} | ||
@@ -163,5 +170,13 @@ async unwrap(ctx, res) { | ||
throw res; | ||
const caught = await this.slug.error(ctx, res); | ||
caught.headers.set("X-Caught", "true"); | ||
return caught; | ||
let caught = await this.slug.error(ctx, res); | ||
if (caught instanceof Response) { | ||
caught.headers.set("X-Caught", "true"); | ||
return caught; | ||
} | ||
ctx.headers.set("X-Caught", "true"); | ||
return new Response(caught, res instanceof Response ? res : { | ||
status: 500, | ||
statusText: "Internal Server Error", | ||
headers: ctx.headers | ||
}); | ||
} | ||
@@ -168,0 +183,0 @@ } |
81488
67
2351