Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRY] Core data: extend stable key to use per_page and page query parts #56354

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 18 additions & 3 deletions packages/core-data/src/queried-data/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -60,6 +67,12 @@ export function getQueryParts( query ) {

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':
Expand Down Expand Up @@ -97,9 +110,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
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core-data/src/queried-data/test/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updating these tests due to OCD.

Given they they're checking for empty stableKeys here, I'm not very sure about this approach.

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,
} );
Expand Down Expand Up @@ -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,
} );
Expand All @@ -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,
} );
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe( 'reducer', () => {
default: { 1: true },
},
queries: {
default: { 's=a': { itemIds: [ 1 ] } },
default: { 'per_page=3&s=a': { itemIds: [ 1 ] } },
},
} );
} );
Expand Down
Loading