如何将服务工作者与htaccess保护的目录结合使用? [英] How do I use service workers with an htaccess protected directory?

查看:38
本文介绍了如何将服务工作者与htaccess保护的目录结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些基础服务人员.第一次注册服务工作者时,服务工作者本身将正常工作.我总是遇到的问题是,该人以后(例如第二天)重新访问该网站并尝试访问受 .htaccess / .htpasswd 保护的目录.他们没有像通常那样显示对话框,而是直接显示401错误.

I'm trying some basic service workers. The service worker itself will work normally the first time the service worker is registered. The problem I always get is once the person revisits the website in the future (e.g. the following day) and tries to access a .htaccess/.htpasswd protected directory. Instead of getting the dialog box, as normal, they go straight to a 401 error.

这就是我在HTML的脚本标签中注册服务工作者的方式.

This is how I am registering the service worker in script tags in the HTML.

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('/sw.js').then(function(registration) {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(err) {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}

我在 sw.js 本身中尝试了几种不同的方法,每次遇到相同的错误时,我都会尝试.我相信这是来自Google airhorner示例的一个.

I have tried a couple of different methods in the sw.js itself and every time I get the same error. This is one from the Google airhorner example, I believe...

self.addEventListener('install', e => {
  const timeStamp = Date.now();
  e.waitUntil(
    caches.open('somename').then(cache => {
      return cache.addAll([
        `/`,
        `/index.html`,
        `/css/tour-2.css`
      ])
          .then(() => self.skipWaiting());
    })
  );
});

self.addEventListener('activate', event => {
      event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request, {ignoreSearch: true}).then(response => {
      return response || fetch(event.request);
    })
  );
});

有人知道是否可以将服务工作者用于受 .htaccess 保护的目录的网站?

Does anyone know if it is possible to use service workers with websites with .htaccess protected directories?

谢谢.

推荐答案

一种治愈方法是:

self.addEventListener('fetch', event => {
    // Exclude admin panel.
    if (0 === event.request.url.indexOf("https://www.my-site.com/my-protected-area")) {
        return;
    }

应该有帮助.

来源:TIV.NET

这篇关于如何将服务工作者与htaccess保护的目录结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆