wxt-dev / wxt

⚡ Next-gen Web Extension Framework
https://wxt.dev
MIT License
4.55k stars 190 forks source link

Reload when localization changes #735

Closed aklinker1 closed 5 months ago

aklinker1 commented 5 months ago

Feature Request

When saving a change to any public/_locales/*/messages.json, the extension should either do a full reload, or it should support HMR somehow. It also needs to support any file pattern, so this also supports the changes in https://github.com/wxt-dev/wxt/pull/343.

A couple of ways to do this:

  1. During dev, add a proxy around the browser.i18n API. It waits for messages from the dev server, caches the latest translations, and pulls the latest localization from the web.
    • Couple of problems with this approach: won't automatically update text on page, but the next time you get the localization, it will be updated. That means you'd have to reload the HTML pages or content scripts, or re-trigger logic in the background. The other problem, is how do I know which locale to pull text from. Use @@ui_locale
  2. Use Vite's HMR API
    • I don't really know how to do this. I would have to create a virtual module that imports all the localization files. It would automatically detect file changes because it would be imported... somewhere? Then I'd have to use the import.meta.hot API on the client side to setup changes? Once again, no idea how this would work.

I'm gonna go with option 1, using the web-socket to notify when code changes and cache the latest values client side. For UI's, we can add framework-specific utils around automatically re-rendering when the text changes. I don't think there's a single-solution-fits-all available here. Until those custom utils are implemented, which likely won't be in the first PR, you'll just have to refresh the HTML page.

Is your feature request related to a bug?

N/A

What are the alternatives?

Stopping the dev command and restarting it.

Additional context

aklinker1 commented 5 months ago

For the purpose of this issue, I'm going to just do a basic reload. There's lots of complications around this if I were to try and do a hot reload, so for now, I'm skipping that.

I also noticed that the extension doesn't reload properly when changing public files, localization included. So I'll make a small fix to fix both these issues together.

aklinker1 commented 4 months ago

Basic reloading added in v0.18.8. I'm working on a wrapper around the i18n APIs to make it easier, and plan on adding HMR to it: https://github.com/wxt-dev/wxt/pull/758

If you have any feedback around the API, let me know!