Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version param to PublicUrl in FileStorage #2061

Closed
lukasan opened this issue Apr 11, 2024 · 5 comments
Closed

Add version param to PublicUrl in FileStorage #2061

lukasan opened this issue Apr 11, 2024 · 5 comments

Comments

@lukasan
Copy link

lukasan commented Apr 11, 2024

Hello,
I came across an issue with file updating.
I have a website that saves and serves files like doc, pdf, docx and etc. into local storage.
Using Piranha 10.3.0.

Steps to reproduce:

  1. Open details of any editable file in Media, like a .docx, maybe even a .txt would work.
  2. Download the file using URL (not Download)
  3. Change the content of the file
  4. In Update tab of the file, upload the changed file.
  5. Details tab of the file click Download media link - downloaded file is actually changed, works as expected.
  6. Click on URL - returns the previously cached downloaded version.

I have custom blocks with Tiny editor that link to the file. I don't know if there is a way to serve such files, only with a link to the file in the blocks.

It does not matter if I add or remove inside of AddPiranha:

options.UseMemoryCache();

that is PiranhaServiceBuilder.

Changing cache level also does not do anything here:

Piranha.App.CacheLevel = Piranha.Cache.CacheLevel.None;

the result is the same. Is it even a Piranha issue?
I noticed #502 but the requirement is that the link should stay the same. So no version appends or anything.

Tested on Edge and Firefox.

@tidyui
Copy link
Member

tidyui commented Apr 13, 2024

Hi there! The public URL used to retrieved an asset is not proxied through any code from Piranha, the storage providers we provide Local.FileStorage and Azure.BlobStorage exposes the public URL for the asset which is then handled either by Asp.net or by Azure. In either case these providers usually provide cache headers to make sure browsers cache assets and this is probably the root cause, not anything done by Piranha.

The cache code you're referring to handles server-side caching of the model class which (like you noticed) won't affect the browser cache for images.

The easiest way to deal with this problem would probably be to append a query string parameter to the URL of your asset, like ?version=xxx where the version number could be a hash generated from the LastModified date of the asset. This way browsers will use the cached image as long as your version-id remains the same, but after the file has been updated the id is updated and the browser should re-fetch the image.

Please note that if you're using a CDN that caches all assets this has to be configured properly as well.

Regards

@lukasan
Copy link
Author

lukasan commented Apr 13, 2024

Thank you for a detailed response!

Unfortunately, I cannot change the path in any way or I would need to change the content in blocks for each appearance.

Can't I extend Local.FileStorage to add no cache headers? I searched for a solution, but couldn't find anything that works. And disabling cache overall is probably a bad idea. And any middleware does not hit /uploads that I tried. It's not really "static files", I guess.

A server-side ASP.NET solution for this is probably completely out of scope for Piranha, but maybe you'd know how it might be done from the top of your head?

@tidyui
Copy link
Member

tidyui commented May 12, 2024

Hi again! Sorry for the slow response here. This is the code in the file storage that is responsible for creating the public url

https://github.com/PiranhaCMS/piranha.core/blob/master/core/Piranha.Local.FileStorage/FileStorage.cs#L67

If you want a quick fix you can just copy this project, make your own version of the file storage provider and register this in your application instead. I'll add this as a feature request to the included provider as well for the upcoming release.

@tidyui tidyui changed the title After updating a file, the file link return a cached version Add version param to PublicUrl in FileStorage May 12, 2024
@tidyui tidyui self-assigned this May 12, 2024
@tidyui tidyui closed this as completed in 8938e3f May 12, 2024
@tidyui
Copy link
Member

tidyui commented May 12, 2024

Syntax for setting this up in application startup will be:

options.UseFileStorage(naming: Piranha.Local.FileStorageNaming.UniqueFolderNames, generateVersionParam: true);

@lukasan
Copy link
Author

lukasan commented May 12, 2024

Thanks, turns out in my case there was no real issue, since, I think, etag is handled by a browser when a new file is uploaded and accessed via the same URI.

I tried hacking with Nginx to add no cache on /uploads path. Also injecting middleware to HTTP requests to return no cache. All that was not ideal. Also terrible for performance (instead of milliseconds for a PDF it became half a minute). Not to mention all media in blocks.

I'm glad something for improvement came out of this, though! Thank you for your responses.

Maybe even some sort of a configuration for which separate files to set no-cache or version param to might make it to an enhancement request? 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants