Symfony在获取脚本时收到错误的HTTP响应代码(404)

时间:2019-05-11 14:32:55

标签: symfony progressive-web-apps

我有一个Symfony应用程序,正在其中创建一个PWA页面。

manifest.json可以正常工作,但是每当我尝试注册服务工作者时,都会收到错误消息。

这些是我在chrome控制台中不断遇到的错误:

A bad HTTP response code (404) was received when fetching the script.
Failed to load resource: net::ERR_INVALID_RESPONSE

我的页面中有JavaScript块:

{% block javascripts %}
    <script type='text/javascript'>  if ('serviceWorker' in navigator) {
    window.addEventListener('load', () => {
      navigator.serviceWorker.register('js/sw.js').then(registration => {
        console.log('SW registered: ', registration);
      }).catch(registrationError => {
        console.log('SW registration failed: ', registrationError);
      });
    });
  }</script>

{% endblock %}

这是我的sw.js文件:

var cacheName = 'hello-pwa';
var filesToCache = [
  '/css/style.css',
  '/js/script.js',
  'user/Index.html.twig',
];

/* Start the service worker and cache all of the app's content */
self.addEventListener('install', function(e) {
  e.waitUntil(
    caches.open(cacheName).then(function(cache) {
      return cache.addAll(filesToCache);
    })
  );
});

/* Serve cached content when offline */
self.addEventListener('fetch', function(e) {
  e.respondWith(
    caches.match(e.request).then(function(response) {
      return response || fetch(e.request);
    })
  );
});

这是我的文件树:


  |-- Project
        |-- public
               |-- js
                     |-- sw.js
                     |-- script.js
               |-- css
                     |-- style.css
               |-- manifest.json
        |-- template
               |-- user
                     |-- index.html.twig (we're here)


0 个答案:

没有答案