From c3ba3ca31f1cb3de9b2b222f24afecbbde3e2a56 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Tue, 25 Jun 2024 16:33:07 -0300 Subject: [PATCH 01/22] Add cache api as default and add corresponding methods --- runtime/caches/common.ts | 2 ++ runtime/caches/mod.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/caches/common.ts b/runtime/caches/common.ts index 42604c446..ffc62284e 100644 --- a/runtime/caches/common.ts +++ b/runtime/caches/common.ts @@ -63,6 +63,8 @@ export const withInstrumentation = ( const cacheImpl = await cache.open(cacheName); return { ...cacheImpl, + delete: cacheImpl.delete.bind(cacheImpl), + put: cacheImpl.put.bind(cacheImpl), match: async (req, opts) => { const span = tracer.startSpan("cache-match", { attributes: { engine }, diff --git a/runtime/caches/mod.ts b/runtime/caches/mod.ts index 26637ae18..11b92a09e 100644 --- a/runtime/caches/mod.ts +++ b/runtime/caches/mod.ts @@ -11,7 +11,7 @@ import { createTieredCache } from "./tiered.ts"; export const ENABLE_LOADER_CACHE = Deno.env.get("ENABLE_LOADER_CACHE") === "true"; -const DEFAULT_CACHE_ENGINE = "KV"; +const DEFAULT_CACHE_ENGINE = "CACHE_API"; const WEB_CACHE_ENGINES: CacheEngine[] = Deno.env.has("WEB_CACHE_ENGINE") ? Deno.env.get("WEB_CACHE_ENGINE")!.split(",") as CacheEngine[] : [DEFAULT_CACHE_ENGINE]; From 71c45cd71ae93fc17bfd602f0b21f32045f20142 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Tue, 25 Jun 2024 16:35:41 -0300 Subject: [PATCH 02/22] CACHI API hard coded for testing - uncommit later --- runtime/caches/mod.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/runtime/caches/mod.ts b/runtime/caches/mod.ts index 11b92a09e..23db7d136 100644 --- a/runtime/caches/mod.ts +++ b/runtime/caches/mod.ts @@ -12,9 +12,10 @@ import { createTieredCache } from "./tiered.ts"; export const ENABLE_LOADER_CACHE = Deno.env.get("ENABLE_LOADER_CACHE") === "true"; const DEFAULT_CACHE_ENGINE = "CACHE_API"; -const WEB_CACHE_ENGINES: CacheEngine[] = Deno.env.has("WEB_CACHE_ENGINE") - ? Deno.env.get("WEB_CACHE_ENGINE")!.split(",") as CacheEngine[] - : [DEFAULT_CACHE_ENGINE]; +const WEB_CACHE_ENGINES: CacheEngine[] = [DEFAULT_CACHE_ENGINE]; +// Deno.env.has("WEB_CACHE_ENGINE") +// ? Deno.env.get("WEB_CACHE_ENGINE")!.split(",") as CacheEngine[] +// : [DEFAULT_CACHE_ENGINE]; export interface CacheStorageOption { implementation: CacheStorage; From 8b979f3fc0ba803f7d368774d8f5da0578fd1539 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Fri, 28 Jun 2024 09:39:44 -0300 Subject: [PATCH 03/22] Enable cache by default --- blocks/loader.ts | 4 ++-- runtime/caches/mod.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 57306a2d0..8d6beae58 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -99,9 +99,9 @@ export const wrapCaughtErrors = async < }; export const LOADER_CACHE_START_TRESHOLD = - Deno.env.get("LOADER_CACHE_START_TRESHOLD") ?? 5; + Deno.env.get("LOADER_CACHE_START_TRESHOLD") ?? 0; -export const LOADER_CACHE_SIZE = Deno.env.get("LOADER_CACHE_SIZE") ?? 1_024; +export const LOADER_CACHE_SIZE = Deno.env.get("LOADER_CACHE_SIZE") ?? 1_024_000; const stats = { cache: meter.createCounter("loader_cache", { diff --git a/runtime/caches/mod.ts b/runtime/caches/mod.ts index 23db7d136..9b7d44916 100644 --- a/runtime/caches/mod.ts +++ b/runtime/caches/mod.ts @@ -9,8 +9,7 @@ import { // import { caches as cachesS3, isS3Available } from "./s3.ts"; import { createTieredCache } from "./tiered.ts"; -export const ENABLE_LOADER_CACHE = - Deno.env.get("ENABLE_LOADER_CACHE") === "true"; +export const ENABLE_LOADER_CACHE = true; const DEFAULT_CACHE_ENGINE = "CACHE_API"; const WEB_CACHE_ENGINES: CacheEngine[] = [DEFAULT_CACHE_ENGINE]; // Deno.env.has("WEB_CACHE_ENGINE") From 3be0dc1302316c16d85265764c110d170d5029c4 Mon Sep 17 00:00:00 2001 From: carcara-gustavo <166416316+carcara-gustavo@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:54:12 -0300 Subject: [PATCH 04/22] fix: add state in cacheKey function (#706) --- blocks/app.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/blocks/app.ts b/blocks/app.ts index a24e2bca5..6a3e7f7fd 100644 --- a/blocks/app.ts +++ b/blocks/app.ts @@ -221,7 +221,11 @@ const injectAppStateOnManifest = < ), loaders: mapObjKeys( manifest.loaders ?? {}, - (mod) => ({ ...mod, default: injectAppState(state, mod.default) }), + (mod) => ({ + ...mod, + default: injectAppState(state, mod.default), + cacheKey: mod.cacheKey && injectAppState(state, mod.cacheKey), + }), ), }; }; From d4d13ee4724555b79806b3a2c08bf11e0e63aaf5 Mon Sep 17 00:00:00 2001 From: gimenes Date: Wed, 3 Jul 2024 12:54:31 -0300 Subject: [PATCH 05/22] Release [1.75.2] --- deno.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deno.json b/deno.json index abf859fef..c8ebd34c5 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@deco-cx/deco", - "version": "1.75.1", + "version": "1.75.2", "exports": "./mod.ts", "tasks": { "start": "deno task check", From e12195d733bcadff4a5532e0342a387161e14afb Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Wed, 10 Jul 2024 08:26:36 -0300 Subject: [PATCH 06/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28ea8a922..1fa86be7b 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ The deno project created with Deco is completely standalone — all of the CMS i This means you can deploy a Deco project easily to any hosting platform you want. By using our integrated hosting partners, you get full first-class environment support an observability inside Deco. > [!WARNING] -> Self-hosting the editor itself is coming in early 2024. Bear with us as we refactor some innards before we can invite more developers to extend it! We're looking forward to it. +> Self-hosting the editor itself is coming in early 2025. Bear with us as we refactor some innards before we can invite more developers to extend it! We're looking forward to it. ## Deploy to the deco.cx PRO edge From 8a15d52f9c0c07f775d76f3a79591328a488d587 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Tue, 16 Jul 2024 15:11:29 -0300 Subject: [PATCH 07/22] Add some logs for debugging in DD --- blocks/loader.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 8d6beae58..b0c44704e 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -173,6 +173,7 @@ const wrapLoader = ( const start = performance.now(); let status: "bypass" | "miss" | "stale" | "hit" | undefined; const cacheKeyValue = cacheKey(props, req, ctx); + logger.info("cacheKeyValue: ", cacheKeyValue); try { // Should skip cache if ( @@ -181,6 +182,7 @@ const wrapLoader = ( !isCache(maybeCache) || cacheKeyValue === null ) { + logger.info("bypassed the cache"); status = "bypass"; stats.cache.add(1, { status, loader }); @@ -250,22 +252,24 @@ const wrapLoader = ( const callHandlerAndCache = async () => { const json = await handler(props, req, ctx); + const response = new Response(JSON.stringify(json), { + headers: { + "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) + .toUTCString(), + }, + }) + logger.info("caching the following request: ", request, "\nAnd response: ", response); cache.put( request, - new Response(JSON.stringify(json), { - headers: { - "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) - .toUTCString(), - }, - }), - ).catch((error) => logger.error(`loader error ${error}`)); + response, + ).catch((error) => logger.error(`loader error ${error} -- callhandlerandcache`)); return json; }; const staleWhileRevalidate = async () => { const matched = await cache.match(request).catch(() => null); - + logger.info(`matched: ${matched}`); if (!matched) { status = "miss"; stats.cache.add(1, { status, loader }); @@ -281,7 +285,7 @@ const wrapLoader = ( stats.cache.add(1, { status, loader }); callHandlerAndCache().catch((error) => - logger.error(`loader error ${error}`) + logger.error(`loader error ${error} -- stale`) ); } else { status = "hit"; From 1186959dee2de7cfa71060446c0189aa99665d8d Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Wed, 17 Jul 2024 12:16:56 -0300 Subject: [PATCH 08/22] Add more logs for debugging purposes --- blocks/loader.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index b0c44704e..06d9cd654 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -1,6 +1,6 @@ // deno-lint-ignore-file no-explicit-any import JsonViewer from "../components/JsonViewer.tsx"; -import { ValueType, weakcache } from "../deps.ts"; +import { LoggerProvider, ValueType, weakcache } from "../deps.ts"; import type { Block, BlockModule, InstanceOf } from "../engine/block.ts"; import { FieldResolver } from "../engine/core/resolver.ts"; import { singleFlight } from "../engine/core/utils.ts"; @@ -258,18 +258,20 @@ const wrapLoader = ( .toUTCString(), }, }) - logger.info("caching the following request: ", request, "\nAnd response: ", response); + logger.info("caching the following request: ", JSON.stringify(request), "\nAnd response: ", JSON.stringify(response)); cache.put( request, response, - ).catch((error) => logger.error(`loader error ${error} -- callhandlerandcache`)); + ).catch((error) => { + logger.info(`ERROR -> request ${JSON.stringify(request)} -- response ${JSON.stringify(response)} -- callhandlerandcache`); + logger.error(`ERROR -> loader error ${error} -- callhandlerandcache`)}); return json; }; const staleWhileRevalidate = async () => { const matched = await cache.match(request).catch(() => null); - logger.info(`matched: ${matched}`); + logger.info(`matched: ${JSON.stringify(matched)}`); if (!matched) { status = "miss"; stats.cache.add(1, { status, loader }); From ddf66c39890b45cbd5c3d68e4886889a2aedcc77 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Wed, 24 Jul 2024 16:43:46 -0300 Subject: [PATCH 09/22] add more logs for debugging --- blocks/loader.ts | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 06d9cd654..bc175b9a0 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -62,7 +62,7 @@ export const wrapCaughtErrors = async < return ctx.next!(); } try { - return await ctx.next!(); + return await ctx.next!(); // Seems like here } catch (err) { if (err instanceof HttpError) { throw err; @@ -173,7 +173,7 @@ const wrapLoader = ( const start = performance.now(); let status: "bypass" | "miss" | "stale" | "hit" | undefined; const cacheKeyValue = cacheKey(props, req, ctx); - logger.info("cacheKeyValue: ", cacheKeyValue); + console.log("loader.ts cacheKeyValue: ", cacheKeyValue, " props: ", props, " req: ", req); try { // Should skip cache if ( @@ -182,7 +182,7 @@ const wrapLoader = ( !isCache(maybeCache) || cacheKeyValue === null ) { - logger.info("bypassed the cache"); + console.log("bypassed the cache, cacheKeyValue: ", cacheKeyValue, " mode: ", mode, " ENABLE_LOADER_CACHE: ", ENABLE_LOADER_CACHE, " isCache(maybeCache): ", isCache(maybeCache)); status = "bypass"; stats.cache.add(1, { status, loader }); @@ -251,34 +251,53 @@ const wrapLoader = ( const request = new Request(url); const callHandlerAndCache = async () => { - const json = await handler(props, req, ctx); + let json; + try { + json = await handler(props, req, ctx); // aqui tá rolando o erro + } catch (error) { + console.log("ERROR -> handler error: ", error); + } + console.log("Length of json in call handler and cache: ", JSON.stringify(json) ? JSON.stringify(json).length : 0); const response = new Response(JSON.stringify(json), { headers: { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) .toUTCString(), }, - }) - logger.info("caching the following request: ", JSON.stringify(request), "\nAnd response: ", JSON.stringify(response)); + }); + console.log( + "caching the following request: ", + request, + "\nAnd response: ", + response, + "Request headers: ", request.headers, + ); cache.put( request, response, ).catch((error) => { - logger.info(`ERROR -> request ${JSON.stringify(request)} -- response ${JSON.stringify(response)} -- callhandlerandcache`); - logger.error(`ERROR -> loader error ${error} -- callhandlerandcache`)}); + console.log( + `ERROR -> request ${request} -- response ${ + response + } -- callhandlerandcache`, + ); + console.error( + `ERROR -> loader error ${error} -- callhandlerandcache`, + ); + }); return json; }; const staleWhileRevalidate = async () => { const matched = await cache.match(request).catch(() => null); - logger.info(`matched: ${JSON.stringify(matched)}`); + console.log(`matched: ${matched}`); if (!matched) { status = "miss"; stats.cache.add(1, { status, loader }); return await callHandlerAndCache(); } - + console.log("matched headers: ", matched.headers); const expires = matched.headers.get("expires"); const isStale = expires ? !inFuture(expires) : false; @@ -286,15 +305,14 @@ const wrapLoader = ( status = "stale"; stats.cache.add(1, { status, loader }); - callHandlerAndCache().catch((error) => - logger.error(`loader error ${error} -- stale`) - ); + callHandlerAndCache(); } else { status = "hit"; stats.cache.add(1, { status, loader }); } - - return await matched.json(); + const matchedJson = matched.json(); + console.log("matchedJson: ", matchedJson); + return await matchedJson; }; return await flights.do(request.url, staleWhileRevalidate); From a18d8f674b94eb40225cb25e583c6f9f3819658d Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Wed, 24 Jul 2024 18:16:09 -0300 Subject: [PATCH 10/22] more logs and content lenght on header --- blocks/loader.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index bc175b9a0..f940473da 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -254,14 +254,16 @@ const wrapLoader = ( let json; try { json = await handler(props, req, ctx); // aqui tá rolando o erro + json = JSON.stringify(json); } catch (error) { console.log("ERROR -> handler error: ", error); } - console.log("Length of json in call handler and cache: ", JSON.stringify(json) ? JSON.stringify(json).length : 0); - const response = new Response(JSON.stringify(json), { + console.log("Length of json in call handler and cache: ", json ? json.length : 0); + const response = new Response(json, { headers: { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) .toUTCString(), + "Content-Length": json.length.toString(), }, }); console.log( @@ -276,9 +278,11 @@ const wrapLoader = ( response, ).catch((error) => { console.log( - `ERROR -> request ${request} -- response ${ - response - } -- callhandlerandcache`, + "ERROR -> request", + request, + "\nAnd response: ", + response, + " -- callhandlerandcache", ); console.error( `ERROR -> loader error ${error} -- callhandlerandcache`, From 8801fe700d14b5add7e5dd4b8e954e020f213889 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Wed, 24 Jul 2024 18:33:51 -0300 Subject: [PATCH 11/22] redo json stuff --- blocks/loader.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index f940473da..e328dcdab 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -254,16 +254,15 @@ const wrapLoader = ( let json; try { json = await handler(props, req, ctx); // aqui tá rolando o erro - json = JSON.stringify(json); } catch (error) { console.log("ERROR -> handler error: ", error); } - console.log("Length of json in call handler and cache: ", json ? json.length : 0); - const response = new Response(json, { + console.log("Length of json in call handler and cache: ", JSON.stringify(json) ? JSON.stringify(json).length : 0); + const response = new Response(JSON.stringify(json), { headers: { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) .toUTCString(), - "Content-Length": json.length.toString(), + "Content-Length": JSON.stringify(json) ? JSON.stringify(json).length.toString() : "0", }, }); console.log( From 2286d6705af7233ca82a7688cede9a1ef5e0ad88 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Fri, 26 Jul 2024 01:26:55 -0300 Subject: [PATCH 12/22] add more headers --- blocks/loader.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index e328dcdab..d34fe9ac3 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -257,12 +257,14 @@ const wrapLoader = ( } catch (error) { console.log("ERROR -> handler error: ", error); } - console.log("Length of json in call handler and cache: ", JSON.stringify(json) ? JSON.stringify(json).length : 0); - const response = new Response(JSON.stringify(json), { + const jsonString = JSON.stringify(json); + console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0); + const response = new Response(jsonString, { headers: { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) .toUTCString(), - "Content-Length": JSON.stringify(json) ? JSON.stringify(json).length.toString() : "0", + "Content-Length": jsonString ? jsonString.length.toString() : "0", + "content-length": jsonString ? jsonString.length.toString() : "0", }, }); console.log( From 2a998a47d8753ed2cff4b3588fb1087c64da06f6 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Fri, 26 Jul 2024 01:52:33 -0300 Subject: [PATCH 13/22] read the json and print it, let's debug this yay --- blocks/loader.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/blocks/loader.ts b/blocks/loader.ts index d34fe9ac3..1119200f7 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -316,7 +316,9 @@ const wrapLoader = ( stats.cache.add(1, { status, loader }); } const matchedJson = matched.json(); + const anotherMatchedJson = await matched.clone().json(); console.log("matchedJson: ", matchedJson); + console.log("anotherMatchedJson: ", anotherMatchedJson); return await matchedJson; }; From 92bd07029b083f1c8e1ccfc50437401316c5e38e Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Fri, 26 Jul 2024 01:53:22 -0300 Subject: [PATCH 14/22] json type --- blocks/loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 1119200f7..b13bcb69c 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -258,7 +258,7 @@ const wrapLoader = ( console.log("ERROR -> handler error: ", error); } const jsonString = JSON.stringify(json); - console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0); + console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0, " type of the json: ", typeof json); const response = new Response(jsonString, { headers: { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) From 89be66fa6d8cb329ae9ea1c2aa65d896dd747d9a Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 11:10:01 -0300 Subject: [PATCH 15/22] Remove noisy logs, refactor headers --- blocks/loader.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index b13bcb69c..e6f17b4f4 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -173,7 +173,6 @@ const wrapLoader = ( const start = performance.now(); let status: "bypass" | "miss" | "stale" | "hit" | undefined; const cacheKeyValue = cacheKey(props, req, ctx); - console.log("loader.ts cacheKeyValue: ", cacheKeyValue, " props: ", props, " req: ", req); try { // Should skip cache if ( @@ -182,7 +181,7 @@ const wrapLoader = ( !isCache(maybeCache) || cacheKeyValue === null ) { - console.log("bypassed the cache, cacheKeyValue: ", cacheKeyValue, " mode: ", mode, " ENABLE_LOADER_CACHE: ", ENABLE_LOADER_CACHE, " isCache(maybeCache): ", isCache(maybeCache)); + // console.log("bypassed the cache, cacheKeyValue: ", cacheKeyValue, " mode: ", mode, " ENABLE_LOADER_CACHE: ", ENABLE_LOADER_CACHE, " isCache(maybeCache): ", isCache(maybeCache)); status = "bypass"; stats.cache.add(1, { status, loader }); @@ -250,6 +249,7 @@ const wrapLoader = ( url.searchParams.set("cacheKey", cacheKeyValue); const request = new Request(url); + // console.log("loader.ts cacheKeyValue: ", cacheKeyValue, " props: ", props, " req: ", req); const callHandlerAndCache = async () => { let json; try { @@ -258,20 +258,24 @@ const wrapLoader = ( console.log("ERROR -> handler error: ", error); } const jsonString = JSON.stringify(json); - console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0, " type of the json: ", typeof json); + console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0); + + const headers: { [key: string]: string } = { + "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)).toUTCString() + }; + + if (jsonString && jsonString.length > 0) { + headers["Content-Length"] = (jsonString.length * 2).toString(); + } + const response = new Response(jsonString, { - headers: { - "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)) - .toUTCString(), - "Content-Length": jsonString ? jsonString.length.toString() : "0", - "content-length": jsonString ? jsonString.length.toString() : "0", - }, + headers: headers, }); console.log( "caching the following request: ", - request, + request.url, "\nAnd response: ", - response, + response.url, "Request headers: ", request.headers, ); cache.put( @@ -280,9 +284,9 @@ const wrapLoader = ( ).catch((error) => { console.log( "ERROR -> request", - request, + request.url, "\nAnd response: ", - response, + response.url, " -- callhandlerandcache", ); console.error( @@ -295,7 +299,7 @@ const wrapLoader = ( const staleWhileRevalidate = async () => { const matched = await cache.match(request).catch(() => null); - console.log(`matched: ${matched}`); + // console.log(`matched: ${matched}`); if (!matched) { status = "miss"; stats.cache.add(1, { status, loader }); @@ -316,9 +320,7 @@ const wrapLoader = ( stats.cache.add(1, { status, loader }); } const matchedJson = matched.json(); - const anotherMatchedJson = await matched.clone().json(); - console.log("matchedJson: ", matchedJson); - console.log("anotherMatchedJson: ", anotherMatchedJson); + // console.log("matchedJson: ", matchedJson); return await matchedJson; }; From da2ddd25d03688655b676ac30d93fcc585dfeec0 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 11:33:08 -0300 Subject: [PATCH 16/22] add content type to headers --- blocks/loader.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index e6f17b4f4..eed8321fd 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -261,11 +261,12 @@ const wrapLoader = ( console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0); const headers: { [key: string]: string } = { - "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)).toUTCString() + "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)).toUTCString(), }; if (jsonString && jsonString.length > 0) { headers["Content-Length"] = (jsonString.length * 2).toString(); + headers["Content-Type"] = "application/json; charset=utf-8"; } const response = new Response(jsonString, { From 3895cab33136eac4908502f0cf258d12995c6c86 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 11:42:30 -0300 Subject: [PATCH 17/22] remove * --- blocks/loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index eed8321fd..80f334dd9 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -265,7 +265,7 @@ const wrapLoader = ( }; if (jsonString && jsonString.length > 0) { - headers["Content-Length"] = (jsonString.length * 2).toString(); + headers["Content-Length"] = (jsonString.length).toString(); headers["Content-Type"] = "application/json; charset=utf-8"; } From 4c0a6e366e780ea32be11d42b63a8f53e666cc19 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 11:56:09 -0300 Subject: [PATCH 18/22] Add matched text log --- blocks/loader.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 80f334dd9..4288b4640 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -320,9 +320,9 @@ const wrapLoader = ( status = "hit"; stats.cache.add(1, { status, loader }); } - const matchedJson = matched.json(); - // console.log("matchedJson: ", matchedJson); - return await matchedJson; + const matchedText = await matched.text(); + console.log("matchedText: ", matchedText); + return JSON.parse(matchedText); }; return await flights.do(request.url, staleWhileRevalidate); From 1509b9c3fd2b5b7576b783b126621995537a34a7 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 14:02:04 -0300 Subject: [PATCH 19/22] add error handling and stuff --- blocks/loader.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 4288b4640..f00a5ce84 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -321,8 +321,15 @@ const wrapLoader = ( stats.cache.add(1, { status, loader }); } const matchedText = await matched.text(); + console.log("Length of matchedText: ", matchedText.length); console.log("matchedText: ", matchedText); - return JSON.parse(matchedText); + try { + return JSON.parse(matchedText); + } catch (error) { + console.error("Error parsing JSON: ", error.message); + console.error("Faulty JSON snippet: ", matchedText.slice(4262941 - 20, 4262941 + 20)); // Log around the error position + throw error; // Re-throw the error after logging for debugging purposes + } }; return await flights.do(request.url, staleWhileRevalidate); From a03ea59fa399b36e4508ac3461cbb6c0f9e13116 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 14:22:17 -0300 Subject: [PATCH 20/22] change faulty log slice to -500 --- blocks/loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index f00a5ce84..77c039d6a 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -327,7 +327,7 @@ const wrapLoader = ( return JSON.parse(matchedText); } catch (error) { console.error("Error parsing JSON: ", error.message); - console.error("Faulty JSON snippet: ", matchedText.slice(4262941 - 20, 4262941 + 20)); // Log around the error position + console.error("Faulty JSON snippet: ", matchedText.slice(-500)); // Log the last 500 characters of matched text throw error; // Re-throw the error after logging for debugging purposes } }; From bf160438e57f775530982dfbfe9e844ee5e98e56 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Thu, 1 Aug 2024 16:08:03 -0300 Subject: [PATCH 21/22] remove problematic headers --- blocks/loader.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 77c039d6a..774f11edd 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -262,12 +262,13 @@ const wrapLoader = ( const headers: { [key: string]: string } = { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)).toUTCString(), + "Content-Type": "application/json", }; - if (jsonString && jsonString.length > 0) { - headers["Content-Length"] = (jsonString.length).toString(); - headers["Content-Type"] = "application/json; charset=utf-8"; - } + // if (jsonString && jsonString.length > 0) { + // headers["Content-Length"] = (jsonString.length).toString(); + // headers["Content-Type"] = "application/json; charset=utf-8"; + // } const response = new Response(jsonString, { headers: headers, From 0a12818d41eaa61f9d1aa1a4fa895fc38e748744 Mon Sep 17 00:00:00 2001 From: Itamar Rocha Filho Date: Fri, 2 Aug 2024 10:14:50 -0300 Subject: [PATCH 22/22] byte size correctly --- blocks/loader.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/blocks/loader.ts b/blocks/loader.ts index 774f11edd..56e151202 100644 --- a/blocks/loader.ts +++ b/blocks/loader.ts @@ -258,19 +258,21 @@ const wrapLoader = ( console.log("ERROR -> handler error: ", error); } const jsonString = JSON.stringify(json); + const jsonStringEncoded = new TextEncoder().encode(jsonString); console.log("Length of json in call handler and cache: ", jsonString ? jsonString.length : 0); + console.log("Length of jsonEncoded in call handler and cache: ", jsonStringEncoded ? jsonStringEncoded.length : 0); const headers: { [key: string]: string } = { "expires": new Date(Date.now() + (MAX_AGE_S * 1e3)).toUTCString(), "Content-Type": "application/json", }; + + if (jsonStringEncoded && jsonStringEncoded.length > 0) { + headers["Content-Length"] = "" + jsonStringEncoded.length; + // headers["Content-Type"] = "application/json; charset=utf-8"; + } - // if (jsonString && jsonString.length > 0) { - // headers["Content-Length"] = (jsonString.length).toString(); - // headers["Content-Type"] = "application/json; charset=utf-8"; - // } - - const response = new Response(jsonString, { + const response = new Response(jsonStringEncoded, { headers: headers, }); console.log(