Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix render-to-string timings #72

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compat/$live/handlers/devPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { adminUrlFor, isAdmin } from "deco/utils/admin.ts";
import { ConnInfo } from "std/http/server.ts";
import Fresh from "../../../website/handlers/fresh.ts";
import { pageIdFromMetadata } from "../../../website/pages/Page.tsx";

import { AppContext } from "../mod.ts";
export interface DevConfig {
page: Page;
}
Expand All @@ -13,8 +13,8 @@ export interface DevConfig {
* @title Private Fresh Page
* @description Useful for pages under development.
*/
export default function DevPage(devConfig: DevConfig) {
const freshHandler = Fresh(devConfig);
export default function DevPage(devConfig: DevConfig, ctx: AppContext) {
const freshHandler = Fresh(devConfig, ctx);
return (req: Request, ctx: ConnInfo) => {
const referer = req.headers.get("origin") ?? req.headers.get("referer");
const isOnAdmin = referer && isAdmin(referer);
Expand Down
3 changes: 2 additions & 1 deletion compat/$live/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import type { Manifest as WebSiteManifest } from "../../website/manifest.gen.ts"
import webSite, { Props } from "../../website/mod.ts";
import type { Manifest as WorkflowsManifest } from "../../workflows/manifest.gen.ts";

import { SourceMap } from "deco/blocks/app.ts";
import { AppContext as AC, SourceMap } from "deco/blocks/app.ts";
import workflows from "../../workflows/mod.ts";
import manifest, { Manifest as _Manifest } from "./manifest.gen.ts";

export { onBeforeResolveProps } from "../../website/mod.ts";
export type AppContext = AC<ReturnType<typeof App>>;
export type Manifest = Omit<LiveManifest, "routes" | "islands"> & _Manifest;

export type ManifestMappings = Partial<
Expand Down
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.37.0/"
"deco/": "https://deno.land/x/deco@1.37.1/"
}
}
13 changes: 11 additions & 2 deletions website/handlers/fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { DecoState } from "deco/types.ts";
import { allowCorsFor } from "deco/utils/http.ts";
import { ConnInfo } from "std/http/server.ts";
import { AppContext } from "../mod.ts";

/**
* @title Fresh Config
Expand All @@ -26,27 +27,35 @@ export const isFreshCtx = <TState>(
* @title Fresh Page
* @description Renders a fresh page.
*/
export default function Fresh(freshConfig: FreshConfig) {
export default function Fresh(
freshConfig: FreshConfig,
appContext: Pick<AppContext, "monitoring">,
) {
return async (req: Request, ctx: ConnInfo) => {
if (req.method === "HEAD") {
return new Response(null, { status: 200 });
}
const endResolvePage = appContext?.monitoring?.t?.start?.("load-data");
const page =
isDeferred<Page, BaseContext & { context: ConnInfo }>(freshConfig.page)
? await freshConfig.page({ context: ctx })
: freshConfig.page;
endResolvePage?.();
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({
const end = appContext?.monitoring?.t?.start?.("render-to-string");
const response = await ctx.render({
page,
routerInfo: {
flags: ctx.state.flags,
pagePath: ctx.state.pathTemplate,
},
});
end?.();
return response;
}
return Response.json({ message: "Fresh is not being used" }, {
status: 500,
Expand Down
Loading