Skip to content

Commit

Permalink
Go with fetch polyfill approach
Browse files Browse the repository at this point in the history
  • Loading branch information
bttf committed Apr 22, 2024
1 parent b2be1f4 commit dff1af9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 10 additions & 0 deletions next-js/src/app/gb-revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NextRequest } from "next/server";
import { revalidateTag } from "next/cache";

// Use a route handler to refresh all cache entries related to GrowthBook.
// GrowthBook SDK webhooks can be used to trigger this event immediately after
// making a change on the platform.
export const POST = (_req: NextRequest) => {
// TODO assert request is authorized from GrowthBook instance
revalidateTag("growthbook");
};
2 changes: 0 additions & 2 deletions next-js/src/app/server/combo/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createGB } from "@/lib/growthbook";
import ClientComponent from "./ClientComponent";

export const revalidate = 3600; // refresh cache every hour

export default async function ServerCombo() {
// create instance per request, server-side
const gb = createGB();
Expand Down
18 changes: 17 additions & 1 deletion next-js/src/lib/growthbook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import { GrowthBook, Context } from "@growthbook/growthbook";
import { GrowthBook, Context, setPolyfills } from "@growthbook/growthbook";

setPolyfills({
fetch: (
url: Parameters<typeof fetch>[0],
opts: Parameters<typeof fetch>[1]
) =>
fetch(url, {
...opts,
next: {
// cache results for an hour at most (static pages)
revalidate: 3600,
// see route handler for revalidating cache tagged under 'growthbook'
tags: ["growthbook"],
},
}),
});

// It's important to create a new instance per request when server-side to prevent
// race condtions from occurring between different user requests.
Expand Down

0 comments on commit dff1af9

Please sign in to comment.