Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/ninety-lemons-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/headstartwp": minor
"@headstartwp/core": minor
---

Introducing optimizeYoastPayload to reduce payload size when using the yoast integration
7 changes: 7 additions & 0 deletions packages/core/src/data/strategies/AbstractFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export interface EndpointParams {
*/
lang?: string;

/**
* The custom parameter to optimize the Yoast payload.
*
* This is only used if the YoastSEO integration is enabled
*/
optimizeYoastPayload?: boolean;

[k: string]: unknown;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCustomTaxonomies } from '../../utils';
import { getCustomTaxonomies, getSiteBySourceUrl } from '../../utils';
import { PostEntity } from '../types';
import { authorArchivesMatchers } from '../utils/matchers';
import { parsePath } from '../utils/parsePath';
Expand All @@ -25,6 +25,10 @@ export class AuthorArchiveFetchStrategy<
const matchers = [...authorArchivesMatchers];
const customTaxonomies = getCustomTaxonomies(this.baseURL);

const config = getSiteBySourceUrl(this.baseURL);

this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;

customTaxonomies?.forEach((taxonomy) => {
const slug = taxonomy?.rewrite ?? taxonomy.slug;
matchers.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './AbstractFetchStrategy';
import { PostParams, SinglePostFetchStrategy } from './SinglePostFetchStrategy';
import { PostsArchiveFetchStrategy, PostsArchiveParams } from './PostsArchiveFetchStrategy';
import { FrameworkError, NotFoundError } from '../../utils';
import { FrameworkError, NotFoundError, getSiteBySourceUrl } from '../../utils';

/**
* The params supported by {@link PostOrPostsFetchStrategy}
Expand Down Expand Up @@ -61,11 +61,17 @@ export class PostOrPostsFetchStrategy<

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

optimizeYoastPayload: boolean = false;

getDefaultEndpoint(): string {
return '@postOrPosts';
}

getParamsFromURL(path: string, params: Partial<P> = {}): Partial<P> {
const config = getSiteBySourceUrl(this.baseURL);

this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;

this.urlParams = {
single: this.postStrategy.getParamsFromURL(path, params.single),
archive: this.postsStrategy.getParamsFromURL(path, params.archive),
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/data/strategies/PostsArchiveFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export class PostsArchiveFetchStrategy<

locale: string = '';

optimizeYoastPayload: boolean = false;

getDefaultEndpoint(): string {
return endpoints.posts;
}
Expand All @@ -234,6 +236,8 @@ export class PostsArchiveFetchStrategy<
this.locale = config.integrations?.polylang?.enable && params.lang ? params.lang : '';
this.path = path;

this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;

const matchers = [...postsMatchers];

if (typeof params.taxonomy === 'string') {
Expand Down Expand Up @@ -457,6 +461,12 @@ export class PostsArchiveFetchStrategy<
}
}

if (this.optimizeYoastPayload) {
finalUrl = addQueryArgs(finalUrl, {
optimizeYoastPayload: true,
});
}

return super.fetcher(finalUrl, params, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class SearchNativeFetchStrategy<

locale: string = '';

optimizeYoastPayload: boolean = false;

getDefaultEndpoint() {
return endpoints.search;
}
Expand All @@ -94,6 +96,8 @@ export class SearchNativeFetchStrategy<
// Required for search lang url.
this.locale = config.integrations?.polylang?.enable && params.lang ? params.lang : '';

this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;

return parsePath(searchMatchers, path) as Partial<P>;
}

Expand Down Expand Up @@ -165,6 +169,10 @@ export class SearchNativeFetchStrategy<
queriedObject.search.yoast_head_json = seo_json;
}

if (this.optimizeYoastPayload) {
params.optimizeYoastPayload = true;
}

const response = await super.fetcher(url, params, {
...options,
throwIfNotFound: false,
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/data/strategies/SinglePostFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
removeSourceUrl,
NotFoundError,
getSiteBySourceUrl,
addQueryArgs,
} from '../../utils';
import { PostEntity } from '../types';
import { postMatchers } from '../utils/matchers';
Expand Down Expand Up @@ -90,6 +91,8 @@ export class SinglePostFetchStrategy<

shouldCheckCurrentPathAgainstPostLink: boolean = true;

optimizeYoastPayload: boolean = false;

getDefaultEndpoint(): string {
return endpoints.posts;
}
Expand All @@ -108,6 +111,8 @@ export class SinglePostFetchStrategy<

this.path = nonUrlParams.fullPath ?? path;

this.optimizeYoastPayload = !!config.integrations?.yoastSEO?.optimizeYoastPayload;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { year, day, month, ...params } = parsePath(postMatchers, path);

Expand Down Expand Up @@ -280,6 +285,7 @@ export class SinglePostFetchStrategy<
*/
async fetcher(url: string, params: P, options: Partial<FetchOptions> = {}) {
const { burstCache = false } = options;
let finalUrl = url;

if (params.authToken) {
options.previewToken = params.authToken;
Expand Down Expand Up @@ -310,7 +316,13 @@ export class SinglePostFetchStrategy<
}

try {
const result = await super.fetcher(url, params, options);
if (this.optimizeYoastPayload) {
finalUrl = addQueryArgs(finalUrl, {
optimizeYoastPayload: true,
});
}

const result = await super.fetcher(finalUrl, params, options);

return result;
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export interface Integration {
enable: boolean;
}

export interface YoastSEOIntegration extends Integration {}
export interface YoastSEOIntegration extends Integration {
optimizeYoastPayload?: boolean;
}

export interface PolylangIntegration extends Integration {}

Expand Down
7 changes: 7 additions & 0 deletions projects/wp-multisite-i18n-nextjs/headstartwp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ module.exports = {
useWordPressPlugin: true,
},
],

integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
6 changes: 6 additions & 0 deletions projects/wp-multisite-nextjs-app/headstartwp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ module.exports = {
hostUrl: 'http://js1.localhost:3000',
},
],
integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
6 changes: 6 additions & 0 deletions projects/wp-multisite-nextjs/headstartwp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ module.exports = {
useWordPressPlugin: true,
},
],
integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
1 change: 1 addition & 0 deletions projects/wp-nextjs-app/headstartwp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
1 change: 1 addition & 0 deletions projects/wp-nextjs/headstartwp.config.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
polylang: {
enable: process?.env?.NEXT_PUBLIC_ENABLE_POLYLANG_INTEGRATION === 'true',
Expand Down
4 changes: 4 additions & 0 deletions projects/wp-polylang-nextjs-app/headstartwp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ module.exports = {
polylang: {
enable: true,
},
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
integrations: {
yoastSEO: {
enable: true,
optimizeYoastPayload: true,
},
},
};
Loading
Loading