Skip to content

Commit

Permalink
Defer page resolve (#71)
Browse files Browse the repository at this point in the history
* Defer page resolve

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

* Fix router redirects

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 21, 2023
1 parent 0ecac7d commit 0533157
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 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.9/",
"deco/": "https://deno.land/x/deco@1.36.20/"
"deco/": "https://deno.land/x/deco@1.37.0/"
}
}
25 changes: 23 additions & 2 deletions website/handlers/fresh.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import { Page } from "deco/blocks/page.ts";
import {
asResolved,
BaseContext,
isDeferred,
} from "deco/engine/core/resolver.ts";
import { DecoState } from "deco/types.ts";
import { allowCorsFor } from "deco/utils/http.ts";
import { ConnInfo } from "std/http/server.ts";
Expand All @@ -21,15 +26,22 @@ export const isFreshCtx = <TState>(
* @title Fresh Page
* @description Renders a fresh page.
*/
export default function Fresh(page: FreshConfig) {
export default function Fresh(freshConfig: FreshConfig) {
return async (req: Request, ctx: ConnInfo) => {
if (req.method === "HEAD") {
return new Response(null, { status: 200 });
}
const page =
isDeferred<Page, BaseContext & { context: ConnInfo }>(freshConfig.page)
? await freshConfig.page({ context: ctx })
: freshConfig.page;
const url = new URL(req.url);
if (url.searchParams.get("asJson") !== null) {
return Response.json(page, { headers: allowCorsFor(req) });
}
if (isFreshCtx<DecoState>(ctx)) {
return await ctx.render({
...page,
page,
routerInfo: {
flags: ctx.state.flags,
pagePath: ctx.state.pathTemplate,
Expand All @@ -41,3 +53,12 @@ export default function Fresh(page: FreshConfig) {
});
};
}

export const onBeforeResolveProps = (
props: FreshConfig,
) => {
if (props?.page) {
return { ...props, page: asResolved(props.page, true) };
}
return props;
};
7 changes: 5 additions & 2 deletions website/handlers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ export const router = (
ctx,
);
};
if (url.pathname && url.pathname in hrefRoutes) {

const handler = hrefRoutes[`${url.pathname}${url.search || ""}`] ??
hrefRoutes[url.pathname];
if (handler) {
return route(
hrefRoutes[url.pathname],
handler,
`${url.pathname}${url.search || ""}`,
);
}
Expand Down

0 comments on commit 0533157

Please sign in to comment.