使用vue ios bfcache失效

ios框架wkwebview
H5使用vue作为框架
现象:
一个聊天框,跳其他页面后再反回去内容被刷了
尝试打印pageshow-》e.persisted显示false。说明bfcahe失效

在看问题之前先了解下MessageChannel

参考1
参考2

然后看下webkit相关源码
首先是PageCache文件描述了页面缓存相关的信息

static bool canCacheFrame(Frame& frame, DiagnosticLoggingClient& diagnosticLoggingClient, unsigned indentLevel)
{
.....
 Vector<ActiveDOMObject*> unsuspendableObjects;
    if (frame.document() && !frame.document()->canSuspendActiveDOMObjectsForDocumentSuspension(&unsuspendableObjects)) {
        PCLOG("   -The document cannot suspend its active DOM Objects");
        for (auto* activeDOMObject : unsuspendableObjects) {
            PCLOG("    - Unsuspendable: ", activeDOMObject->activeDOMObjectName());
            diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::unsuspendableDOMObjectKey(), activeDOMObject->activeDOMObjectName(), ShouldSample::Yes);
            UNUSED_PARAM(activeDOMObject);
        }
        logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::cannotSuspendActiveDOMObjectsKey());
        isCacheable = false;
    }
.......
}
//canSuspendActiveDOMObjectsForDocumentSuspension()主要处理是否可缓存信息

然后看下对应ScriptExecutionContext文件

有个canSuspendForDocumentSuspension方法

bool ScriptExecutionContext::canSuspendActiveDOMObjectsForDocumentSuspension(Vector<ActiveDOMObject*>* unsuspendableObjects)
{
    checkConsistency();

    bool canSuspend = true;

    forEachActiveDOMObject([&](auto& activeDOMObject) {
        if (!activeDOMObject.canSuspendForDocumentSuspension()) {
            canSuspend = false;
            if (unsuspendableObjects)
                unsuspendableObjects->append(&activeDOMObject);
            else
                return ShouldContinue::No;
        }
        return ShouldContinue::Yes;
    });

    if (unsuspendableObjects) {
        // Remove activeDOMObjects that have been destroyed while we were iterating above.
        unsuspendableObjects->removeAllMatching([&](auto* activeDOMObject) {
            return !m_activeDOMObjects.contains(activeDOMObject);
        });
    }

    return canSuspend;
}

那有哪些activeDOMObject对象呢,在webcore下搜了下 53个
可以看下worker

bool Worker::canSuspendForDocumentSuspension() const
{
    // FIXME: It is not currently possible to suspend a worker, so pages with workers can not go into page cache.
    return false;
}
//canSuspendForDocumentSuspension 直接返回false 用了 new Worker也不会生效了
//
//idb操作的
bool IDBObjectStore::canSuspendForDocumentSuspension() const
{
    return false;
}
//indexedDB.open('test')
//eventsource操作的
bool EventSource::canSuspendForDocumentSuspension() const
{
    // FIXME: We should return true here when we can because this object is not actually currently active.
    return false;
}
//new EventSource

再看(MessagePort文件)[https://github.com/WebKit/webkit/blob/master/Source/WebCore/dom/MessagePort.cpp]

bool MessagePort::canSuspendForDocumentSuspension() const
{
    return !hasPendingActivity() || (!m_started || m_closed);
}
//canSuspendForDocumentSuspension拿到false(由于条件限制无法开调试看具体原因)

实际现象是使用

var nmc=    new MessageChannel();
//必须有下面这句
nmc.port1.onmessage =function(){}
//vue2.6之前版本用了MessageChannel  
//解决办法
//1. MessageChannel=null
//2.ios多开webview
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Mobile Web Favorites 参与贡献 移动前端开发收藏夹,欢迎使用Issues以及 Pull Req...
    柴东啊阅读 4,223评论 0 2
  • 我的笔记 关于Chromium 源文章地址 源代码语言 结构 Chromium分为三个主要部分(不包括其他库):浏...
    zhchhhemmm阅读 1,072评论 0 1
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,047评论 1 45
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 8,864评论 1 11
  • 提问 sw-precache是什么?workbox又是什么? web前端的各位同学可能或多或少听过pwa,听过se...
    brandonxiang阅读 15,542评论 0 5