generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature: Issue-218: Cache Initial Post API Calls #219
Merged
efuller
merged 12 commits into
develop
from
feature/issue-218/cache-initial-query-api-calls
Aug 21, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2670864
Install SWR
efuller 623f209
Initial query block post SWR fetcher function
efuller 16a8575
Build posts API path query args
efuller b49a488
Sure up types and formatting
efuller f9fcd64
Initial pass at using SWR for posts API call
efuller 2f18495
Swap out useSWR hook with useSWRImmutable
efuller a165797
Cleanup comments
efuller de0f467
Update comments
efuller 0553a9c
Bump phpstan to 1024M
efuller ff9296d
Merge branch 'refs/heads/develop' into feature/issue-218/cache-initia…
efuller c314ce0
Install collision as a dev dep
efuller 32e1e4c
Bump version.
efuller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
interface PostsApiPathProps { | ||
search: string, | ||
offset: number, | ||
postType: string, | ||
status: 'publish', | ||
perPage: 20, | ||
orderBy: string, | ||
currentPostId: number | ||
} | ||
|
||
export default function buildPostsApiPath(pathProps: PostsApiPathProps) { | ||
return addQueryArgs('/wp-curate/v1/posts', { | ||
search: pathProps.search, | ||
offset: pathProps.offset, | ||
post_type: pathProps.postType, | ||
status: pathProps.status, | ||
per_page: pathProps.perPage, | ||
orderby: pathProps.orderBy, | ||
current_post_id: pathProps.currentPostId, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
type FetcherProps = [string | undefined, number]; | ||
type PostResponse = number[] | { id: number }; | ||
|
||
/** | ||
* Fetches the post data from the API. | ||
* @param url The API url. | ||
* @param currentPostId The current post ID. | ||
*/ | ||
const queryBlockPostFetcher = ( | ||
[url, currentPostId]: FetcherProps, | ||
) => apiFetch<PostResponse>({ path: url }) | ||
.then((response) => { | ||
let revisedResponse; | ||
// If the response is an array, filter out the current post. | ||
if (Array.isArray(response)) { | ||
revisedResponse = response.filter((item) => item !== currentPostId); | ||
} else if (response.id === currentPostId) { | ||
// Response is an object, if id is the current post, nullify it. | ||
revisedResponse = null; | ||
} else { | ||
revisedResponse = response; | ||
} | ||
return revisedResponse; | ||
}); | ||
|
||
export default queryBlockPostFetcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@efuller Does disabling automatic revalidation eliminate the need for workarounds like this one? https://l.alley.dev/ca8db44a8f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dlh01 I think it could.
useSWRImmutable
will simply just cache an API call and unless that request changes in any way, it will continue to use the cache.The use case I wanted to guard against was the revalidation after switching tabs. Here are some docs on revalidation and there are a ton of additional options as well.
I think it could be worth a try!