Angular Service Worker:脚本资源位于重定向之后,不允许这样做

时间:2019-05-27 06:24:07

标签: angular service-worker angular-service-worker

我的网站曾经在https://jobs.af上运行,四个月后,我们附加了www-> https://www.jobs.af

我们没有想到的是服务人员在重定向方面表现不佳

我们没有注意的是 服务人员 重定向 / p>

用户将转到https://www.jobs.af,NGINX将向他们发送请求的每种资源发送 301重定向

这对于新用户来说效果很好,但是已经访问过该网站的用户会遇到一些问题。

如果有新版本可用,服务人员会自动更新自己。在我们的案例中,它尝试在https://www.jobs.af/ngsw-worker.js上获取新版本,该新版本重定向到https://jobs.af/ngsw-worker.js。因此,服务工作者将尝试从重定向后面的新服务工作者进行更新,这会导致以下错误:

enter image description here

我的代码:

app.browser.module.ts

@NgModule({
  imports: [
    BrowserModule.withServerTransition({ appId: "app-root" }),
     .
     .
     .
    ServiceWorkerModule.register("./ngsw-worker.js", {
      enabled: environment.production
    })
  ],
  bootstrap: [AppComponent]
})
export class AppBrowserModule {}

main.ts

document.addEventListener("DOMContentLoaded", () => {
  platformBrowserDynamic()
    .bootstrapModule(AppBrowserModule)
    .then(() => {
      if ("serviceWorker" in navigator && environment.production) {
        navigator.serviceWorker.register("./ngsw-worker.js");
      }
    })
    .catch(err => console.log(err, "error"));
});

manifest.json

{
...
  "scope": "/",
  "start_url": "/"
}

default.conf

upstream website {
   ip_hash;
   server backend1:3000;
   server backend2:3000;
   server backend3:3000;
   server backend4:3000;
}

server {
    listen 80;
    server_name www.jobs.af jobs.af;
    return 301 https://$server_name$request_uri;
}

server {
    #Listen for HTTPS connections using http2;
    listen 443 ssl http2;

    server_name jobs.af www.jobs.af;

        if ($host = "jobs.af") {
           return 301 https://www.jobs.af$request_uri;
       }
    ssl_certificate      ****.pem;
    ssl_certificate_key  ****.key;
    keepalive_timeout    70;

    # Cache SSL handshakes
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 5m;

    sl_ecdh_curve prime256v1;

    # SSL Protocols
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # Use gzip to save on bandwith 
    server_tokens off;  
    gzip on;
    gzip_comp_level    7;
    gzip_min_length    256;
    gzip_proxied       any;
    gzip_vary          on;
    client_max_body_size 10M;
    gzip_types
          application/atom+xml
          application/javascript
          application/json
          application/ld+json
          application/manifest+json
          application/rss+xml
          application/vnd.geo+json
          application/vnd.ms-fontobject
          application/x-font-ttf
          application/x-web-app-manifest+json
          application/xhtml+xml
          application/xml
          font/opentype
          image/bmp
          image/svg+xml
          image/x-icon
          text/cache-manifest
          text/css
          text/plain
          text/vcard
      text/vnd.rim.location.xloc
          text/vtt
          text/x-component
          text/x-cross-domain-policy;

    location / {
           rewrite ^/(.*) /$1 break;
            proxy_pass http://website;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
            proxy_redirect     off;
    }

    location /socket.io {
                proxy_pass http://website/socket.io;
                proxy_http_version 1.1;
                proxy_buffering off;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
       }    
}

0 个答案:

没有答案