Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the project-loading hook to make localStorage cache usage dependent on the active i18n locale, aiming to prevent incorrect cached content from being loaded after CCP locale switching.
Changes:
- Adds a locale match check (
cachedProject?._cachedLocale === i18n.language) before loading a cached project. - Simplifies the cache eligibility logic into a single
shouldUseCachecondition.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
For my understanding, do you mean this disables the project caching when you change locale? Or is it more than that? |
Yes, I tried to change as little as possible from the existing code for the effect of disabling caching. |
There was a problem hiding this comment.
The GitHub diff looks odd for this file; basically, I commented out caching-related tests and added these two tests:
describe("When not embedded", () => {
...
// Tests for the temporarily disabled cache functionality
test("does not use cache even with loadCache true and matching cache in localStorage", async () => {
syncProject.mockImplementation(jest.fn((_) => loadProject));
localStorage.setItem("hello-world-project", JSON.stringify(cachedProject));
renderHook(
() =>
useProject({
projectIdentifier: "hello-world-project",
accessToken,
loadCache: true,
reactAppApiEndpoint,
}),
{ wrapper },
);
expect(setProject).not.toHaveBeenCalledWith(cachedProject);
expect(syncProject).toHaveBeenCalledWith("load");
});
test("does not use cache when cache locale does not match effectiveLocale and loads from server", async () => {
syncProject.mockImplementation(jest.fn((_) => loadProject));
const cachedWrongLocale = { ...cachedProject, locale: "en-GB" };
localStorage.setItem(
cachedWrongLocale.identifier,
JSON.stringify(cachedWrongLocale),
);
renderHook(
() =>
useProject({
projectIdentifier: cachedWrongLocale.identifier,
loadCache: true,
accessToken,
reactAppApiEndpoint,
}),
{ wrapper },
);
expect(setProject).not.toHaveBeenCalledWith(cachedWrongLocale);
expect(syncProject).toHaveBeenCalledWith("load");
await waitFor(() =>
expect(loadProject).toHaveBeenCalledWith({
identifier: cachedWrongLocale.identifier,
locale: "ja-JP",
accessToken,
reactAppApiEndpoint,
}),
);
});
Changes
Locale switching fix
useProjecthookuseProject.testand added couple of new tests.Initial load problem fix
reactProps()ofweb-component.jsattributeChangedCallbackhasn't been run yet