1

Let's say we don't want to use visual force page at this case.

I want to show the static resource in a external site.

Let's say filename: me.png

however, user will keep updating this file. Then, how could we avoid the caching problem.

For example: https://na53.lightning.force.com/me.png

I believe this will cause the cached issue and also we have no way to get the timestamp for fetching this image.

How could we solve the problem?

3

The URL of a static resource automatically includes the timestamp when it was last updated providing you access it via $Resource (and similarly $Resource for Lightning) and this ensures that the static resource is re-cached at the client-side when it changes.

Use this mechanism in your logic.

If you can't go via Visualforce, you can create the equivalent URL yourself via querying the static resource's LastModifiedDate:

        for (StaticResource sr : [
                    select LastModifiedDate
                    from StaticResource
                    where Name = :name
                    order by CreatedDate desc
                    limit 1
                    ]) {
            return '/resource/' + sr.LastModifiedDate.getTime() + '/' + name;
        }

Or if no caching at all is OK, you can just insert the numerical value of now in the URL and that will route to the latest version. (As may having no timestamp at all - use your browser's "Network" tab from its "Developer Tools" to check.)

  • this solution works out. We did put Time.now() for fetching the latest statis resource, many thanks! – Dummy Aug 6 '18 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.