Skip to content

Commit

Permalink
fix: add 'force-cache' to query
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Oct 10, 2024
1 parent 193ca74 commit a6fdce2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
13 changes: 12 additions & 1 deletion app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { revalidatePath, revalidateTag } from "next/cache";
import { NextRequest } from "next/server";
import tags from "@/lib/tags";
import { languages, fallbackLng } from "@/lib/i18n/settings";

const REVALIDATE_SECRET_TOKEN = process.env.CRAFT_SECRET_TOKEN;
const CRAFT_HOMEPAGE_URI = "__home__";

export async function GET(request: NextRequest) {
const uri = request.nextUrl.searchParams.get("uri");
Expand All @@ -25,7 +27,16 @@ export async function GET(request: NextRequest) {
}

if (uri) {
revalidatePath(`/[locale]/${uri}`, "page");
languages.forEach((locale) => {
const parts: Array<string> = uri === CRAFT_HOMEPAGE_URI ? [] : [uri];
if (locale !== fallbackLng) {
parts.unshift(locale);
}

revalidatePath(`/${parts.join("/")}`);
});

revalidateTag(tags.globals);
Object.entries(tags).forEach(([, tag]) => {
revalidateTag(tag);
});
Expand Down
17 changes: 12 additions & 5 deletions lib/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,25 @@ export async function queryAPI<
token?: Token;
previewToken?: string;
}): Promise<OperationResult<Query, Variables>> {
const url = previewToken ? `${API_URL}?token=${previewToken}` : API_URL;
const params = new URLSearchParams({});

if (previewToken) {
params.append("token", previewToken);
}

const makeClient = () => {
return createClient({
url,
url: `${API_URL}?${params.toString()}`,
exchanges: [cacheExchange, fetchExchange],
fetchOptions: () => {
const opts = {
const opts: RequestInit = {
cache: "force-cache",
next: {
revalidate: previewToken ? 0 : 60,
revalidate: previewToken ? 0 : undefined,
},
};
if (!token) return { ...opts };

if (!token) return opts;
return {
...opts,
headers: {
Expand Down
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.

0 comments on commit a6fdce2

Please sign in to comment.