From 9f05f3f701204069bfbec8b395da40b3857e61da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcholas=20Andr=C3=A9?= Date: Thu, 25 Jul 2024 15:24:26 -0300 Subject: [PATCH] Fix/cache app router (#823) --- .changeset/tasty-waves-juggle.md | 6 ++++++ .../src/data/strategies/AbstractFetchStrategy.ts | 15 ++++++++++++--- .../data/strategies/PostsArchiveFetchStrategy.ts | 13 +++++++++++-- .../src/data/strategies/SearchFetchStrategy.ts | 2 +- .../data/strategies/SearchNativeFetchStrategy.ts | 2 +- .../data/strategies/SinglePostFetchStrategy.ts | 1 + .../__tests__/SinglePostFetchStrategy.ts | 6 ++++++ .../rsc/data/queries/__tests__/prepareQuery.ts | 6 ++---- .../next/src/rsc/data/queries/prepareQuery.ts | 4 +--- .../next/src/rsc/handlers/previewRouteHandler.ts | 1 + .../src/rsc/handlers/revalidateRouterHandler.ts | 2 +- .../src/app/[site]/(single)/[...path]/page.tsx | 5 ----- .../src/app/[site]/page.tsx | 1 - projects/wp-nextjs-app/next.config.js | 5 +++++ .../src/app/[lang]/(single)/[...path]/page.tsx | 5 ----- 15 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 .changeset/tasty-waves-juggle.md diff --git a/.changeset/tasty-waves-juggle.md b/.changeset/tasty-waves-juggle.md new file mode 100644 index 000000000..d9b8815d7 --- /dev/null +++ b/.changeset/tasty-waves-juggle.md @@ -0,0 +1,6 @@ +--- +"@headstartwp/core": patch +"@headstartwp/next": patch +--- + +Fix fetch cache option diff --git a/packages/core/src/data/strategies/AbstractFetchStrategy.ts b/packages/core/src/data/strategies/AbstractFetchStrategy.ts index 48841ffc0..615b67342 100644 --- a/packages/core/src/data/strategies/AbstractFetchStrategy.ts +++ b/packages/core/src/data/strategies/AbstractFetchStrategy.ts @@ -58,7 +58,6 @@ type NextJSHeaders = { revalidate?: false | 0 | number; tags?: string[]; }; - cache?: 'no-store' | 'force-cache'; }; /** @@ -94,6 +93,11 @@ export interface FetchOptions { */ burstCache?: boolean; + /** + * Cache option + */ + cache?: 'no-store' | 'force-cache'; + /** * Headers to sent to fetch */ @@ -309,7 +313,7 @@ export abstract class AbstractFetchStrategy> { const { burstCache = false } = options; - const args = {}; + const args: Record = {}; const headers: Record = options.headers ?? {}; const authHeader = this.getAuthHeader(options); @@ -319,12 +323,17 @@ export abstract class AbstractFetchStrategy 0) { - // @ts-expect-error args.headers = headers; } diff --git a/packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts b/packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts index a1cde0ea6..349556e7c 100644 --- a/packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts +++ b/packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts @@ -394,6 +394,15 @@ export class PostsArchiveFetchStrategy< const { burstCache = false } = options; let finalUrl = url; const settings = getSiteBySourceUrl(this.baseURL); + const args: Record = {}; + const headers: Record = options.headers ?? {}; + if (options.cache) { + args.cache = options.cache; + } + + if (Object.keys(headers).length > 0) { + args.headers = headers; + } const customTaxonomies = getCustomTaxonomies(this.baseURL); if (customTaxonomies) { @@ -411,7 +420,7 @@ export class PostsArchiveFetchStrategy< } else { const terms = await apiGet( `${this.baseURL}${taxonomy.endpoint}?slug=${params[paramSlug]}`, - {}, + args, burstCache, ); @@ -435,7 +444,7 @@ export class PostsArchiveFetchStrategy< if (params.author && typeof params.author === 'string' && !settings.useWordPressPlugin) { const authors = await apiGet( `${this.baseURL}${authorsEndpoint}?slug=${params.author}`, - {}, + args, burstCache, ); diff --git a/packages/core/src/data/strategies/SearchFetchStrategy.ts b/packages/core/src/data/strategies/SearchFetchStrategy.ts index ccbe1496e..849665ed7 100644 --- a/packages/core/src/data/strategies/SearchFetchStrategy.ts +++ b/packages/core/src/data/strategies/SearchFetchStrategy.ts @@ -61,7 +61,7 @@ export class SearchFetchStrategy< addQueryArgs(`${wpUrl}${endpoints.yoast}`, { url: `${wpUrl}${pageParam}/?s=${params.search ?? ''}${localeParam}`, }), - { headers: options.headers ?? {} }, + { headers: options.headers ?? {}, cache: options?.cache }, burstCache, ); diff --git a/packages/core/src/data/strategies/SearchNativeFetchStrategy.ts b/packages/core/src/data/strategies/SearchNativeFetchStrategy.ts index 388f0c16f..85f74e402 100644 --- a/packages/core/src/data/strategies/SearchNativeFetchStrategy.ts +++ b/packages/core/src/data/strategies/SearchNativeFetchStrategy.ts @@ -141,7 +141,7 @@ export class SearchNativeFetchStrategy< addQueryArgs(`${wpUrl}${endpoints.yoast}`, { url: `${wpUrl}${pageParam}/?s=${params.search ?? ''}${localeParam}`, }), - { headers: options.headers ?? {} }, + { headers: options.headers ?? {}, cache: options?.cache }, burstCache, ); diff --git a/packages/core/src/data/strategies/SinglePostFetchStrategy.ts b/packages/core/src/data/strategies/SinglePostFetchStrategy.ts index 92c12e5b2..7d7fdb7b1 100644 --- a/packages/core/src/data/strategies/SinglePostFetchStrategy.ts +++ b/packages/core/src/data/strategies/SinglePostFetchStrategy.ts @@ -290,6 +290,7 @@ export class SinglePostFetchStrategy< headers: { [this.getPreviewHeaderName(options)]: authHeader, }, + cache: 'no-store', }, burstCache, ); diff --git a/packages/core/src/data/strategies/__tests__/SinglePostFetchStrategy.ts b/packages/core/src/data/strategies/__tests__/SinglePostFetchStrategy.ts index 845b3d88c..321f59d7f 100644 --- a/packages/core/src/data/strategies/__tests__/SinglePostFetchStrategy.ts +++ b/packages/core/src/data/strategies/__tests__/SinglePostFetchStrategy.ts @@ -240,6 +240,7 @@ describe('SinglePostFetchStrategy', () => { 1, '/wp-json/wp/v2/posts/1/revisions?per_page=1', { + cache: 'no-store', headers: { Authorization: 'Bearer test token' }, }, false, @@ -248,6 +249,7 @@ describe('SinglePostFetchStrategy', () => { 2, '/wp-json/wp/v2/posts/1', { + cache: 'no-store', headers: { Authorization: 'Bearer test token' }, }, false, @@ -281,6 +283,7 @@ describe('SinglePostFetchStrategy', () => { 1, '/wp-json/wp/v2/posts/1/revisions?per_page=1', { + cache: 'no-store', headers: { 'X-HeadstartWP-Authorization': 'Bearer test token' }, }, false, @@ -290,6 +293,7 @@ describe('SinglePostFetchStrategy', () => { 2, '/wp-json/wp/v2/posts/1', { + cache: 'no-store', headers: { 'X-HeadstartWP-Authorization': 'Bearer test token' }, }, false, @@ -317,6 +321,7 @@ describe('SinglePostFetchStrategy', () => { 1, '/wp-json/wp/v2/posts/10', { + cache: 'no-store', headers: { Authorization: 'Bearer test token' }, }, false, @@ -346,6 +351,7 @@ describe('SinglePostFetchStrategy', () => { 1, '/wp-json/wp/v2/posts/10', { + cache: 'no-store', headers: { 'X-HeadstartWP-Authorization': 'Bearer test token' }, }, false, diff --git a/packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts b/packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts index 5e1707cf9..33be62b3e 100644 --- a/packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts +++ b/packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts @@ -29,12 +29,10 @@ describe('prepareQuery', () => { }); }); - it('default headers to no-store', () => { + it('default cache to no-store', () => { expect(prepareQuery({})).toMatchObject({ options: { - headers: { - cache: 'no-store', - }, + cache: 'no-store', }, }); }); diff --git a/packages/next/src/rsc/data/queries/prepareQuery.ts b/packages/next/src/rsc/data/queries/prepareQuery.ts index 9e1092ccb..52e06ca75 100644 --- a/packages/next/src/rsc/data/queries/prepareQuery.ts +++ b/packages/next/src/rsc/data/queries/prepareQuery.ts @@ -39,9 +39,7 @@ export function prepareQuery

( const options = merge['options']>([ { - headers: { - cache: 'no-store', - }, + cache: 'no-store', }, rest.options ?? {}, ]); diff --git a/packages/next/src/rsc/handlers/previewRouteHandler.ts b/packages/next/src/rsc/handlers/previewRouteHandler.ts index 3fc4e4a2b..96ad961d1 100644 --- a/packages/next/src/rsc/handlers/previewRouteHandler.ts +++ b/packages/next/src/rsc/handlers/previewRouteHandler.ts @@ -147,6 +147,7 @@ export async function previewRouteHandler( options: { alternativePreviewAuthorizationHeader: preview?.alternativeAuthorizationHeader ?? false, + cache: 'no-store', }, }, config, diff --git a/packages/next/src/rsc/handlers/revalidateRouterHandler.ts b/packages/next/src/rsc/handlers/revalidateRouterHandler.ts index 8714e8a77..3a5bb0f14 100644 --- a/packages/next/src/rsc/handlers/revalidateRouterHandler.ts +++ b/packages/next/src/rsc/handlers/revalidateRouterHandler.ts @@ -80,8 +80,8 @@ export async function revalidateRouteHandler(request: NextRequest) { const verifyTokenStrategy = new VerifyTokenFetchStrategy(sourceUrl); const { result } = await verifyTokenStrategy.get({ authToken: token, - // TODO: check if this is correct (it's a separate github issue) lang: typeof locale === 'string' ? locale : undefined, + cache: 'no-store', }); const verifiedPath = result.path ?? ''; diff --git a/projects/wp-multisite-nextjs-app/src/app/[site]/(single)/[...path]/page.tsx b/projects/wp-multisite-nextjs-app/src/app/[site]/(single)/[...path]/page.tsx index 4fd359fe4..cf7831f37 100644 --- a/projects/wp-multisite-nextjs-app/src/app/[site]/(single)/[...path]/page.tsx +++ b/projects/wp-multisite-nextjs-app/src/app/[site]/(single)/[...path]/page.tsx @@ -7,11 +7,6 @@ const Single = async ({ params }: HeadstartWPRoute) => { params: { postType: ['post', 'page'], }, - options: { - headers: { - cache: 'force-cache', - }, - }, }); return ( diff --git a/projects/wp-multisite-nextjs-app/src/app/[site]/page.tsx b/projects/wp-multisite-nextjs-app/src/app/[site]/page.tsx index 0704ee397..992afc2ea 100644 --- a/projects/wp-multisite-nextjs-app/src/app/[site]/page.tsx +++ b/projects/wp-multisite-nextjs-app/src/app/[site]/page.tsx @@ -2,7 +2,6 @@ import { BlocksRenderer } from '@headstartwp/core/react'; import { HeadstartWPRoute, queryPost } from '@headstartwp/next/app'; const Home = async ({ params }: HeadstartWPRoute) => { - console.log('HOME'); const { data } = await queryPost({ routeParams: params, params: { diff --git a/projects/wp-nextjs-app/next.config.js b/projects/wp-nextjs-app/next.config.js index c69600315..3aea7dbd8 100644 --- a/projects/wp-nextjs-app/next.config.js +++ b/projects/wp-nextjs-app/next.config.js @@ -11,6 +11,11 @@ const nextConfig = { return config; }, + logging: { + fetches: { + fullUrl: true, + }, + }, }; module.exports = withHeadstartWPConfig(nextConfig); diff --git a/projects/wp-polylang-nextjs-app/src/app/[lang]/(single)/[...path]/page.tsx b/projects/wp-polylang-nextjs-app/src/app/[lang]/(single)/[...path]/page.tsx index 4fd359fe4..cf7831f37 100644 --- a/projects/wp-polylang-nextjs-app/src/app/[lang]/(single)/[...path]/page.tsx +++ b/projects/wp-polylang-nextjs-app/src/app/[lang]/(single)/[...path]/page.tsx @@ -7,11 +7,6 @@ const Single = async ({ params }: HeadstartWPRoute) => { params: { postType: ['post', 'page'], }, - options: { - headers: { - cache: 'force-cache', - }, - }, }); return (