Service Worker preparation method uses comments to replace code

Created on 13 September 2023, over 1 year ago

Problem/Motivation

Inside the PWAController's "serviceWorkerRegistration()" method, the serviceworker.js gets populated with drupal data. Some of the logic on how the js gets populated is a bit flawed. Especially line 275, where data gets injected based on comments in code:

    $replace = [
      '[/*cacheUrls*/]' => Json::encode($cacheWhitelist),
      '[/*activeLanguages*/]' => Json::encode(array_keys($languages)),
      '[/*exclude_cache_url*/]' => Json::encode($exclude_cache_url),
      "'/offline'/*offlinePage*/" => "'" . $config->get('offline_page') . "'",
      '[/*modulePath*/]' => '/' . $this->moduleHandler->getModule('pwa')->getPath(),
      '1/*cacheVersion*/' => '\'' . $this->getCacheVersion() . '\'',
      'false/*pwaSkipWaiting*/' => $skip_waiting,
    ];

And one example from the javascript, where the data gets injected:

// Active languages on the site.
const ACTIVE_LANGUAGES = [/*activeLanguages*/];

This feels off. Shouldn't this be possible through simply adding these variables through the drupal settings js API? Couldn't we simply inject this data through the "pwa_page_attachments()" method, similiar to how the "scope" is passed to the "serviceworker-load.js"? Or is this NOT possible because of basically copying the "serviceworker.js" or other general serviceworker cache flaws?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024