Skip to content

Commit

Permalink
Fix/cache app router (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio authored Jul 25, 2024
1 parent 79ae20e commit 9f05f3f
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/tasty-waves-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/core": patch
"@headstartwp/next": patch
---

Fix fetch cache option
15 changes: 12 additions & 3 deletions packages/core/src/data/strategies/AbstractFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type NextJSHeaders = {
revalidate?: false | 0 | number;
tags?: string[];
};
cache?: 'no-store' | 'force-cache';
};

/**
Expand Down Expand Up @@ -94,6 +93,11 @@ export interface FetchOptions {
*/
burstCache?: boolean;

/**
* Cache option
*/
cache?: 'no-store' | 'force-cache';

/**
* Headers to sent to fetch
*/
Expand Down Expand Up @@ -309,7 +313,7 @@ export abstract class AbstractFetchStrategy<E, Params extends EndpointParams, R
): Promise<FetchResponse<R>> {
const { burstCache = false } = options;

const args = {};
const args: Record<string, unknown> = {};
const headers: Record<string, unknown> = options.headers ?? {};
const authHeader = this.getAuthHeader(options);

Expand All @@ -319,12 +323,17 @@ export abstract class AbstractFetchStrategy<E, Params extends EndpointParams, R

const previewAuthHeader = this.getPreviewAuthHeader(options);

if (options.cache) {
args.cache = options.cache;
}

if (options.previewToken) {
headers[this.getPreviewHeaderName(options)] = previewAuthHeader;
// always set cache to no store if preview token is present
args.cache = 'no-store';
}

if (Object.keys(headers).length > 0) {
// @ts-expect-error
args.headers = headers;
}

Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ export class PostsArchiveFetchStrategy<
const { burstCache = false } = options;
let finalUrl = url;
const settings = getSiteBySourceUrl(this.baseURL);
const args: Record<string, unknown> = {};
const headers: Record<string, unknown> = 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) {
Expand All @@ -411,7 +420,7 @@ export class PostsArchiveFetchStrategy<
} else {
const terms = await apiGet(
`${this.baseURL}${taxonomy.endpoint}?slug=${params[paramSlug]}`,
{},
args,
burstCache,
);

Expand All @@ -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,
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/data/strategies/SearchFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export class SinglePostFetchStrategy<
headers: {
[this.getPreviewHeaderName(options)]: authHeader,
},
cache: 'no-store',
},
burstCache,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -248,6 +249,7 @@ describe('SinglePostFetchStrategy', () => {
2,
'/wp-json/wp/v2/posts/1',
{
cache: 'no-store',
headers: { Authorization: 'Bearer test token' },
},
false,
Expand Down Expand Up @@ -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,
Expand All @@ -290,6 +293,7 @@ describe('SinglePostFetchStrategy', () => {
2,
'/wp-json/wp/v2/posts/1',
{
cache: 'no-store',
headers: { 'X-HeadstartWP-Authorization': 'Bearer test token' },
},
false,
Expand Down Expand Up @@ -317,6 +321,7 @@ describe('SinglePostFetchStrategy', () => {
1,
'/wp-json/wp/v2/posts/10',
{
cache: 'no-store',
headers: { Authorization: 'Bearer test token' },
},
false,
Expand Down Expand Up @@ -346,6 +351,7 @@ describe('SinglePostFetchStrategy', () => {
1,
'/wp-json/wp/v2/posts/10',
{
cache: 'no-store',
headers: { 'X-HeadstartWP-Authorization': 'Bearer test token' },
},
false,
Expand Down
6 changes: 2 additions & 4 deletions packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
});
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/rsc/data/queries/prepareQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export function prepareQuery<P extends EndpointParams>(

const options = merge<NextQueryProps<P>['options']>([
{
headers: {
cache: 'no-store',
},
cache: 'no-store',
},
rest.options ?? {},
]);
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/rsc/handlers/previewRouteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function previewRouteHandler(
options: {
alternativePreviewAuthorizationHeader:
preview?.alternativeAuthorizationHeader ?? false,
cache: 'no-store',
},
},
config,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/handlers/revalidateRouterHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const Single = async ({ params }: HeadstartWPRoute) => {
params: {
postType: ['post', 'page'],
},
options: {
headers: {
cache: 'force-cache',
},
},
});

return (
Expand Down
1 change: 0 additions & 1 deletion projects/wp-multisite-nextjs-app/src/app/[site]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
5 changes: 5 additions & 0 deletions projects/wp-nextjs-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const nextConfig = {

return config;
},
logging: {
fetches: {
fullUrl: true,
},
},
};

module.exports = withHeadstartWPConfig(nextConfig);
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const Single = async ({ params }: HeadstartWPRoute) => {
params: {
postType: ['post', 'page'],
},
options: {
headers: {
cache: 'force-cache',
},
},
});

return (
Expand Down

0 comments on commit 9f05f3f

Please sign in to comment.