descriptionPmWiki recipe that adds versions (modification timestamps) to attachment URLs (for proper browser cache invalidation).
homepage URLhttps://www.pmwiki.org/wiki/Cookbook/VersionedAssets
ownersaid@saidachmiz.net
last changeSun, 12 Dec 2021 19:39:05 +0000 (12 14:39 -0500)
content tags
add:
readme

If your wiki’s webserver is properly configured (i.e. sends cache-control headers properly), then images and other attachments will be cached by visitors’ browsers (so that visitors to your site don’t have to download the same images/etc. over and over when they visit a page multiple times). This is good, and makes your wiki feel faster and more responsive.

But what happens you modify or update an attachment, or upload a new version? Repeat visitors who have the old version cached, will still see the old versionunless you have versioned assets. That’s what this recipe adds.

Versioned Assets appends, to the URLs for attachments (a.k.a. “attachlinks”), a URL parameter containing the modification time of the uploaded file (assuming the file exists, of course). This means that when an uploaded file is modified, or when a new version is uploaded, the URL of the attachlink will automatically update. A browser that has the old version of the file cached, will see the changed URL, and will retrieve and display the new version, instead of showing the old version.

Installation

Download versioned_assets.php and put it into your cookbook/ directory. Then add such a line to config.php:

include_once("$FarmD/cookbook/versioned_assets.php");

Configuration

There is one configuration option: $VersionedAssetsReattachFileExtension (defaults to true). It causes an uploaded file’s extension to be appended to the version string that’s added to the attachment link URL. (This is so that LinkIcons, and similar things that check whether a link ends in a particular file extension, can continue to work properly.)

To disable this behavior, add the following line to config.php (before including the recipe):

$VersionedAssetsReattachFileExtension = false;

Usage

You don’t need to do anything special once Versioned Assets is installed; all attachment links (generated by Attach: markup) will automatically include the version in the URL.

Notes

Versioned Assets should work equally well with $EnableDirectDownload on or off, and also with SecureAttachments configured. However, I have not tested all of these possibilities.

This recipe may be incompatible with certain other recipes that change how Attach: markup is handled, or how attachment links are generated.

Change log / Release notes

F.A.Q.

Will old links to an attachment still work, when a new link is generated? (e.g. if someone saves the direct link to an attachment on my wiki) Or do old links stop working?

Old links (those with a previous version parameter in the URL) continue to work, but someone who has an old link may see an old (cached) version of an updated attachment.

If someone omits the version parameter from a link to an attachment on my wiki, will the link work?

Yes, the link will work, but again, someone with a version-less link may see an old (cached) version of an updated attachment.

shortlog
2021-12-12 Said AchmizMinor fixmaster
2021-12-12 achmizsAdd LICENSE and README.md
2019-01-28 achmizsInitial commitmob
heads
2 years ago master
5 years ago mob