Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Feb 15, 2024
1 parent 834f790 commit 74a2c71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"std/": "https://deno.land/std@0.204.0/",
"partytown/": "https://deno.land/x/partytown@0.4.8/",
"deco-sites/std/": "https://denopkg.com/deco-sites/std@1.24.2/",
"deco/": "https://denopkg.com/deco-cx/deco@17e82e5da44aaee2e1932fe5944e07d493ea7cee/"
"deco/": "https://denopkg.com/deco-cx/deco@d3fda66b4845932da3e8039be36289166d1053da/"
},
"lock": false,
"tasks": {
Expand Down
25 changes: 16 additions & 9 deletions website/handlers/fresh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HandlerContext } from "$fresh/server.ts";
import { Page } from "deco/blocks/page.tsx";
import { CancelationToken } from "deco/deco.ts";
import { RequestContext } from "deco/deco.ts";
import {
asResolved,
BaseContext,
Expand Down Expand Up @@ -31,7 +31,10 @@ export const isFreshCtx = <TState>(
*/
export default function Fresh(
freshConfig: FreshConfig,
appContext: Pick<AppContext, "monitoring" | "response" | "caching" | "delay">,
appContext: Pick<
AppContext,
"monitoring" | "response" | "caching" | "firstByteThresholdMS"
>,
) {
return async (req: Request, ctx: ConnInfo) => {
if (req.method === "HEAD") {
Expand All @@ -44,24 +47,28 @@ export default function Fresh(

// Setup abort signals
const ctrl = new AbortController();
CancelationToken.set(ctrl.signal);

req.signal.addEventListener("abort", () => ctrl.abort());

// Add more cases when not allowed to abort due to MAX_AWAIT_TIME
const delay = appContext.delay || 100;
const delay = Number(appContext.firstByteThresholdMS);
if (asJson == null && delay) {
setTimeout(() => ctrl.abort(), delay);
}

const getPage = RequestContext.bind(
{ signal: ctrl.signal },
async () =>
isDeferred<Page, BaseContext & { context: ConnInfo }>(freshConfig.page)
? await freshConfig.page({ context: ctx })
: freshConfig.page,
);

const page = await appContext?.monitoring?.tracer?.startActiveSpan?.(
"load-data",
async (span) => {
try {
return isDeferred<Page, BaseContext & { context: ConnInfo }>(
freshConfig.page,
)
? await freshConfig.page({ context: ctx })
: freshConfig.page;
return await getPage();
} catch (e) {
span.recordException(e);
throw e;
Expand Down
4 changes: 2 additions & 2 deletions website/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export interface Props {

/**
* @title Async Rendering
* @description Number of milliseconds to wait before rendering preview. Set to 0 to disable it
* @description Number of milliseconds to wait before rendering preview. Set to 0 to disable it.
* @default 0
*/
delay?: number;
firstByteThresholdMS?: 0 | 100 | 300 | 500 | 700;
}

/**
Expand Down

0 comments on commit 74a2c71

Please sign in to comment.