Bug 238866 - ServiceWorker downloads do not start in reopened tabs
Summary: ServiceWorker downloads do not start in reopened tabs
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: Safari Technology Preview
Hardware: Mac (Intel) macOS 12
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-04-06 06:37 PDT by mrskman
Modified: 2022-06-02 06:13 PDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mrskman 2022-04-06 06:37:47 PDT
Safari supports ServiceWorker downloads since 15.4 (Technology Preview 138). It is working great (big thank you for that) unless you want to do downloads in a reopened tab.

How to reproduce #1:
1. Open a new tab and go to https://stat-info.cz/safari-download-reopened-tab.html .
2. Close the tab.
3. Reopen the closed tab (Menu -> Edit -> Undo Close Tab)
4. Download buttons are not working now.

How to reproduce #2:
1. Set your Safari to open all windows from last session (Menu -> Safari -> Preferences -> General).
2. Open a new tab and go to https://stat-info.cz/safari-download-reopened-tab.html .
3. Quit Safari (Menu -> Safari -> Quit).
4. Start Safari again.
5. Download buttons are not working now.

I expect both buttons to download file named file.bin (5 bytes). But it is working only in freshly opened new tab or after you do manual reload of the tab. Tested on Safari 15.4 and Technology Preview 142.
Comment 1 Radar WebKit Bug Importer 2022-04-13 06:38:16 PDT
<rdar://problem/91686107>
Comment 2 youenn fablet 2022-04-13 07:26:11 PDT
Quickly looking at it, I can see that WebPage::didReceivePolicyDecision  policyAction is set to 2 (ignore) in the download failure case while it is set to 1 (download) in the working case.
Comment 3 mrskman 2022-05-26 00:20:31 PDT
(In reply to youenn fablet from comment #2)
> Quickly looking at it, I can see that WebPage::didReceivePolicyDecision 
> policyAction is set to 2 (ignore) in the download failure case while it is
> set to 1 (download) in the working case.

Is there any workaround for this? Is the "Service Workers" component category correct or should I change it? Thank you!
Comment 4 youenn fablet 2022-06-02 04:38:34 PDT
The workaround is to trigger the download synchronously as part of the click event handler. Then everything will work fine from there.

This is not specific to service worker, I would expect that a setTimeout would also trigger that same behavior.


This is controlled by Safari code and I am not sure why this behavior is like this.
I will send the bug report there.
Comment 5 mrskman 2022-06-02 04:58:09 PDT
Downloads there are always asynchronous (navigator.serviceWorker.register is called first), so setTimeout is working fine until you reopen the tab. You can test it with the third button I've added.
Comment 6 youenn fablet 2022-06-02 05:55:48 PDT
(In reply to mrskman from comment #5)
> Downloads there are always asynchronous (navigator.serviceWorker.register is
> called first), so setTimeout is working fine until you reopen the tab.

Right, so one workaround so far is to register a service worker specifically for downloads, in advance, then the download can be triggered synchronously in the click event handler.

> You can test it with the third button I've added.

Thanks for the updated test.
Comment 7 mrskman 2022-06-02 06:13:32 PDT
(In reply to youenn fablet from comment #6)
> Right, so one workaround so far is to register a service worker specifically
> for downloads, in advance, then the download can be triggered synchronously
> in the click event handler.

Which is pain if you need to negotiate the download with server first based on what user chooses. And then there is a chance, that browser has already unregistered the service worker.

Anyway thank you!