Skip to content

Commit fc8a39e

Browse files
lucymtcLucy Tomás Crossnicholasio
authored
Feature/yoast payload optimize (#727)
Co-authored-by: Lucy Tomás Cross <lucytomascross@Lucys-MacBook-Pro.local> Co-authored-by: Nícholas Oliveira <nicholas.andre@10up.com>
1 parent 27d2999 commit fc8a39e

17 files changed

Lines changed: 606 additions & 4 deletions

File tree

.changeset/ninety-lemons-care.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@headstartwp/headstartwp": minor
3+
"@headstartwp/core": minor
4+
---
5+
6+
Introducing optimizeYoastPayload to reduce payload size when using the yoast integration

packages/core/src/data/strategies/AbstractFetchStrategy.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export interface EndpointParams {
2828
*/
2929
lang?: string;
3030

31+
/**
32+
* The custom parameter to optimize the Yoast payload.
33+
*
34+
* This is only used if the YoastSEO integration is enabled
35+
*/
36+
optimizeYoastPayload?: boolean;
37+
3138
[k: string]: unknown;
3239
}
3340

packages/core/src/data/strategies/AuthorArchiveFetchStrategy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCustomTaxonomies } from '../../utils';
1+
import { getCustomTaxonomies, getSiteBySourceUrl } from '../../utils';
22
import { PostEntity } from '../types';
33
import { authorArchivesMatchers } from '../utils/matchers';
44
import { parsePath } from '../utils/parsePath';
@@ -25,6 +25,10 @@ export class AuthorArchiveFetchStrategy<
2525
const matchers = [...authorArchivesMatchers];
2626
const customTaxonomies = getCustomTaxonomies(this.baseURL);
2727

28+
const config = getSiteBySourceUrl(this.baseURL);
29+
30+
this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;
31+
2832
customTaxonomies?.forEach((taxonomy) => {
2933
const slug = taxonomy?.rewrite ?? taxonomy.slug;
3034
matchers.push({

packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './AbstractFetchStrategy';
99
import { PostParams, SinglePostFetchStrategy } from './SinglePostFetchStrategy';
1010
import { PostsArchiveFetchStrategy, PostsArchiveParams } from './PostsArchiveFetchStrategy';
11-
import { FrameworkError, NotFoundError } from '../../utils';
11+
import { FrameworkError, NotFoundError, getSiteBySourceUrl } from '../../utils';
1212

1313
/**
1414
* The params supported by {@link PostOrPostsFetchStrategy}
@@ -61,11 +61,17 @@ export class PostOrPostsFetchStrategy<
6161

6262
postsStrategy: PostsArchiveFetchStrategy = new PostsArchiveFetchStrategy(this.baseURL);
6363

64+
optimizeYoastPayload: boolean = false;
65+
6466
getDefaultEndpoint(): string {
6567
return '@postOrPosts';
6668
}
6769

6870
getParamsFromURL(path: string, params: Partial<P> = {}): Partial<P> {
71+
const config = getSiteBySourceUrl(this.baseURL);
72+
73+
this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;
74+
6975
this.urlParams = {
7076
single: this.postStrategy.getParamsFromURL(path, params.single),
7177
archive: this.postsStrategy.getParamsFromURL(path, params.archive),

packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export class PostsArchiveFetchStrategy<
211211

212212
locale: string = '';
213213

214+
optimizeYoastPayload: boolean = false;
215+
214216
getDefaultEndpoint(): string {
215217
return endpoints.posts;
216218
}
@@ -234,6 +236,8 @@ export class PostsArchiveFetchStrategy<
234236
this.locale = config.integrations?.polylang?.enable && params.lang ? params.lang : '';
235237
this.path = path;
236238

239+
this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;
240+
237241
const matchers = [...postsMatchers];
238242

239243
if (typeof params.taxonomy === 'string') {
@@ -457,6 +461,12 @@ export class PostsArchiveFetchStrategy<
457461
}
458462
}
459463

464+
if (this.optimizeYoastPayload) {
465+
finalUrl = addQueryArgs(finalUrl, {
466+
optimizeYoastPayload: true,
467+
});
468+
}
469+
460470
return super.fetcher(finalUrl, params, options);
461471
}
462472

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export class SearchNativeFetchStrategy<
7474

7575
locale: string = '';
7676

77+
optimizeYoastPayload: boolean = false;
78+
7779
getDefaultEndpoint() {
7880
return endpoints.search;
7981
}
@@ -94,6 +96,8 @@ export class SearchNativeFetchStrategy<
9496
// Required for search lang url.
9597
this.locale = config.integrations?.polylang?.enable && params.lang ? params.lang : '';
9698

99+
this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;
100+
97101
return parsePath(searchMatchers, path) as Partial<P>;
98102
}
99103

@@ -165,6 +169,10 @@ export class SearchNativeFetchStrategy<
165169
queriedObject.search.yoast_head_json = seo_json;
166170
}
167171

172+
if (this.optimizeYoastPayload) {
173+
params.optimizeYoastPayload = true;
174+
}
175+
168176
const response = await super.fetcher(url, params, {
169177
...options,
170178
throwIfNotFound: false,

packages/core/src/data/strategies/SinglePostFetchStrategy.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
removeSourceUrl,
66
NotFoundError,
77
getSiteBySourceUrl,
8+
addQueryArgs,
89
} from '../../utils';
910
import { PostEntity } from '../types';
1011
import { postMatchers } from '../utils/matchers';
@@ -90,6 +91,8 @@ export class SinglePostFetchStrategy<
9091

9192
shouldCheckCurrentPathAgainstPostLink: boolean = true;
9293

94+
optimizeYoastPayload: boolean = false;
95+
9396
getDefaultEndpoint(): string {
9497
return endpoints.posts;
9598
}
@@ -108,6 +111,8 @@ export class SinglePostFetchStrategy<
108111

109112
this.path = nonUrlParams.fullPath ?? path;
110113

114+
this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;
115+
111116
// eslint-disable-next-line @typescript-eslint/no-unused-vars
112117
const { year, day, month, ...params } = parsePath(postMatchers, path);
113118

@@ -289,6 +294,7 @@ export class SinglePostFetchStrategy<
289294
*/
290295
async fetcher(url: string, params: P, options: Partial<FetchOptions> = {}) {
291296
const { burstCache = false } = options;
297+
let finalUrl = url;
292298

293299
if (params.authToken) {
294300
options.previewToken = params.authToken;
@@ -319,7 +325,13 @@ export class SinglePostFetchStrategy<
319325
}
320326

321327
try {
322-
const result = await super.fetcher(url, params, options);
328+
if (this.optimizeYoastPayload) {
329+
finalUrl = addQueryArgs(finalUrl, {
330+
optimizeYoastPayload: true,
331+
});
332+
}
333+
334+
const result = await super.fetcher(finalUrl, params, options);
323335

324336
return result;
325337
} catch (e) {

packages/core/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export interface Integration {
4848
enable: boolean;
4949
}
5050

51-
export interface YoastSEOIntegration extends Integration {}
51+
export interface YoastSEOIntegration extends Integration {
52+
optimizeYoastPayload?: boolean;
53+
}
5254

5355
export interface PolylangIntegration extends Integration {}
5456

projects/wp-multisite-i18n-nextjs/headstartwp.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ module.exports = {
4545
useWordPressPlugin: true,
4646
},
4747
],
48+
49+
integrations: {
50+
yoastSEO: {
51+
enable: true,
52+
optimizeYoastPayload: true,
53+
},
54+
},
4855
};

projects/wp-multisite-nextjs-app/headstartwp.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ module.exports = {
2222
hostUrl: 'http://js1.localhost:3000',
2323
},
2424
],
25+
integrations: {
26+
yoastSEO: {
27+
enable: true,
28+
optimizeYoastPayload: true,
29+
},
30+
},
2531
};

0 commit comments

Comments
 (0)