2

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.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • 1
    It's an undocumented [design decision](https://crbug.com/1175349) and there's no clean workaround so you'll have to replicate your actions via chrome.browsingData to unregister the sw and optionally clear the site cache or data. Maybe you can open a report in the [documentation repo](https://github.com/GoogleChrome/developer.chrome.com/issues) since my comment on crbug is evidently ignored. – wOxxOm Sep 19 '22 at 09:02
  • Thanks for pointing this out. I have no idea how Google didn't yield that page. Also that would require adding `browsingData` permission. Sucks to have to explain that we would need to clear up user's Google cache *and* service worker for it to work. Will submit a bug request in the doc repo – Luke Vo Sep 19 '22 at 09:11

0 Answers0