视频作为背景图像无法在iOS上的Gatsby PWA中使用

时间:2019-05-23 19:59:35

标签: service-worker mp4 progressive-web-apps gatsby

我与Gatsby合作,为公司的潜在过渡创建了一个选择加入应用程序,现在对结果非常满意。我使它成为了渐进式Web应用程序,因为使用gatsby插件相当容易。

PWA在Android上运行良好,可以按预期显示背景视频,但在iOS上则不显示视频。

我将所有软件包和依赖项更新到最新版本,但这并没有改变。我尝试使用谷歌搜索问题,但尝试关闭该应用程序的人尝试让PWA在后台播放视频时遇到了很多搜索结果(不是我的情况)。

{
   resolve: `gatsby-plugin-manifest`,
      options: {
        name: `Afstuderen bij Arcady`,
        short_name: `Afstuderen`,
        start_url: `/`,
        background_color: `#FFF`,
        theme_color: `#00a667`,
        display: `standalone`,
        icon: `src/images/bear_green.png`,
      },
},
'gatsby-plugin-offline',

以及服务人员的内容

importScripts("workbox-v3.6.3/workbox-sw.js");
workbox.setConfig({modulePathPrefix: "workbox-v3.6.3"});

workbox.core.setCacheNameDetails({prefix: "gatsby-plugin-offline"});

workbox.skipWaiting();
workbox.clientsClaim();

/**
 * The workboxSW.precacheAndRoute() method efficiently caches and responds to
 * requests for URLs in the manifest.
 */
self.__precacheManifest = [
  {
    "url": "webpack-runtime-aec2408fe3a97f1352af.js"
  },
  {
    "url": "app-5b624d17337895ddf874.js"
  },
  {
    "url": "component---node-modules-gatsby-plugin-offline-app-shell-js-b97c345e19bb442c644f.js"
  },
  {
    "url": "offline-plugin-app-shell-fallback/index.html",
    "revision": "ac0d57f6ce61fac4bfa64e7e08d076c2"
  },
  {
    "url": "0-d2c3040ae352cda7b69f.js"
  },
  {
    "url": "component---src-pages-404-js-cf647f7c3110eab2f912.js"
  },
  {
    "url": "static/d/285/path---404-html-516-62a-0SUcWyAf8ecbYDsMhQkEfPzV8.json"
  },
  {
    "url": "static/d/604/path---offline-plugin-app-shell-fallback-a-30-c5a-BawJvyh36KKFwbrWPg4a4aYuc8.json"
  },
  {
    "url": "manifest.webmanifest",
    "revision": "5a580d53785b72eace989a49ea1e24f7"
  }
].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerRoute(/(\.js$|\.css$|static\/)/, workbox.strategies.cacheFirst(), 'GET');
workbox.routing.registerRoute(/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, workbox.strategies.staleWhileRevalidate(), 'GET');
workbox.routing.registerRoute(/^https?:\/\/fonts\.googleapis\.com\/css/, workbox.strategies.staleWhileRevalidate(), 'GET');

/* global importScripts, workbox, idbKeyval */

importScripts(`idb-keyval-iife.min.js`)
const WHITELIST_KEY = `custom-navigation-whitelist`

const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => {
  const { pathname } = new URL(event.request.url)

  return idbKeyval.get(WHITELIST_KEY).then((customWhitelist = []) => {
    // Respond with the offline shell if we match the custom whitelist
    if (customWhitelist.includes(pathname)) {
      const offlineShell = `/offline-plugin-app-shell-fallback/index.html`
      const cacheName = workbox.core.cacheNames.precache

      return caches.match(offlineShell, { cacheName }).then(cachedResponse => {
        if (cachedResponse) return cachedResponse

        console.error(
          `The offline shell (${offlineShell}) was not found ` +
            `while attempting to serve a response for ${pathname}`
        )

        return fetch(offlineShell).then(response => {
          if (response.ok) {
            return caches.open(cacheName).then(cache =>
              // Clone is needed because put() consumes the response body.
              cache.put(offlineShell, response.clone()).then(() => response)
            )
          } else {
            return fetch(event.request)
          }
        })
      })
    }

    return fetch(event.request)
  })
})

workbox.routing.registerRoute(navigationRoute)

let updatingWhitelist = null

function rawWhitelistPathnames(pathnames) {
  if (updatingWhitelist !== null) {
    // Prevent the whitelist from being updated twice at the same time
    return updatingWhitelist.then(() => rawWhitelistPathnames(pathnames))
  }

  updatingWhitelist = idbKeyval
    .get(WHITELIST_KEY)
    .then((customWhitelist = []) => {
      pathnames.forEach(pathname => {
        if (!customWhitelist.includes(pathname)) customWhitelist.push(pathname)
      })

      return idbKeyval.set(WHITELIST_KEY, customWhitelist)
    })
    .then(() => {
      updatingWhitelist = null
    })

  return updatingWhitelist
}

function rawResetWhitelist() {
  if (updatingWhitelist !== null) {
    return updatingWhitelist.then(() => rawResetWhitelist())
  }

  updatingWhitelist = idbKeyval.set(WHITELIST_KEY, []).then(() => {
    updatingWhitelist = null
  })

  return updatingWhitelist
}

const messageApi = {
  whitelistPathnames(event) {
    let { pathnames } = event.data

    pathnames = pathnames.map(({ pathname, includesPrefix }) => {
      if (!includesPrefix) {
        return `${pathname}`
      } else {
        return pathname
      }
    })

    event.waitUntil(rawWhitelistPathnames(pathnames))
  },

  resetWhitelist(event) {
    event.waitUntil(rawResetWhitelist())
  },
}

self.addEventListener(`message`, event => {
  const { gatsbyApi } = event.data
  if (gatsbyApi) messageApi[gatsbyApi](event)
})

我希望iOS PWA(野生动物园)能够像在Android上一样显示视频,但会显示灰屏。

我希望有人能帮助我或指出正确的方向。

2 个答案:

答案 0 :(得分:0)

您的视频有多大?

我上次检查时,iOS的PWA缓存限制为50MB,因此,如果您的视频大于50MB,则可能是它只能在Android上运行的原因(没有此类限制)。

答案 1 :(得分:0)

我发现这个blog post可以帮助我解决此问题

要将范围请求处理添加到gatsby-plugin-offline,我添加了一个名为range-request-handler.js的脚本,其内容如下:

// range-request-handler.js

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

// Bring in workbox libs
const { registerRoute } = require('workbox-routing');
const { CacheFirst } = require('workbox-strategies');
const { RangeRequestsPlugin } = require('workbox-range-requests'); // The fix

// Add Range Request support to fetching videos from cache
registerRoute(
    /(\.webm$|\.mp4$)/,
    new CacheFirst({
        plugins: [
            new RangeRequestsPlugin(),
        ],
    })
);

然后在我的gatsby-config.js中,我将插件配置为附加上述脚本:

// gatsby-config.js

module.exports = {
  // ...
  plugins: [
    // ...plugins
    {
      resolve: 'gatsby-plugin-offline',
      options: {
        appendScript: require.resolve('./range-request-handler.js'),
      },
    },
    // ...plugins
  ],
  // ...
};

视频现在可以在Safari浏览器中播放了。如果有更好的方法来实现这一点,我全神贯注。目前,它可以正常工作