google-chrome - Chrome 开发工具 : How to simulate offline for a particular domain than the entire network?

标签 google-chrome firebase google-chrome-devtools google-cloud-firestore

用例? Firebase有新产品 Firestore , 启用 offlinePersistence根据他们的文档。

https://firebase.google.com/docs/firestore/manage-data/enable-offline?authuser=0#listen_to_offline_data

我想测试一种情况,应用程序加载,但没有与 firebase 建立连接(想想具有 serviceworker 缓存静态 Assets 的 Progressive Web App),但没有连接到 Firebase 的网络。

我的代码看起来像

import React from "react";
import {fdb} from "../mainPage/constants";

// includeDocumentMetadataChanges to know when backend has written the local changes
// includeQueryMetadataChanges to know if changes come from cache using 'fromCache' property
// https://firebase.google.com/docs/firestore/manage-data/enable-offline?authuser=0#listen_to_offline_data
const queryOptions = {
    includeDocumentMetadataChanges: true,
    includeQueryMetadataChanges: true
};

const collectionName = "todos";
export default class ToDos extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            items: [],
            textBox: "",
            loading: true
        }
    }

    componentWillMount() {
        let unsubscribe = fdb.collection(collectionName)

            .onSnapshot(queryOptions, function (querySnapshot) {
                let items = [];
                querySnapshot.forEach(function (doc) {
                    items.push(doc);
                    //console.log(" data: ", doc && doc.data());
                });
                this.setState({"items": items});
            }.bind(this));
        this.setState({"unsubscribe": unsubscribe});
    }

    componentWillUnmount() {
        this.state.unsubscribe();
    }

    handleTextBoxChange = (event) => {
        this.setState({textBox: event.target.value});
    };

    handleAddItem = () => {
        fdb.collection(collectionName).add({
            "title": this.state.textBox
        }).then(function (docRef) {
            //console.log("added " + docRef.id, docRef.get());
        });
    };

    handleRemoveItem = (item) => {
        console.log("Deleting: ", item.id);
        item.ref.delete()
            .then(function () {
                console.log(item.id + " deleted successfully");
            })
            .catch(function(reason){
                console.log(item.id + " failed to delete: ", reason);
            })
    };

    render() {
        return (
            <div>
                <div>
                    <input type="text" value={this.state.textBox} onChange={this.handleTextBoxChange}/>
                    <input type="submit" value="Add Item" onClick={this.handleAddItem}/>
                </div>
                <div>{this.state.items
                    .map((item, index) => <Item key={index}
                                                item={item}
                                                onDeleteClick={this.handleRemoveItem}/>)}</div>

            </div>
        )
    }
}

const Item = ({item, onDeleteClick}) => {
    let pendingWrite = item.metadata.hasPendingWrites ? "[PendingWrite]" : "[!PendingWrite]";
    let source = item.metadata.fromCache ? "[Cache]" : "[Server]";
    return <div>
        <input type="button" value="delete" onClick={() => onDeleteClick(item)}/>
        <span>{source + " " + pendingWrite + " " + item.data().title}</span>

    </div>
};

问题
Chrome 开发者工具中有没有办法模拟/禁用对特定域的调用?

最佳答案

您正在寻找“请求阻止”功能。要使用它,请转到溢出菜单,然后单击“更多工具”,然后单击“请求阻止”。这会在抽屉中打开适当的面板以阻止对某个域或资源的请求。

查找菜单项。

locate request blocking

请求阻止面板。

request blocking in drawer

关于google-chrome - Chrome 开发工具 : How to simulate offline for a particular domain than the entire network?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747745/

相关文章:

performance - Chrome DevTools 100%CPU

javascript - 谷歌浏览器扩展不显示弹出窗口

javascript - 服务 worker 中的全局错误处理是如何工作的?

javascript - :not selector not working on ul class - alternatives?

php - Firebase Firestore 添加数据而不覆盖

javascript - 文件显示在“源”选项卡中但不在“网络”中

javascript - $.getJSON() 不适用于 chrome

ios - 结构体数组在闭包之外不更新

api - Firebase 通过 REST API PHP CURL 获取数据

google-chrome-devtools - 如何转到 Chrome 开发者工具中的特定文件?