-
Notifications
You must be signed in to change notification settings - Fork 62
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
feat: retry invalid/expired refs #356
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c062cb6
feat: retry invalid/expired refs
angeloashmore fc131d1
fix: remove duplicate ref retry logic and support `getFirst`
angeloashmore d10ebe6
test: invalid ref retry
angeloashmore 345ff2b
feat: allow up to 3 retries before throwing
angeloashmore ef49bdc
feat: use a new master ref once a known-stale ref is used
angeloashmore 1499cd1
test: simplify test title
angeloashmore f3ae551
docs: update const description
angeloashmore 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { expect, it, vi } from "vitest" | ||
|
||
import { rest } from "msw" | ||
|
||
import { createTestClient } from "./createClient" | ||
import { getMasterRef } from "./getMasterRef" | ||
import { mockPrismicRestAPIV2 } from "./mockPrismicRestAPIV2" | ||
|
||
import * as prismic from "../../src" | ||
|
||
type TestInvalidRefRetryArgs = { | ||
run: ( | ||
client: prismic.Client, | ||
params?: Parameters<prismic.Client["get"]>[0], | ||
) => Promise<unknown> | ||
} | ||
|
||
export const testInvalidRefRetry = ( | ||
description: string, | ||
args: TestInvalidRefRetryArgs, | ||
): void => { | ||
it.concurrent(description, async (ctx) => { | ||
const client = createTestClient({ ctx }) | ||
|
||
const triedRefs: string[] = [] | ||
|
||
const repositoryResponse = ctx.mock.api.repository() | ||
repositoryResponse.refs = [ctx.mock.api.ref({ isMasterRef: true })] | ||
|
||
const latestRef = ctx.mock.api.ref().ref | ||
|
||
mockPrismicRestAPIV2({ ctx, repositoryResponse }) | ||
|
||
const queryEndpoint = new URL( | ||
"documents/search", | ||
`${client.documentAPIEndpoint}/`, | ||
).toString() | ||
|
||
ctx.server.use( | ||
rest.get(queryEndpoint, (req, res, ctx) => { | ||
const ref = req.url.searchParams.get("ref") | ||
if (ref) { | ||
triedRefs.push(ref) | ||
} | ||
|
||
if (triedRefs.length <= 1) { | ||
return res( | ||
ctx.status(404), | ||
ctx.json({ | ||
type: "api_notfound_error", | ||
message: `Ref not found. Ensure you have the correct ref and try again. Master ref is: ${latestRef}`, | ||
}), | ||
) | ||
} | ||
}), | ||
) | ||
|
||
const consoleWarnSpy = vi | ||
.spyOn(console, "warn") | ||
.mockImplementation(() => void 0) | ||
|
||
await args.run(client) | ||
|
||
expect(triedRefs).toStrictEqual([ | ||
getMasterRef(repositoryResponse), | ||
latestRef, | ||
]) | ||
|
||
// Check that refs are not retried more than once. | ||
ctx.server.use( | ||
rest.get(queryEndpoint, (_req, res, ctx) => { | ||
return res( | ||
ctx.status(404), | ||
ctx.json({ | ||
type: "api_notfound_error", | ||
message: `Ref not found. Ensure you have the correct ref and try again. Master ref is: ${triedRefs[0]}`, | ||
}), | ||
) | ||
}), | ||
) | ||
|
||
await expect(async () => { | ||
await args.run(client) | ||
}).rejects.toThrow(prismic.RefNotFoundError) | ||
|
||
consoleWarnSpy.mockRestore() | ||
}) | ||
} |
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.
...or when multiple requests (e.g.
Promise.all(...)
) fail at the same time, which I believe is symptomatic of this kind of error(?)Maybe we should just retry directly instead of recursively calling the function after that?
Or base the retry off whether or not we had an explicit
ref
parameter(?)💡 #idea: Maybe we should clear the repository cache when this happens so that other requests (during the cache's 5s lifespan) can fetch a fresh ref?
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.
You're right; it would likely cause some issues when queries are deduplicated. I removed the
retriedRefs
array and check and simply trust that the provided refs won't cause a loop.Here's the comment I sent to you in Slack:
I'd rather not retry directly since it means we can only follow the API's response once. If a website has a very stale cache with multiple chained invalid ref responses, we should be able to use each of them.
We log a warning each time we retry, so devs will still be nudged to fix their caching issue.
A retry may be necessary whether or not an explicit
ref
is provided. Websites may have a stale/api/v2
, which would result in a stale master ref being used implicitly.Maybe! This fix is really intended for Next.js' indefinite cache, which works outside the client's internal 5s cache.
I can't think of any downside to clearing the internal cache. It would resolve the tiny chance that a burst of requests from the same client results uses a stale, internally cached ref. If you can't think of any downsides, I'll add it in.
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.
Thanks for the clarification!