Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable installation of the multi language Angular PWA with single service worker?

I have prepared applications with localized language versions. For this purpose I used the native Angular i18n module, which prepared two application packages with the appropriate language in each.

Each version of the application is available under the individual location on my nginx server:

  • myapp.com/en/...
  • myapp.com/pl/...

I would like my application to be installed once as PWA and provide both language versions using a (single?) service worker.

Unfortunately all I could do now with @angular/pwa module is to provide a separate service workers for each application, which forces separate installations of each language version of my application.

My question is: Is there any way for the application to work in the way I described using @angular/pwa or Workbox?

// By the way, it's a bit surprising that the creators of Angular didn't highlight the integration of @angular/localize with @angular/pwa.

like image 744
Jelly Avatar asked Apr 30 '20 21:04

Jelly


People also ask

What does the command ng add Angular PWA do?

To set up the Angular service worker in your project, use the CLI command ng add @angular/pwa . It takes care of configuring your application to use service workers by adding the @angular/service-worker package along with setting up the necessary support files.

Can Angular be used for PWA?

Adding a service worker to an Angular application is one of the steps for turning an application into a Progressive Web App (also known as a PWA). At its simplest, a service worker is a script that runs in the web browser and manages caching for an application. Service workers function as a network proxy.

How do you implement PWA in Angular 8?

Let us create a new application and convert it into PWA application. Run the web server and set our production build of the application as root folder. Open browser and enter http://localhost:8080. Now, go to Developer tools -> Network and select Offline option.

What is ng add angular PWA?

Progressive Web Application considers service workers as the primary technology. It allows deep platform integration, such as offline support, background sync, rich caching, and push notifications. The “ng add angular pwa” command generated the ngsw-config.json file, It is solely responsible for service workers.

How do I use service workers in angular?

It takes care of configuring your application to use service workers by adding the @angular/service-worker package along with setting up the necessary support files. Adds the @angular/service-worker package to your project. Enables service worker build support in the CLI. Imports and registers the service worker in the application module.

How to enable PWA with NGSW in production build?

The ngsw-worker.js file will be available in the production build. Finally, after you verify all the static files add ngsw-config.json. In this file used to add the static files like .js, .css, images, font files, and external URLs. Now, it is ready to check with PWA enabled application. The PWA has to work in the production build.

How do I add a service worker to an existing project?

Adding a service worker to your project link To set up the Angular service worker in your project, use the CLI command ng add @angular/pwa. It takes care of configuring your application to use service workers by adding the service-worker package along with setting up the necessary support files.


Video Answer


1 Answers

You have N apps for each language. They are hosted on different domains. They are actually different apps. Therefore even if you install all of them in one URL you get to install all of them.

As an option of using one domain with different apps you could either create a config with all langs and their corresponding JS files, or make a parser for index.html. Then, you need to create your own worker which will insert the right scripts on page load (you can't do it on go since JS code would be already loaded). This will load the appropriate app. And there you have your workers for each app included I guess.

You may want to consider switching your i18n tool if you are about publishing it in Google Store as a hybrid app.

As another option you could provide a layer for your WebView and load different URL depending on language user's chosen.

like image 80
Sergey Avatar answered Oct 22 '22 18:10

Sergey