Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/rude-cows-shake.md
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
3 changes: 2 additions & 1 deletion packages/repack/src/modules/ScriptManager/ScriptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ export class ScriptManager extends EventEmitter {
const fetch = await locator.shouldUpdateScript(
scriptId,
caller,
script.shouldUpdateCache(this.cache[cacheKey])
script.shouldUpdateCache(this.cache[cacheKey]),
this.cache[cacheKey]
Comment on lines +508 to +509
Copy link
Copy Markdown
Contributor

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 into shouldUpdateScript implies 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 mutates cachedData. so, I'd prefer to pass it in more safer way:

Suggested change
script.shouldUpdateCache(this.cache[cacheKey]),
this.cache[cacheKey]
script.shouldUpdateCache(this.cache[cacheKey]),
this.cache[cacheKey] ? { ...this.cache[cacheKey] } : undefined

);

// If it returns true, we need to fetch the script
Expand Down
8 changes: 7 additions & 1 deletion packages/repack/src/modules/ScriptManager/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { NormalizedScriptLocator } from './NativeScriptManager.js';

/**
* Interface specifying how to fetch a script.
* It represents the output of {@link ScriptLocatorResolver} function used by {@link ScriptManager}.
Expand Down Expand Up @@ -132,7 +134,11 @@ export interface ScriptLocator {
shouldUpdateScript?: (
scriptId?: string,
caller?: string,
isScriptCacheOutdated?: boolean
isScriptCacheOutdated?: boolean,
cachedData?: Pick<
NormalizedScriptLocator,
'method' | 'url' | 'query' | 'headers' | 'body'
>
) => Promise<boolean> | boolean;
}

Expand Down
37 changes: 37 additions & 0 deletions website/src/latest/api/runtime/script-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code assumes cachedData always exists, but it might be undefined on first load. so let's guard no-cache case before dereferencing cachedData

Suggested change
// 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
);
};
// Custom logic to determine if the script should be updated
if (!cachedData) return true;
return (
cachedData.method !== locator.method ||
cachedData.url !== locator.url ||
cachedData.query !== locator.query ||
!shallowEqual(cachedData.headers, locator.headers) ||
cachedData.body !== locator.body
);
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I’d also suggest adding a unit test covering for these cases:

  • cachedData === undefined on first load
  • cachedData populated on subsequent load
  • changed locator making isScriptCacheOutdated === true

return args;
});
```

### Override script locator URL after resolution

```js
Expand Down