Only retrieve cached projects for the current locale#1342
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,
}),
);
});There was a problem hiding this comment.
Thanks for looking at the the tests!
Commented out code like this can be confusing- it's often not clear why they are commented and what someone should do when they come back to it.
Also some of them look like they are also testing cases that are no longer covered? (it's harder to see because of the diff).
Could the old tests be adapted to work with locales so they pass? And if there are tests that are now redundant or covered by your new cases, could we just remove them? They will be in the git history if we want to change back to this behaviour later.
Let me know if it would be helpful to look at this together today or this week - I do I find testing hooks can be complicated so happy to help.
There was a problem hiding this comment.
Thanks for the feedback, Chris.
Instead of commenting out, I skipped those tests that are no longer passing.
There was a problem hiding this comment.
Me and @cocomarine talked through this - I think it's skipped tests can be just like commented out tests and be less to future people looking at the code as they don't explain why they are commented or when they will be fixed
It would be better to use the tests to document and test the current behaviour of the system, which might mean reviewing each test to update or delete it.
…undation/editor-ui into hj/testing-locale-switch-3
|
I've talked this through with @cocomarine - I'd like another day to just understand the consequences for code editor and if we'll need to do additional work to support this. I'll try to get back to you tomorrow. |
There's currently a caching issue when switching between locales on the project site [1]. To fix this we can only load the project if it has the expected locale. However, this would break caching for projects that aren't localized. The code editor only uses the project cache when you've not yet logged in or saved a project. The projects you're likely to be editing in this case are the blank starter projects. Since these projects are blank with no instructions they aren't really localizable so I have decided to flag them as having a 'global' localization. Adding this to the list of locales will mean we can also set this on any imported projects we want this behaviour for. I'll amend [1] to allow using the cache when when the project is set to global. ### After deploy Deploy separate migration to update locale for the two blank projects. [1] RaspberryPiFoundation/editor-ui#1342
I spent some time understanding how caching and locales work in the editor. I found:
The consequences of this change is that:
This will affect project site more than editor, so fine to approve once the tests are updated |
…undation/editor-ui into hj/testing-locale-switch-3
zetter-rpf
left a comment
There was a problem hiding this comment.
Thanks for updating the tests!
Changes
Locale switching fix
useProjecthookuseProject.testInitial load problem fix
reactProps()ofweb-component.jsattributeChangedCallbackhasn't been run yet