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
5 changes: 5 additions & 0 deletions .changeset/free-rocks-take.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/core": patch
---

Fix: account for possible `single` property in the default post types when executing the default post path matching.
4 changes: 3 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ jobs:
build-test:
runs-on: ${{ matrix.os }}
strategy:
# This ensures only one combination runs at a time to avoid rate limiting
max-parallel: 1
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
exclude:
- os: windows-latest
node-version: 18
- os: windows-latest
node-version: 20
node-version: 20

steps:
- name: Configure Git line endings
Expand Down
21 changes: 21 additions & 0 deletions docs/documentation/01-Getting Started/headless-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ module.exports = {
}
```

Another use case is if you want your posts to sit at a different prefix (e.g: `/blog`), you can change your permalinks in WordPress (e.g: `/blog/%postname/`) and update the default `post` post type so that its `sigle` property is equal to `/blog`.

```js title="headstartwp.config.js"
module.exports = {
sourceUrl: process.env.NEXT_PUBLIC_HEADLESS_WP_URL,
hostUrl: process.env.HOST_URL,
customPostTypes: (defaultPostTypes) => {
return defaultPostTypes.map((postType) => {
if (postType === 'post') {
return {
...postType,
single: '/blog'
}
}

return postType;
};
}
}
```

## customTaxonomies

To add support for custom taxonomies, add your custom taxonomy to the `customTaxonomies` setting in `headstartwp.config.js`.
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/data/strategies/SinglePostFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ export class SinglePostFetchStrategy<
}
}

return postPath === currentPath || postPath === `/${this.locale}${currentPath}`;
const postPostTypeObject = getCustomPostType('post', this.baseURL);
const postSinglePrefix = postPostTypeObject?.single?.replace(/\/?$/, '') ?? '';

return (
postPath === `${postSinglePrefix}${currentPath}` ||
postPath === `/${this.locale}${postSinglePrefix}${currentPath}`
);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setHeadlessConfig } from '../../../utils';
import { getHeadstartWPConfig, setHeadlessConfig, setHeadstartWPConfig } from '../../../utils';
import { apiGet } from '../../api';
import { PostParams, SinglePostFetchStrategy } from '../SinglePostFetchStrategy';

Expand Down Expand Up @@ -439,20 +439,57 @@ describe('SinglePostFetchStrategy', () => {
});
});

it('handles post path mapping', async () => {
it.each([
{
settings: {
customPostTypes: [{ slug: 'post', single: '/', endpoint: '/wp-json/wp/v2/posts' }],
},
mockedData: [
{ title: 'test', id: 1, link: `http://sourceurl.com/%s` },
{
title: 'test',
id: 2,
link: `http://sourceurl.com/%s`,
},
],
},
{
settings: {
customPostTypes: [
{
slug: 'post',
single: '/blog',
archive: '/blog',
endpoint: '/wp-json/wp/v2/posts',
},
],
},
mockedData: [
{ title: 'test', id: 1, link: `http://sourceurl.com/blog/%s` },
{
title: 'test',
id: 2,
link: `http://sourceurl.com/blog/%s`,
},
],
},
])('handles post path mapping with and without prefix', async ({ settings, mockedData }) => {
const englishPostSlug = 'test';
const utf8EncodedPostSlug = 'لأخبار-المالية';
const originalSettings = getHeadstartWPConfig();

setHeadstartWPConfig({
...originalSettings,
...settings,
});

const post1 = {
title: 'test',
id: 1,
link: `http://sourceurl.com/${englishPostSlug}`,
...mockedData[0],
link: `${mockedData[0].link.replace('%s', englishPostSlug)}`,
};

const post2 = {
title: 'test',
id: 2,
link: `http://sourceurl.com/${encodeURIComponent(utf8EncodedPostSlug)}`,
...mockedData[1],
link: `${mockedData[1].link.replace('%s', encodeURIComponent(utf8EncodedPostSlug))}`,
};

apiGetMock.mockResolvedValue({
Expand Down
Loading