From 15bdbc3d90249c1bc87e46bbfbcab70723b6aef5 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Tue, 12 Sep 2023 14:51:27 -0300 Subject: [PATCH] Update fresh and router to export path template (#44) * Update fresh and router to export path template Signed-off-by: Marcos Candeia * Update deco version Signed-off-by: Marcos Candeia --------- Signed-off-by: Marcos Candeia --- import_map.json | 2 +- website/handlers/fresh.ts | 19 ++++++++++++++----- website/handlers/router.ts | 17 ++++++----------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/import_map.json b/import_map.json index 8a300665a..913be61dc 100644 --- a/import_map.json +++ b/import_map.json @@ -9,6 +9,6 @@ "std/": "https://deno.land/std@0.190.0/", "partytown/": "https://deno.land/x/partytown@0.3.4/", "deco-sites/std/": "https://denopkg.com/deco-sites/std@1.22.0/", - "deco/": "https://deno.land/x/deco@1.35.7/" + "deco/": "https://deno.land/x/deco@1.35.12/" } } diff --git a/website/handlers/fresh.ts b/website/handlers/fresh.ts index bd59a425b..9f4ae7b9e 100644 --- a/website/handlers/fresh.ts +++ b/website/handlers/fresh.ts @@ -1,9 +1,12 @@ import { HandlerContext } from "$fresh/server.ts"; import { Page } from "deco/blocks/page.ts"; -import { RouterContext } from "deco/types.ts"; +import { DecoState } from "deco/types.ts"; import { allowCorsFor } from "deco/utils/http.ts"; import { ConnInfo } from "std/http/server.ts"; +/** + * @title Fresh Config + */ export interface FreshConfig { page: Page; } @@ -15,8 +18,8 @@ export const isFreshCtx = ( }; /** - * @title Page - * @description Renders a Page using Deno's Fresh. + * @title Fresh Page + * @description Renders a fresh page. */ export default function Fresh(page: FreshConfig) { return async (req: Request, ctx: ConnInfo) => { @@ -24,8 +27,14 @@ export default function Fresh(page: FreshConfig) { if (url.searchParams.get("asJson") !== null) { return Response.json(page, { headers: allowCorsFor(req) }); } - if (isFreshCtx<{ routerInfo: RouterContext }>(ctx)) { - return await ctx.render({ ...page, routerInfo: ctx.state.routerInfo }); + if (isFreshCtx(ctx)) { + return await ctx.render({ + ...page, + routerInfo: { + flags: ctx.state.flags, + pagePath: ctx.state.pathTemplate, + }, + }); } return Response.json({ message: "Fresh is not being used" }, { status: 500, diff --git a/website/handlers/router.ts b/website/handlers/router.ts index bd730df88..0fae66c98 100644 --- a/website/handlers/router.ts +++ b/website/handlers/router.ts @@ -8,7 +8,7 @@ import { isAwaitable } from "deco/engine/core/utils.ts"; import { FreshContext } from "deco/engine/manifest/manifest.ts"; import { isFreshCtx } from "deco/handlers/fresh.ts"; import { observe } from "deco/observability/observe.ts"; -import { Flag, LiveState, RouterContext } from "deco/types.ts"; +import { DecoSiteState, DecoState } from "deco/types.ts"; import { ConnInfo, Handler } from "std/http/server.ts"; import { Route, Routes } from "../flags/audience.ts"; import { AppContext } from "../mod.ts"; @@ -52,18 +52,11 @@ export const router = ( ) => { const ctx = { ...connInfo, params: (groups ?? {}) } as ConnInfo & { params: Record; - state: { - routes: Route[]; - routerInfo: RouterContext; - flags: Flag[]; - }; + state: DecoState; }; ctx.state.routes = routes; - ctx.state.routerInfo = { - flags: ctx.state.flags, - pagePath: routePath, - }; + ctx.state.pathTemplate = routePath; const resolvedOrPromise = isDeferred< Handler, @@ -166,7 +159,9 @@ export default function RoutesSelection( ctx: AppContext, ): Handler { return async (req: Request, connInfo: ConnInfo): Promise => { - const t = isFreshCtx(connInfo) ? connInfo.state.t : undefined; + const t = isFreshCtx(connInfo) + ? connInfo.state.t + : undefined; const routesFromProps = Array.isArray(audiences) ? audiences : []; // everyone should come first in the list given that we override the everyone value with the upcoming flags.