Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import { createMemoryHistory } from "@octanejs/tanstack-router";import type { Middleware } from "@octanejs/vite-plugin";import type { AppRouter } from "./router.ts";
export const ROUTER_STATE_KEY = "tanstack.router";
/** * Build and load a server router for one request. Loading runs the matched * routes' loaders, so the render that follows reads settled data and the * router can report a redirect or a 404 before any HTML is sent. */export async function loadServerRouter(url: URL): Promise<AppRouter> { const { createAppRouter } = await import("./router.ts"); const router = createAppRouter({ history: createMemoryHistory({ initialEntries: [url.pathname + url.search], }), isServer: true, origin: url.origin, }); await router.load(); return router;}
/** * RenderRoute middleware: loads the router, hands it to <App> through request * state, and turns the router's outcome into the response status. */export const tanstackRouter: Middleware = async (context, next) => { const router = await loadServerRouter(context.url);
const redirect = router.state.redirect; if (redirect) { const href = redirect.options.href ?? redirect.headers.get("Location"); if (href) { return new Response(null, { status: redirect.status || 307, headers: { Location: new URL(href, context.url).toString() }, }); } }
context.state.set(ROUTER_STATE_KEY, router); const response = await next(); const status = router.stores.statusCode.get(); if (status === response.status || !response.ok) return response; return new Response(response.body, { status, headers: response.headers });};