Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: cache resources (#5381)
Bogdan Chadkin037607560503
diff --git a/fixtures/react-router-cloudflare/app/routes/[another-page]._index.tsx b/fixtures/react-router-cloudflare/app/routes/[another-page]._index.tsxindex 17d3491d0b..13f5ce08b0 100644--- a/fixtures/react-router-cloudflare/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-cloudflare/app/routes/[another-page]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-cloudflare/app/routes/_index.tsx b/fixtures/react-router-cloudflare/app/routes/_index.tsxindex dec09db556..23ce05241b 100644--- a/fixtures/react-router-cloudflare/app/routes/_index.tsx+++ b/fixtures/react-router-cloudflare/app/routes/_index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-docker/app/routes/[another-page]._index.tsx b/fixtures/react-router-docker/app/routes/[another-page]._index.tsxindex 17d3491d0b..13f5ce08b0 100644--- a/fixtures/react-router-docker/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-docker/app/routes/[another-page]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-docker/app/routes/_index.tsx b/fixtures/react-router-docker/app/routes/_index.tsxindex dec09db556..23ce05241b 100644--- a/fixtures/react-router-docker/app/routes/_index.tsx+++ b/fixtures/react-router-docker/app/routes/_index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx b/fixtures/react-router-netlify/app/routes/[another-page]._index.tsxindex 17d3491d0b..13f5ce08b0 100644--- a/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-netlify/app/routes/_index.tsx b/fixtures/react-router-netlify/app/routes/_index.tsxindex dec09db556..23ce05241b 100644--- a/fixtures/react-router-netlify/app/routes/_index.tsx+++ b/fixtures/react-router-netlify/app/routes/_index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx b/fixtures/react-router-vercel/app/routes/[another-page]._index.tsxindex 17d3491d0b..13f5ce08b0 100644--- a/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/react-router-vercel/app/routes/_index.tsx b/fixtures/react-router-vercel/app/routes/_index.tsxindex dec09db556..23ce05241b 100644--- a/fixtures/react-router-vercel/app/routes/_index.tsx+++ b/fixtures/react-router-vercel/app/routes/_index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsxindex 8b285af2d9..6c357a9e8d 100644--- a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx+++ b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx@@ -13,6 +13,7 @@ import {isLocalResource,loadResource,loadResources,+ cachedFetch,formIdFieldName,formBotFieldName,} from "@webstudio-is/sdk/runtime";@@ -43,7 +44,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -53,7 +54,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsxindex 98dfabad53..1baf468dce 100644--- a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx+++ b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx@@ -13,6 +13,7 @@ import {isLocalResource,loadResource,loadResources,+ cachedFetch,formIdFieldName,formBotFieldName,} from "@webstudio-is/sdk/runtime";@@ -43,7 +44,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -53,7 +54,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsxindex edc9ce20a7..77fb67185a 100644--- a/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[animations]._index.tsx b/fixtures/webstudio-features/app/routes/[animations]._index.tsxindex e765d6ae71..f240fdfef0 100644--- a/fixtures/webstudio-features/app/routes/[animations]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[animations]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[class-names]._index.tsx b/fixtures/webstudio-features/app/routes/[class-names]._index.tsxindex b1f500d541..d8791547f4 100644--- a/fixtures/webstudio-features/app/routes/[class-names]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[class-names]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[content-block]._index.tsx b/fixtures/webstudio-features/app/routes/[content-block]._index.tsxindex 4707da8caa..939509cbed 100644--- a/fixtures/webstudio-features/app/routes/[content-block]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[content-block]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[duration]._index.tsx b/fixtures/webstudio-features/app/routes/[duration]._index.tsxindex 41ce6045d8..31ee645445 100644--- a/fixtures/webstudio-features/app/routes/[duration]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[duration]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[expressions]._index.tsx b/fixtures/webstudio-features/app/routes/[expressions]._index.tsxindex ea8fc413a6..b2a0f977ef 100644--- a/fixtures/webstudio-features/app/routes/[expressions]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[expressions]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[form]._index.tsx b/fixtures/webstudio-features/app/routes/[form]._index.tsxindex 1b5da5bec0..c331a23705 100644--- a/fixtures/webstudio-features/app/routes/[form]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[form]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx b/fixtures/webstudio-features/app/routes/[head-tag]._index.tsxindex 18a7523107..85071d61b2 100644--- a/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx b/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsxindex 88e4ae7fb7..d5566ad117 100644--- a/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx b/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsxindex ef970f1cdc..dfca5b9247 100644--- a/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[radix]._index.tsx b/fixtures/webstudio-features/app/routes/[radix]._index.tsxindex 035aa84f2d..dc0c7ceef6 100644--- a/fixtures/webstudio-features/app/routes/[radix]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[radix]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[resources]._index.tsx b/fixtures/webstudio-features/app/routes/[resources]._index.tsxindex 91664fecce..f2d19bd039 100644--- a/fixtures/webstudio-features/app/routes/[resources]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[resources]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/[text-duration]._index.tsx b/fixtures/webstudio-features/app/routes/[text-duration]._index.tsxindex eaf7487522..4cda7f1b4c 100644--- a/fixtures/webstudio-features/app/routes/[text-duration]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[text-duration]._index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/fixtures/webstudio-features/app/routes/_index.tsx b/fixtures/webstudio-features/app/routes/_index.tsxindex dec09db556..23ce05241b 100644--- a/fixtures/webstudio-features/app/routes/_index.tsx+++ b/fixtures/webstudio-features/app/routes/_index.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsxindex 12581dcc36..fb3c6295e4 100644--- a/packages/cli/templates/defaults/app/route-templates/html.tsx+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx@@ -13,6 +13,7 @@ import {isLocalResource,loadResource,loadResources,+ cachedFetch,formIdFieldName,formBotFieldName,} from "@webstudio-is/sdk/runtime";@@ -43,7 +44,7 @@ import { sitemap } from "__SITEMAP__";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -53,7 +54,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/packages/cli/templates/react-router/app/route-templates/html.tsx b/packages/cli/templates/react-router/app/route-templates/html.tsxindex b9dbbbb842..894af05a9c 100644--- a/packages/cli/templates/react-router/app/route-templates/html.tsx+++ b/packages/cli/templates/react-router/app/route-templates/html.tsx@@ -15,6 +15,7 @@ import {loadResources,formIdFieldName,formBotFieldName,+ cachedFetch,} from "@webstudio-is/sdk/runtime";import {ReactSdkContext,@@ -42,7 +43,7 @@ import { sitemap } from "__SITEMAP__";const customFetch: typeof fetch = (input, init) => {if (typeof input !== "string") {- return fetch(input, init);+ return cachedFetch(projectId, input, init);}if (isLocalResource(input, "sitemap.xml")) {@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}- return fetch(input, init);+ return cachedFetch(projectId, input, init);};export const loader = async (arg: LoaderFunctionArgs) => {diff --git a/packages/sdk/package.json b/packages/sdk/package.jsonindex 24f057e15c..0cef9f8c0a 100644--- a/packages/sdk/package.json+++ b/packages/sdk/package.json@@ -41,6 +41,7 @@"dts": "tsc --project tsconfig.dts.json"},"dependencies": {+ "@emotion/hash": "^0.9.2","@webstudio-is/css-engine": "workspace:*","@webstudio-is/fonts": "workspace:*","@webstudio-is/icons": "workspace:*",diff --git a/packages/sdk/src/resource-loader.ts b/packages/sdk/src/resource-loader.tsindex dd2f3504b1..53422de21b 100644--- a/packages/sdk/src/resource-loader.ts+++ b/packages/sdk/src/resource-loader.ts@@ -1,3 +1,4 @@+import hash from "@emotion/hash";import type { ResourceRequest } from "./schema/resources";const LOCAL_RESOURCE_PREFIX = "$resources";@@ -89,3 +90,56 @@ export const loadResources = async ());};++/**+ * cache api supports only get method+ * put hash of method and body into url+ * to support for example graphql queries+ */+const getCacheKey = async (request: Request) => {+ const url = new URL(request.url);+ const method = request.method;+ const body = await request.clone().text();+ // invalidate cache when cache-control is changed+ const cacheControl = request.headers.get("Cache-Control");+ const resourceHash = hash(`${method}:${body}:${cacheControl}`);+ url.searchParams.set("ws-resource-hash", resourceHash);+ return url;+};++export const cachedFetch = async (+ namespace: string,+ input: RequestInfo | URL,+ init?: RequestInit+) => {+ if (globalThis.caches) {+ const request = new Request(input, init);+ const requestCacheControl = request.headers.get("Cache-Control");+ // make cache opt in with cache-control header+ if (!requestCacheControl) {+ return fetch(input, init);+ }+ const cache = await caches.open(namespace);+ const cacheKey = await getCacheKey(request);+ let response = await cache.match(cacheKey);+ if (response) {+ // avoid mutating cached response+ return new Response(response.body, response);+ }+ // load response when missing in cache+ response = await fetch(request);+ // avoid caching failed responses+ if (!response.ok) {+ return response;+ }+ // put Cache-Control from request into response+ // https://developers.cloudflare.com/workers/reference/how-the-cache-works/#cache-api+ // response.clone() does not remove read-only constraint from headers+ response = new Response(response.body, response);+ response.headers.set("Cache-Control", requestCacheControl);+ // avoid mutating cached response+ await cache.put(cacheKey, response.clone());+ return response;+ }+ return fetch(input, init);+};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a7ed11a7f6..ab03473982 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1805,6 +1805,9 @@ importers:packages/sdk:dependencies:+ '@emotion/hash':+ specifier: ^0.9.2+ version: 0.9.2'@webstudio-is/css-engine':specifier: workspace:*version: link:../css-engine