dongqiyou0303 2018-04-16 11:13
浏览 67
已采纳

如何让Workbox PWA与.php文件一起使用

I am new to PWA and using Workbox; So I have this test folder with the following file structure, using localhost as my server (i.e. localhost/test)


  1. index.html

  2. test.css

  3. test.jpg

  4. test.js

  5. sw.js (Code Shown below);

    importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');

    if (workbox) {
    console.log(`Yay! Workbox is loaded
  • 写回答

2条回答 默认 最新

  • dongqian5384 2018-04-18 18:22
    关注

    To elaborate to @pate's answer.

    Workbox by default tries to make sure that pretty URL's are supported out of the box.

    So in the first example, you cached /test/index.html. So when you request /test/, Workbox precaching actually checks the precache for:

    • /test/
    • /test/index.html

    If your page was /test/about.html, and you visited the page /test/about precache would append a .html and check for that.

    When you switched to the .php extension this logic would suddenly no longer work.

    There are a few options to get this working:

    1. If you are using any of the workbox tools to build your manifest, you are use the templatedUrls feature to map to a file (More details here):

      templatedUrls: {
          '/test-2/': ['/test-2/index.php']
      }
      
    2. If you are making the precache list yourself server side, you can just tell it to precache the URL /test-2/ without the index.php and precaching will simply cache that. Please note that you must ensure the revision changes with any changes to index.php.

    3. If you aren't making the precache manifest, you can use urlManipulaion option to tell precache to check for URL's, (More details here):

      workbox.precaching.precacheAndRoute(
        [
          .....
        ],
        {
          urlManipulaion: ({url}) => {
            // TODO: Check if the URL ends in slash or not
            // Add index.php based on this.
            return [url, `${url}.php`, `${url}index.php`];
          }
        }
      );
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路