Skip to content

Only retrieve cached projects for the current locale#1342

Merged
cocomarine merged 15 commits intomainfrom
hj/testing-locale-switch-3
Mar 5, 2026
Merged

Only retrieve cached projects for the current locale#1342
cocomarine merged 15 commits intomainfrom
hj/testing-locale-switch-3

Conversation

@cocomarine
Copy link
Contributor

@cocomarine cocomarine commented Feb 25, 2026

Changes

Locale switching fix

Initial load problem fix

  • Before: when an editor page with locales other than en was first loaded in CCP, it rendered en page rather than the locale page (editor-standalone was fine)
  • Updated to make sure initial attributes like locale are passed to React by reading them from the DOM in reactProps() of web-component.js
    • This is so that the first render uses the host's value even when attributeChangedCallback hasn't been run yet

Copilot AI review requested due to automatic review settings February 25, 2026 14:51
@cocomarine cocomarine temporarily deployed to previews/1342/merge February 25, 2026 14:52 — with GitHub Actions Inactive
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 shouldUseCache condition.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cocomarine cocomarine temporarily deployed to previews/1342/merge February 25, 2026 15:21 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge February 25, 2026 15:43 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge February 26, 2026 08:10 — with GitHub Actions Inactive
@zetter-rpf
Copy link
Contributor

This is a temporary fix as it effectively disabled caching.

For my understanding, do you mean this disables the project caching when you change locale? Or is it more than that?

Copy link
Contributor

@zetter-rpf zetter-rpf left a comment

Choose a reason for hiding this comment

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

Thanks for the fix, the approach looks good. Let me know when you have fixed the tests and I can re-review.

It would be good to have at least one new test for the updated behaviour of useProject

@cocomarine cocomarine temporarily deployed to previews/1342/merge February 26, 2026 16:43 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge March 2, 2026 08:31 — with GitHub Actions Inactive
@cocomarine
Copy link
Contributor Author

This is a temporary fix as it effectively disabled caching.

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.

@cocomarine cocomarine temporarily deployed to previews/1342/merge March 2, 2026 08:54 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge March 2, 2026 15:31 — with GitHub Actions Inactive
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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,
      }),
    );
  });

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, Chris.
Instead of commenting out, I skipped those tests that are no longer passing.

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

@cocomarine cocomarine temporarily deployed to previews/1342/merge March 3, 2026 08:41 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge March 3, 2026 10:00 — with GitHub Actions Inactive
@cocomarine cocomarine temporarily deployed to previews/1342/merge March 3, 2026 12:07 — with GitHub Actions Inactive
@zetter-rpf
Copy link
Contributor

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.

zetter-rpf added a commit to RaspberryPiFoundation/editor-api that referenced this pull request Mar 4, 2026
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
@zetter-rpf zetter-rpf changed the title Update to fix CCP locale switching Only retrieve cached projects for the current locale Mar 4, 2026
@zetter-rpf
Copy link
Contributor

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.

I spent some time understanding how caching and locales work in the editor. I found:

  • When projects are saved the cache isn't written to. This means this will only affect unsaved projects (which is more likely when logged out).
  • Some of the blank starter projects set up in development only exist for the 'en' locale, but in production they have variants for our supported locales.
  • Most projects will have a locale set as we default it to 'en', but not all projects are localised versions available.

The consequences of this change is that:

  • If a user changes their locale on an unsaved project they will lose their work
  • if a user has a locale and the project has no version in that locale, caching won't work and they will lose their work if they refresh/close the page.

This will affect project site more than editor, so fine to approve once the tests are updated

@cocomarine cocomarine temporarily deployed to previews/1342/merge March 4, 2026 15:05 — with GitHub Actions Inactive
Copy link
Contributor

@zetter-rpf zetter-rpf left a comment

Choose a reason for hiding this comment

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

Thanks for updating the tests!

@cocomarine cocomarine merged commit 34d5be2 into main Mar 5, 2026
7 checks passed
@cocomarine cocomarine deleted the hj/testing-locale-switch-3 branch March 5, 2026 08:53
@cocomarine cocomarine mentioned this pull request Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants