humanmade / repress

Connect your Redux store to the WordPress REST API.
Other
83 stars 11 forks source link

Add ability to short-circuit archive requests #34

Open rmccue opened 5 years ago

rmccue commented 5 years ago

In some cases, you may be able to short-circuit archive requests locally if you know how to match items on the client side.

For example, say you have a handler for posts, and you have dynamic archives using the slug:

props => {
    const query = {
        slug: props.slug,
    };
    posts.registerArchive( `slug/${ props.slug }`, query );
    return `slug/${ props.slug }`;
}

As a developer, you know that WordPress de-duplicates slugs, so you can generate an authoritative search and skip the backend request if you happen to already have the post loaded in (from another archive/single request, e.g.).

It'd be good to have the ability to write a local matcher, which would still fall back to the backend. This local matcher would be treated as authoritative by Repress, so it should only be used when you know for certain that you're going to match.

Something like this:

    posts.registerArchive( `slug/${ props.slug }`, query, {
        localMatcher: posts => posts.filter( post => post.slug === props.slug,
    } );

(Naming tbd)

rmccue commented 5 years ago

Naming-wise, I'm thinking "optimiser" might be a good shorthand.