Skip to content

Commit c163790

Browse files
authored
Merge pull request #26 from deco-sites/jonasjesus/deco-5276-section-swr-caching
perf(cache): SWR-cache the Instagram section loader (DECO-5276)
2 parents 3e9673a + fe86e78 commit c163790

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/setup/section-loaders.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ import {
1212
withSearchParam,
1313
compose,
1414
} from "@decocms/start/cms";
15+
import { createCachedLoader } from "@decocms/start/sdk/cachedLoader";
16+
17+
// Instagram's loader hits the Graph API (graph.instagram.com) on every request.
18+
// The feed changes slowly, so cache it with SWR (the "static" profile: ~5min
19+
// fresh, served-if-error window) — keyed by props (token/nposts), not the URL.
20+
// This avoids calling Instagram per page view. (Route + edge caching for
21+
// pages is already provided by cmsRouteConfig's routeCacheDefaults and the
22+
// worker's detectCacheProfile.)
23+
const cachedInstagramLoader = createCachedLoader(
24+
"instagram-posts",
25+
async (props: any) => {
26+
const mod: any = await import("~/sections/Social/InstagramPosts");
27+
return typeof mod.loader === "function" ? mod.loader(props) : props;
28+
},
29+
"static",
30+
);
1531

1632
registerSectionLoaders({
1733
"site/sections/Newsletter/Newsletter.tsx": async (props: any, req: Request) => {
1834
const mod: any = await import("~/sections/Newsletter/Newsletter");
1935
return typeof mod.loader === "function" ? mod.loader(props, req) : props;
2036
},
21-
"site/sections/Social/InstagramPosts.tsx": async (props: any, req: Request) => {
22-
const mod: any = await import("~/sections/Social/InstagramPosts");
23-
return typeof mod.loader === "function" ? mod.loader(props, req) : props;
24-
},
37+
"site/sections/Social/InstagramPosts.tsx": (props: any) =>
38+
cachedInstagramLoader(props),
2539
// SearchResult needs the request URL to rebase filter/sort/pagination links
2640
// (commerce loaders return URLs with the wrong origin/pathname).
2741
"site/sections/Product/SearchResult.tsx": async (props: any, req: Request) => {

0 commit comments

Comments
 (0)