Skip to content

Commit

Permalink
Update fresh and router to export path template (#44)
Browse files Browse the repository at this point in the history
* Update fresh and router to export path template

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Update deco version

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

---------

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
mcandeia authored Sep 12, 2023
1 parent 9c88490 commit 15bdbc3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
}
19 changes: 14 additions & 5 deletions website/handlers/fresh.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -15,17 +18,23 @@ export const isFreshCtx = <TState>(
};

/**
* @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) => {
const url = new URL(req.url);
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<DecoState>(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,
Expand Down
17 changes: 6 additions & 11 deletions website/handlers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -52,18 +52,11 @@ export const router = (
) => {
const ctx = { ...connInfo, params: (groups ?? {}) } as ConnInfo & {
params: Record<string, string>;
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,
Expand Down Expand Up @@ -166,7 +159,9 @@ export default function RoutesSelection(
ctx: AppContext,
): Handler {
return async (req: Request, connInfo: ConnInfo): Promise<Response> => {
const t = isFreshCtx<LiveState>(connInfo) ? connInfo.state.t : undefined;
const t = isFreshCtx<DecoSiteState>(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.
Expand Down

0 comments on commit 15bdbc3

Please sign in to comment.