diff --git a/app/[lang]/(live-current)/current-content-page/page.tsx b/app/[lang]/(live-current)/current-content-page/page.tsx index 618e081cc..f94f93c7e 100644 --- a/app/[lang]/(live-current)/current-content-page/page.tsx +++ b/app/[lang]/(live-current)/current-content-page/page.tsx @@ -24,6 +24,9 @@ export default async function CurrentContentPage({ { locale: params.lang, url: searchParams.uri, + }, + { + tags: [`${searchParams.uri}-${params.lang}`] } ) diff --git a/components/Current/Footer/index.tsx b/components/Current/Footer/index.tsx index 9b98e91fa..a8e1cb77c 100644 --- a/components/Current/Footer/index.tsx +++ b/components/Current/Footer/index.tsx @@ -9,9 +9,15 @@ import type { GetFooterData } from "@/types/requests/footer" import type { LangParams } from "@/types/params" export default async function Footer({ lang }: LangParams) { - const response = await request(GetFooter, { - locale: lang, - }) + const response = await request( + GetFooter, + { + locale: lang, + }, + { + tags: [`footer-${lang}`] + } + ) const footerData = response.data.all_footer.items[0] return ( diff --git a/components/Current/Header/index.tsx b/components/Current/Header/index.tsx index bf49075da..8a344f687 100644 --- a/components/Current/Header/index.tsx +++ b/components/Current/Header/index.tsx @@ -15,8 +15,6 @@ import type { HeaderQueryData } from "@/types/requests/header" import type { HeaderProps } from "@/types/components/current/header" import type { LanguageSwitcherQueryData } from "@/types/requests/languageSwitcher" - - export default async function Header({ lang, uid }: LangParams & HeaderProps) { try { const variables = { @@ -24,14 +22,16 @@ export default async function Header({ lang, uid }: LangParams & HeaderProps) { uid, } - const { data } = await request(GetHeader, { locale: lang }) + const { data } = await request(GetHeader, { locale: lang }, { tags: [`header-${lang}`] }) const { data: urls } = await batchRequest([ { document: GetDaDeEnUrls, + tags: [`DA-DE-EN-${uid}`], variables, }, { document: GetFiNoSvUrls, + tags: [`FI-NO-SV-${uid}`], variables, }, ]) diff --git a/lib/batchRequest.ts b/lib/batchRequest.ts index 1a5aa177d..319db94bd 100644 --- a/lib/batchRequest.ts +++ b/lib/batchRequest.ts @@ -4,10 +4,10 @@ import { request } from "./request" import type { Data } from "@/types/request" import type { BatchRequestDocument } from "graphql-request" -export async function batchRequest(queries: BatchRequestDocument[]): Promise> { +export async function batchRequest(queries: (BatchRequestDocument & NextFetchRequestConfig)[]): Promise> { try { const response = await Promise.allSettled( - queries.map(query => request(query.document, query.variables)) + queries.map(query => request(query.document, query.variables, { tags: query.tags })) ) let data = {} as T diff --git a/lib/request.ts b/lib/request.ts index cc333dfcb..abd365a3e 100644 --- a/lib/request.ts +++ b/lib/request.ts @@ -1,21 +1,27 @@ import "server-only" -import { request as graphqlRequest } from "graphql-request" +import { GraphQLClient } from "graphql-request" import { env } from "@/env/server" +import { cache } from "react" import type { Data } from "@/types/request" import type { DocumentNode } from "graphql" export async function request( query: string | DocumentNode, - variables?: {} + variables?: {}, + next?: NextFetchRequestConfig ): Promise> { try { - if (env.PRINT_QUERY) { - const graphqlRawRequest = (await import("graphql-request")).rawRequest - const print = (await import("graphql/language/printer")).print + const client = new GraphQLClient(env.CMS_URL, { + fetch: cache(async function (url: URL | RequestInfo, params: RequestInit | undefined) { + return fetch(url, params) + }), + next, + }) - const rawResponse = await graphqlRawRequest( - env.CMS_URL, + if (env.PRINT_QUERY) { + const print = (await import("graphql/language/printer")).print + const rawResponse = await client.rawRequest( print(query as DocumentNode), variables, { @@ -40,13 +46,12 @@ export async function request( } } - const response = await graphqlRequest({ + const response = await client.request({ document: query, requestHeaders: { access_token: env.CMS_ACCESS_TOKEN, "Content-Type": "application/json", }, - url: env.CMS_URL, variables, })