declarativeNetRequest (declarative_net_request) does not work on cached requests served by Service Worker

Viewed 24

Consider the following example that simply add a header to all responses from example.com and www.google.com/maps. example.com is used to show that the code is correct and works well, and for the Google Maps page, a cached version is served that bypass this rule. Note that to reproduce the issue, please access Google Maps before side-loading this extension so it can cache the page.

manifest.json:

{
    "version": "3.0",
    "manifest_version": 3,
    "permissions": [
        "declarativeNetRequest"
    ],
    "host_permissions": [
        "*://*.example.com/*",
        "*://www.google.com/maps*"
    ],
    "declarative_net_request": {
        "rule_resources": [
            {
                "id": "rule",
                "enabled": true,
                "path": "rule.json"
            }
        ]
    },
    "name": "Test Net request"
}

rule.json:

[
    {
        "id": 1,
        "priority": 1,
        "action": {
            "type": "modifyHeaders",
            "responseHeaders": [
                {
                    "header": "my-header",
                    "operation": "set",
                    "value": "my-header-value"
                }
            ]
        },
        "condition": {
            "urlFilter": "*.google.com/maps*",
            "resourceTypes": [
                "main_frame",
                "sub_frame"
            ]
        }
    },
    {
        "id": 2,
        "priority": 1,
        "action": {
            "type": "modifyHeaders",
            "responseHeaders": [
                {
                    "header": "my-header",
                    "operation": "set",
                    "value": "my-header-value"
                }
            ]
        },
        "condition": {
            "urlFilter": "*.example.com*",
            "resourceTypes": [
                "main_frame",
                "sub_frame"
            ]
        }
    }
]

When accessing https://www.example.com, the header is there as intended:

enter image description here

However when accessing Google Maps that has been accessed before, the header is not found because a cached version is served:

enter image description here

Is it a bug from Chrome? This problem causes my extension to not work properly unless user clear their whole Google's data from browser. When I test, clearing cache does not work, I had to unregister the Service Worker which would then log user out. And user wouldn't be happy following all these instruction. How can I force future requests to go through this rule?

UPDATE: Apparently it's a Chromium bug that won't be fixed. I submitted a request to update the documentation. You can upvote there if you have the same issue.

0 Answers
Related