【问题标题】:How to check if web app manifest exists via browser neutral methods?如何通过浏览器中性方法检查 Web 应用清单是否存在?
【发布时间】:2018-07-23 23:15:41
【问题描述】:

有没有办法检查一个有效的 manifest.json 是否存在并被用作应用程序清单,可能带有元标记或其他东西?我所处的场景需要逐个推出渐进式 Web 应用程序功能,而不是在整个应用程序中推出,我需要某种条件逻辑来测试 manifest.json 的存在,以防止以后出现错误行。

我曾玩弄过使用 GET 手动检查文件,但由于应用程序的特定和复杂结构,如果我能以某种方式使用不同的方法检查它是否存在,那将更加谨慎。如果可能,我想避免使用 jQuery。

谢谢!

【问题讨论】:

    标签: javascript html node.js reactjs progressive-web-apps


    【解决方案1】:

    你可以试试这样的:

    async function doesManifestExistForCurrentPage() {
      const manifestElement = document.querySelector('link[rel="manifest"]');
      if (!manifestElement) {
        return false;
      }
      const manifestUrl = manifestElement.getAttribute('href');
      if (!manifestElement) {
        return false;
      }
    
      // You could stop here and just return true.
      // If you want to actually see if the manifest file exists on the
      // server, use the following code:
      try {
        const manifestResponse = await fetch(manifestUrl);
        // .ok will be true if fetch() returned a HTTP 2xx response,
        // and false otherwise.
        return manifestResponse.ok;
      } catch (error) {
        // Or return true?
        // Depends on how you want to handle network failures.
        return false;
      }
    }
    

    【讨论】:

      【解决方案2】:
      function hasAppManifest() {
          return document.querySelector('link[rel="manifest"]') ? true : false;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-01
        • 2016-11-20
        • 1970-01-01
        • 1970-01-01
        • 2019-10-19
        • 2018-01-10
        • 1970-01-01
        • 2014-08-07
        相关资源
        最近更新 更多