import type { Middleware } from "@wrnexus/core"; import type { createStudioHandler } from "@wrnexus/design-studio/server"; let studio: ReturnType | undefined; const designStudio: Middleware = async (ctx, next) => { const url = new URL(ctx.req.url); if (url.pathname === "/design-studio") return new Response(null, { status: 308, headers: { location: "/design-studio/" }, }); if (!url.pathname.startsWith("/design-studio/")) return next(); // Load the installed package at runtime so its assets keep their package-relative URLs. const studioModule = import.meta.resolve("@wrnexus/design-studio/server"); studio ??= import(studioModule) .then(({ createStudioHandler }) => createStudioHandler({ hosted: { basePath: "/design-studio" }, }), ) .catch((error) => { studio = undefined; throw error; }); return (await studio)(ctx.req); }; export default designStudio;