Chrome开发者工具:如何为特定域模拟离线而不是整个网络?

时间:2017-10-14 18:04:51

标签: 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建立连接(想想服务工作者使用缓存的静态资产的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开发者工具中是否有办法模拟/禁用对特定域的调用?

1 个答案:

答案 0 :(得分:4)

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

查找菜单项。

locate request blocking

请求阻止面板。

request blocking in drawer