Project

General

Profile

Firefox 98+ PDF download with "Open in Firefox" can orphan Wt session and display download URL

Added by Bruce Toll about 2 years ago

Firefox 98+ changed how downloads are handled for PDFs with the "Open in Firefox" option. Previously, I believe that the PDF would always be downloaded to a local file and opened in a new tab with the URL reflecting the local file name. Now, Firefox opens the download URL in the same tab as the Wt session, effectively orphaning the Wt session. That is, the back button will restore the previous URL but will start a new session in the default Wt configuration. In addition, the URL in the address bar for the PDF reflects the original Wt WResource URL -- which will become invalid once the session ends (e.g. times-out).

This behavior occurs even when suggestFileName() is used to set a filename and ContentDisposition::Attachment. The can be observed with Firefox 98+ and the widgetgallery downloading one of the PDFs at .../media/pdf-output.

As a partial workaround, the WLink for the resource can be changed to LinkTarget::NewWindow. This will keep the original Wt session active, but the URL in the PDF tab will still refer to the original Wt WResource link, rather than a locally downloaded file. WResource::setInvalidAfterChanged() can also be used to invalidate resources so that refreshing a downloaded PDF tab will either retrieve the same PDF or return a 404 not found.

When there is control over the user's browser, the previous download behavior (to a named local file) can be obtained by selecting "Use firefox" or "Always ask", rather than "Open in Firefox".

I do not believe that "Open in Firefox" is the default option for PDFs, and Firefox is no longer as popular as it once was, so this may not be much of an issue in practice. In any case, I do not think this is a Wt issue, but I thought it was worth noting in case anyone else encounters this download issue or has thoughts on how to handle it better.

References:
https://www.mozilla.org/en-US/firefox/98.0/releasenotes/
https://support.mozilla.org/en-US/kb/manage-downloads-preferences-using-downloads-menu
https://support.mozilla.org/en-US/kb/change-firefox-behavior-when-open-file


Replies (3)

RE: Firefox 98+ PDF download with "Open in Firefox" can orphan Wt session and display download URL - Added by Jonathan lisein almost 2 years ago

Hello Bruce Toll,
Thank you for sharing this information.
Could you be a bit more specific in the workaround you suggest? In particular, I do not understand how to change the Wlink of a WFileResource...
Jonathan

RE: Firefox 98+ PDF download with "Open in Firefox" can orphan Wt session and display download URL - Added by Bruce Toll almost 2 years ago

Hi Jonathan,

The workaround involves calling setTarget(Wt::LinkTarget::NewWindow) on the WLink associated with your WResource. The anchor documentation and example in the WidgetGallery is a helpful supplement to the reference documentation: See: https://www.webtoolkit.eu/widgets/navigation/anchor.

The setTarget(Wt::LinkTarget::NewWindow) adds an attribute of "target=_blank" to the html anchor element so that the browser will open the URL in a new tab or window. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target.

The anchor examples in the widgetgallery (above) are external links, but you can also use setTarget for a WLink to a resource. As an example, the widgetgallery pdf image example can be modified to open the image pdf in a new tab/window with the following patch:

diff --git a/examples/widgetgallery/examples/PdfImage.cpp b/examples/widgetgallery/examples/PdfImage.cpp
index fbd54464..76dc92fb 100644
--- a/examples/widgetgallery/examples/PdfImage.cpp
+++ b/examples/widgetgallery/examples/PdfImage.cpp
@@ -39,5 +39,7 @@ auto container = std::make_unique<Wt::WContainerWidget>();
 auto pdf = std::make_shared<SamplePdfResource>();

 Wt::WPushButton *button = container->addNew<Wt::WPushButton>("Create pdf");
-button->setLink(Wt::WLink(pdf));
+auto pdfLink = Wt::WLink{pdf};
+pdfLink.setTarget(Wt::LinkTarget::NewWindow);
+button->setLink(pdfLink);
 SAMPLE_END(return std::move(container))

Hope this helps. --Bruce

RE: Firefox 98+ PDF download with "Open in Firefox" can orphan Wt session and display download URL - Added by Jonathan lisein almost 2 years ago

Thank you Bruce,

I was using a FileResource with application redirect :

    std::unique_ptr<WFileResource> fileResource = std::make_unique<Wt::WFileResource>(aFile);
    boost::filesystem::path p(aFile);
    fileResource->suggestFileName(p.filename().c_str());
    m_app->redirect(fileResource->url());

Thus I have adapted all my code to comply with your proposition.

I work with the authentification widget and I have noticed that when a user connect himself, the pdfLink was directing to a blank page without calling the handlerequest() of my resource. Thus after every calls to session.login().changed() , I recreated an "auto pdf = std::make_shared();"
Jo

    (1-3/3)