From b268b729ad1492e555f40046f2be54a113f66ffd Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 21 Nov 2023 14:38:53 +1100 Subject: [PATCH 1/3] This is a test to check whether extending the stable key with params, which should trigger new API calls, can be added to the state: per_page, page. --- .../src/queried-data/get-query-parts.js | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/core-data/src/queried-data/get-query-parts.js b/packages/core-data/src/queried-data/get-query-parts.js index b3744655728667..694722b7974bf0 100644 --- a/packages/core-data/src/queried-data/get-query-parts.js +++ b/packages/core-data/src/queried-data/get-query-parts.js @@ -8,6 +8,13 @@ import { addQueryArgs } from '@wordpress/url'; */ import { withWeakMapCache, getNormalizedCommaSeparable } from '../utils'; +function appendToStableKey( stableKey, key, value ) { + stableKey += + ( stableKey ? '&' : '' ) + + addQueryArgs( '', { [ key ]: value } ).slice( 1 ); + return stableKey; +} + /** * An object of properties describing a specific query. * @@ -56,10 +63,22 @@ export function getQueryParts( query ) { switch ( key ) { case 'page': parts[ key ] = Number( value ); + // Add query param to stableKey to ensure it's included in cache key. + parts.stableKey = appendToStableKey( + parts.stableKey, + key, + parts[ key ] + ); break; case 'per_page': parts.perPage = Number( value ); + // Add query param to stableKey to ensure it's included in cache key. + parts.stableKey = appendToStableKey( + parts.stableKey, + key, + parts.perPage + ); break; case 'context': @@ -97,9 +116,11 @@ export function getQueryParts( query ) { // should accept a key value pair, which may optimize its // implementation for our use here, vs. iterating an object // with only a single key. - parts.stableKey += - ( parts.stableKey ? '&' : '' ) + - addQueryArgs( '', { [ key ]: value } ).slice( 1 ); + parts.stableKey = appendToStableKey( + parts.stableKey, + key, + value + ); } } From 5803939be673ca7b995a3b1280425c8ee925733a Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 21 Nov 2023 15:05:07 +1100 Subject: [PATCH 2/3] No needs for page here. If a page cannot before, currently the selectors return `[]` --- packages/core-data/src/queried-data/get-query-parts.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/core-data/src/queried-data/get-query-parts.js b/packages/core-data/src/queried-data/get-query-parts.js index 694722b7974bf0..44b9b29f6eaa3b 100644 --- a/packages/core-data/src/queried-data/get-query-parts.js +++ b/packages/core-data/src/queried-data/get-query-parts.js @@ -63,12 +63,6 @@ export function getQueryParts( query ) { switch ( key ) { case 'page': parts[ key ] = Number( value ); - // Add query param to stableKey to ensure it's included in cache key. - parts.stableKey = appendToStableKey( - parts.stableKey, - key, - parts[ key ] - ); break; case 'per_page': From a59b454c6b1509dd391aa69ec15b57ccffd76905 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 21 Nov 2023 15:43:21 +1100 Subject: [PATCH 3/3] Updating tests just to see green --- .../core-data/src/queried-data/test/get-query-parts.js | 8 ++++---- packages/core-data/src/queried-data/test/reducer.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/queried-data/test/get-query-parts.js b/packages/core-data/src/queried-data/test/get-query-parts.js index f355be21a439b9..22307cbd2859af 100644 --- a/packages/core-data/src/queried-data/test/get-query-parts.js +++ b/packages/core-data/src/queried-data/test/get-query-parts.js @@ -4,14 +4,14 @@ import { getQueryParts } from '../get-query-parts'; describe( 'getQueryParts', () => { - it( 'parses out pagination data', () => { + it( 'parses out pagination data and adds to returned object', () => { const parts = getQueryParts( { page: 2, per_page: 2 } ); expect( parts ).toEqual( { context: 'default', page: 2, perPage: 2, - stableKey: '', + stableKey: 'per_page=2', fields: null, include: null, } ); @@ -71,7 +71,7 @@ describe( 'getQueryParts', () => { context: 'default', page: 1, perPage: 10, - stableKey: 'b=2', + stableKey: 'b=2&per_page=10', fields: null, include: null, } ); @@ -84,7 +84,7 @@ describe( 'getQueryParts', () => { context: 'default', page: 1, perPage: -1, - stableKey: 'b=2', + stableKey: 'b=2&per_page=-1', fields: null, include: null, } ); diff --git a/packages/core-data/src/queried-data/test/reducer.js b/packages/core-data/src/queried-data/test/reducer.js index d996f5b07ea22b..fd2c6cc24a3ff2 100644 --- a/packages/core-data/src/queried-data/test/reducer.js +++ b/packages/core-data/src/queried-data/test/reducer.js @@ -170,7 +170,7 @@ describe( 'reducer', () => { default: { 1: true }, }, queries: { - default: { 's=a': { itemIds: [ 1 ] } }, + default: { 'per_page=3&s=a': { itemIds: [ 1 ] } }, }, } ); } );