阻止 PWA 服务工作线程从 WordPress 中的不同 JavaScript 文件下载。

huangapple go评论71阅读模式
英文:

Prevent PWA service worker to be downloaded, from a different javascript file in wordpress

问题

我猜很多人会认为提出对PWA进行下载限制可能不是一个好的战略思路,但在这个具体项目中,我需要确保用户在下载PWA之前已经注册。
我认为正确的方法是在检测到用户已注册后,自动阻止 'service worker' 的下载,并允许其下载。
我需要从处理它的不同文件中阻止 service worker 注册过程。虽然我在使用WordPress和SuperPWA插件,但我不想改变插件内部的代码。
我想我的目标是以某种方式阻止这个函数:

navigator.serviceWorker.register(superpwa_sw.url)

由于WordPress的规范,似乎PWA插件的 .js 文件在我希望编写阻止函数的文件之前加载。

任何能指引我正确方向的线索都将不胜感激,谢谢!

英文:

I guess that many people will consider that proposing restrictions for a PWA to be downloaded could not be a good strategic idea but in this concrete project I would need to been able to accomplish exactly that.
I would like to be sure that the user has registered before downloading the PWA and I think the way to go would be to automatically prevent 'service worker' to be downloaded and, when detecting user has been registered, allow it to be downloaded.
I would need to block the service worker registering process form a different file that the one that handles it. While I'm using wordpress and SuperPWA plugin I don't want to change code inside the plugin
I supose my goal would be to block somehow this function

navigator.serviceWorker.register(superpwa_sw.url)

Due to the wordpress specifications it seems that the .js files of the PWA plugin is loaded before the file from where I would like to write the blocking function.

Any clue that could point me in the right direction would be very welcome, thanks!

答案1

得分: 1

使用[wp_dequeue_script()][1]函数,您可以在php中阻止PWA的发送。

```php
function no_pwa_unless_logged_in() {
        if ( ! is_user_logged_in() ) {
             wp_dequeue_script( 'superpwa-register-sw' );
        }
}
add_action( 'wp_print_scripts', 'no_pwa_unless_logged_in' );

wp_print_scripts action在将入队的脚本发送到浏览器页面之前触发。此代码从队列中移除了PWA脚本。

这是一个不错的方法:它不会允许PWA脚本传送到未登录的访问者,因此这些访问者将无法访问它。


<details>
<summary>英文:</summary>

You can probably prevent the sendout of the PWA in php, in a little bit of code, with [wp_dequeue_script()][1].

```php
function no_pwa_unless_logged_in() {
        if ( ! is_user_logged_in() ) {
             wp_dequeue_script( &#39;superpwa-register-sw&#39; );
        }
}
add_action( &#39;wp_print_scripts&#39;, &#39;no_pwa_unless_logged_in&#39; );

The wp_print_scripts action fires just before sending enqueued scripts to the browser page. This code removes the PWA script from the queue.

This is a good approach: it won't allow your PWA script to go to any not-logged-in visitor, so those visitors won't have any access to it.

答案2

得分: 0

我找到了一个似乎有效的解决方案。

从另一个.js文件取消注册服务工作者是可能的,一旦重新注册它,你就可以获取注册函数的结果,并在之后用于取消注册。
然后,当我检查用户是否已注册并且PWA可以根据它下载时,我可以再次注册它。

function unregServiceWorker(){
    if(navigator.serviceWorker){
        if ("serviceWorker" in navigator) {
          navigator.serviceWorker
            .register(superpwa_sw.url)
            .then((registration) => {
              // registration worked
              console.log("Registration succeeded.");
              registration.unregister().then((boolean) => {
              });
            })
            .catch((error) => {
              console.error(`Registration failed with ${error}`);
            });
        }
    }
}
英文:

I found a solution that seems to work.

Unregistering the service worker from another .js file is possible once you re-register it. This way you can get the result of the registering function and use it onwards for unregistering it.
Then I can register it again when I check the user is registered and the pwa turns downloable according to it.

function unregServiceWorker(){
    if(navigator.serviceWorker){
        if (&quot;serviceWorker&quot; in navigator) {
          navigator.serviceWorker
            .register(superpwa_sw.url)
            .then((registration) =&gt; {
              // registration worked
              console.log(&quot;Registration succeeded.&quot;);
              registration.unregister().then((boolean) =&gt; {
              });
            })
            .catch((error) =&gt; {
              console.error(`Registration failed with ${error}`);
            });
        }
    }
}

答案3

得分: 0

I also tried the solution of @O.jones and it works perfectly. As he says in his comment, this solution seems to be more secure. I changed the function a bit, now it doesn't only de-register it, but it also registers the script when needed, so I call the function in the header.php and again once detected the user is logged in.

function no_pwa_unless_logged_in($user_logged) {
    if ( ! $user_logged ) { //este bool debe ser llamado o cuando de se logea o cuando se da de alta
        wp_dequeue_script( 'superpwa-register-sw' );
    } else {
        wp_enqueue_script( 'superpwa-register-sw' );
    }
}
add_action( 'wp_print_scripts', 'no_pwa_unless_logged_in' );
英文:

I also tried the solution of @O.jones and it works perfectly. As he says in his comment, this solution seems to be more secure. I changed the function a bit, now it doesn't only de-register it, but it also registers the script when needed, so I call the function in the header.php and again once detected the user is logged in.

function no_pwa_unless_logged_in($user_logged) {
        if ( ! $user_logged ) { //este bool debe ser llamado o cuando de se logea o cuando se da de alta
          wp_dequeue_script( &#39;superpwa-register-sw&#39; );
        }else{
          wp_enqueue_script( &#39;superpwa-register-sw&#39; );
        }
}
add_action( &#39;wp_print_scripts&#39;, &#39;no_pwa_unless_logged_in&#39; );

huangapple
  • 本文由 发表于 2023年6月12日 17:02:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定