-
Notifications
You must be signed in to change notification settings - Fork 155
feat: expose cache entry on shouldUpdateScript #1319
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": minor | ||
| --- | ||
|
|
||
| Expose cached data on shouldUpdateScript in order to enable more custom cache resolution |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -256,6 +256,43 @@ ScriptManager.shared.hooks.afterResolve(async (args) => { | |||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ### Override shouldUpdateScript logic | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||
| ScriptManager.shared.hooks.afterResolve(async (args) => { | ||||||||||||||||||||||||||||||||||||||||
| const { locator } = args; | ||||||||||||||||||||||||||||||||||||||||
| locator.shouldUpdateScript = async (scriptId, caller, isScriptCacheOutdated, cachedData) => { | ||||||||||||||||||||||||||||||||||||||||
| // Custom logic to determine if the script should be updated | ||||||||||||||||||||||||||||||||||||||||
| if (isScriptCacheOutdated) { | ||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
| return args; | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| `shouldUpdateScript` also exposes the cached script locator data for more advanced comparison: | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| > **Note:** The `shallowEqual` function is a utility for comparing two objects' keys and values. You can use your own implementation or import one from a utility library. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||
| ScriptManager.shared.hooks.afterResolve(async (args) => { | ||||||||||||||||||||||||||||||||||||||||
| const { locator } = args; | ||||||||||||||||||||||||||||||||||||||||
| locator.shouldUpdateScript = async (scriptId, caller, isScriptCacheOutdated, cachedData) => { | ||||||||||||||||||||||||||||||||||||||||
| // Custom logic to determine if the script should be updated | ||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||
| cachedData.method !== locator.method || | ||||||||||||||||||||||||||||||||||||||||
| cachedData.url !== locator.url || | ||||||||||||||||||||||||||||||||||||||||
| cachedData.query !== locator.query || | ||||||||||||||||||||||||||||||||||||||||
| !shallowEqual(cachedData.headers, locator.headers) || | ||||||||||||||||||||||||||||||||||||||||
| cachedData.body !== locator.body | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+283
to
+291
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code assumes
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d also suggest adding a unit test covering for these cases:
|
||||||||||||||||||||||||||||||||||||||||
| return args; | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ### Override script locator URL after resolution | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
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.
passing
this.cache[cacheKey]directly intoshouldUpdateScriptimplies that users receive the actual cached object, not a defensive copy. which means that Re.Pack’s internal cache entry might be affected If user's code mutatescachedData. so, I'd prefer to pass it in more safer way: