Skip to content

Commit 0376075

Browse files
authored
feat: cache resources (#5381)
Ref #4077 How it works - Cache-Control opts in caching (Cache-Control: max-age=3600) - cached response is matched by url, method and body - cache is invalidated with Cache-Control change - responses stored with the cache are not replicated across regions - responses are not shared between projects Link for testing https://test-cache-hcevl.wstd.work/?message=hello
1 parent 217b026 commit 0376075

File tree

29 files changed

+136
-52
lines changed

29 files changed

+136
-52
lines changed

fixtures/react-router-cloudflare/app/routes/[another-page]._index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-cloudflare/app/routes/_index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-docker/app/routes/[another-page]._index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-docker/app/routes/_index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-netlify/app/routes/[another-page]._index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-netlify/app/routes/_index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-vercel/app/routes/[another-page]._index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/react-router-vercel/app/routes/_index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
loadResources,
1616
formIdFieldName,
1717
formBotFieldName,
18+
cachedFetch,
1819
} from "@webstudio-is/sdk/runtime";
1920
import {
2021
ReactSdkContext,
@@ -42,7 +43,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4243

4344
const customFetch: typeof fetch = (input, init) => {
4445
if (typeof input !== "string") {
45-
return fetch(input, init);
46+
return cachedFetch(projectId, input, init);
4647
}
4748

4849
if (isLocalResource(input, "sitemap.xml")) {
@@ -52,7 +53,7 @@ const customFetch: typeof fetch = (input, init) => {
5253
return Promise.resolve(response);
5354
}
5455

55-
return fetch(input, init);
56+
return cachedFetch(projectId, input, init);
5657
};
5758

5859
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isLocalResource,
1414
loadResource,
1515
loadResources,
16+
cachedFetch,
1617
formIdFieldName,
1718
formBotFieldName,
1819
} from "@webstudio-is/sdk/runtime";
@@ -43,7 +44,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4344

4445
const customFetch: typeof fetch = (input, init) => {
4546
if (typeof input !== "string") {
46-
return fetch(input, init);
47+
return cachedFetch(projectId, input, init);
4748
}
4849

4950
if (isLocalResource(input, "sitemap.xml")) {
@@ -53,7 +54,7 @@ const customFetch: typeof fetch = (input, init) => {
5354
return Promise.resolve(response);
5455
}
5556

56-
return fetch(input, init);
57+
return cachedFetch(projectId, input, init);
5758
};
5859

5960
export const loader = async (arg: LoaderFunctionArgs) => {

fixtures/webstudio-cloudflare-template/app/routes/_index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isLocalResource,
1414
loadResource,
1515
loadResources,
16+
cachedFetch,
1617
formIdFieldName,
1718
formBotFieldName,
1819
} from "@webstudio-is/sdk/runtime";
@@ -43,7 +44,7 @@ import { sitemap } from "../__generated__/$resources.sitemap.xml";
4344

4445
const customFetch: typeof fetch = (input, init) => {
4546
if (typeof input !== "string") {
46-
return fetch(input, init);
47+
return cachedFetch(projectId, input, init);
4748
}
4849

4950
if (isLocalResource(input, "sitemap.xml")) {
@@ -53,7 +54,7 @@ const customFetch: typeof fetch = (input, init) => {
5354
return Promise.resolve(response);
5455
}
5556

56-
return fetch(input, init);
57+
return cachedFetch(projectId, input, init);
5758
};
5859

5960
export const loader = async (arg: LoaderFunctionArgs) => {

0 commit comments

Comments
 (0)