fix(Wordpress Node): Send no-cache header on write requests (#20473)

Signed-off-by: Muhammed Hussain Karimi <info@karimi.dev>
Co-authored-by: Elias Meire <elias@meire.dev>
This commit is contained in:
Muhammed Hussain Karimi
2026-08-13 16:08:54 +00:00
committed by GitHub
parent 62d5de3ec7
commit 077370ff56
2 changed files with 31 additions and 4 deletions
@@ -42,11 +42,18 @@ export async function wordpressApiRequest(
rejectUnauthorized = !(credentials.allowUnauthorizedCerts as boolean);
}
const headers: IDataObject = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
// Some WordPress caching plugins ignore the request method and serve a cached
// GET response to writes, which silently returns existing posts instead of creating one.
if (!['GET', 'HEAD'].includes(method)) {
headers['Cache-Control'] = 'no-cache';
}
let options: IRequestOptions = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
headers,
method,
qs,
body,
@@ -46,6 +46,26 @@ describe('Wordpress > GenericFunctions', () => {
);
});
it('should set Cache-Control to no-cache for write requests', async () => {
mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' });
await wordpressApiRequest.call(mockFunctions, 'POST', '/posts', {}, {});
expect(mockFunctions.helpers.requestWithAuthentication).toHaveBeenCalledWith(
'wordpressApi',
expect.objectContaining({
headers: expect.objectContaining({ 'Cache-Control': 'no-cache' }),
}),
);
});
it('should not set Cache-Control for GET requests', async () => {
mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' });
await wordpressApiRequest.call(mockFunctions, 'GET', '/posts', {}, {});
const callArgs = mockFunctions.helpers.requestWithAuthentication.mock.calls[0][1];
expect(callArgs.headers).not.toHaveProperty('Cache-Control');
});
it('should throw NodeApiError on failure', async () => {
mockFunctions.helpers.requestWithAuthentication.mockRejectedValue({ message: 'fail' });
await expect(