draganr
I'm new here

Re-render media on page load/reload

Jump to solution

Hi all,

I have a format template: ft_downloadJSON, that is rendered inside the javascript file: download.js.

When I change something inside that format template, I need to wait for some time that the changes are also available in download.js, or I need to edit that js file and save it.

Is it possible to automate this, so when I change something inside format template, that it is automatically applied to that media file?

(I have the page where I call ft_downloadJSON and everything works fine there, but again, in media, it needs some time.)

Also inside ft_downloadJSON, I created JSON from a database source, and when I change something (e.g. headline of any entry)  in the data source it also needs some time that it is changed inside download.js.

Kind regards,

Dragan

Labels (1)
1 Solution

Accepted Solutions
Nitros
I'm new here

Re: Re-render media on page load/reload

Jump to solution

I am assuming this is about the preview.

Is there any reason to put the JSON inside a JS file? You could just put it into a script tag:

<script id="data" type="application/json">$CMS_RENDER(template: "ft_downloadJSON")$</script>

Wherever you need the data just read it from the tag:

function loadJsonFromDocument(id) {

    const element = document.getElementById(id);

    return JSON.parse(element.innerHTML);

}

I do that quite a lot and never had issues.

What I assume is happening here is that the preview get's confused. You already need to use force refresh with format templates in a lot of cases. If you add a JS file as well it just get's more complicated.

If it's really important I would try to simplify the data transfer and not mess with the preview servlet cache. Also, just to make sure, you did use force refresh by holding SHIFT when opening the preview? You should see a forceRefresh=1 in the preview url.

View solution in original post

0 Kudos
7 Replies
felix_reinhold
Returning Responder

Re: Re-render media on page load/reload

Jump to solution

Hi Dragan,

I had a similar issue once. For me it was just the normal server caching. Have you tried adding a random request param when opening the file? I just added the lastChange timestamp to all refs of parsed media in preview/cc.

Best regards

Felix

0 Kudos
draganr
I'm new here

Re: Re-render media on page load/reload

Jump to solution

Hi Felix,

I have tried the following, but still the same.

  <script id="download-search-js" type="text/javascript" src="$CMS_REF(media:"downloadsearch", abs: 2)$" $CMS_IF(#global.preview)$data-last-change="$CMS_VALUE(ref(media:"downloadsearch").getMetaData().revision.getCreationTime)$"$CMS_END_IF$></script>

I have also tried with #global.now, but no success.

  <script id="download-search-js" type="text/javascript" src="$CMS_REF(media:"downloadsearch", abs: 2)$" $CMS_IF(#global.preview)$data-last-change="$CMS_VALUE(#global.now.milliseconds)$"$CMS_END_IF$></script>

Sorry, but do you maybe have an example?

Regards,

Dragan

0 Kudos
felix_reinhold
Returning Responder

Re: Re-render media on page load/reload

Jump to solution

Hi Dragan,

you should add the timestamp as GET-param

$CMS_SET(mediaRef, ref(media:"downloadsearch", abs: 2))$

<script id="download-search-js"  type="text/javascript" src="$CMS_VALUE(mediaRef.url)$$CMS_IF(#global.preview && ps_debug)$?_t=$CMS_VALUE(mediaRef.target.revision.getCreationTime())$$CMS_END_IF$"></script>

or you could try it with forceRefresh (don't know if servlet listenes to this param for media)

$CMS_SET(mediaRef, ref(media:"downloadsearch", abs: 2))$

<script id="download-search-js"  type="text/javascript" src="$CMS_VALUE(mediaRef.url)$$CMS_IF(#global.preview && ps_debug)$?forceRefresh=1$CMS_END_IF$"></script>

If this works for you then you should check, if there are already get-params in the url and change use "&" or "?" for th eurl depending on the result.

best regards

Felix

0 Kudos
draganr
I'm new here

Re: Re-render media on page load/reload

Jump to solution

Hi Felix,

I have tried something similar before:

<script type="text/javascript" src="$CMS_REF(media:"downloadsearch", abs: 2)$?v=$CMS_VALUE(#global.now.milliseconds)$"></script>

Now I also tried your suggestions, but still the same. It loads changes after 30 minutes.

This is the way it's called inside downloadsearch.js

var downloadSearch = (function () {

    var jsondata = $CMS_RENDER(template:"ft_downloads_json")$;

        .....

})();

Regards,

Dragan

0 Kudos
felix_reinhold
Returning Responder

Re: Re-render media on page load/reload

Jump to solution

HI Dragan,

just created a small PoC and sadly I can't avoid the caching aswell.

Maybe sth. has changed in the PreviewServlet or I just didn't remember correctly.

Maybe there's another parameter that can be used. Otherwise the only solution I can come up with would be to move the download.js to a PageTemplate instead of a File.

Best regards

Felix

Nitros
I'm new here

Re: Re-render media on page load/reload

Jump to solution

I am assuming this is about the preview.

Is there any reason to put the JSON inside a JS file? You could just put it into a script tag:

<script id="data" type="application/json">$CMS_RENDER(template: "ft_downloadJSON")$</script>

Wherever you need the data just read it from the tag:

function loadJsonFromDocument(id) {

    const element = document.getElementById(id);

    return JSON.parse(element.innerHTML);

}

I do that quite a lot and never had issues.

What I assume is happening here is that the preview get's confused. You already need to use force refresh with format templates in a lot of cases. If you add a JS file as well it just get's more complicated.

If it's really important I would try to simplify the data transfer and not mess with the preview servlet cache. Also, just to make sure, you did use force refresh by holding SHIFT when opening the preview? You should see a forceRefresh=1 in the preview url.

0 Kudos
draganr
I'm new here

Re: Re-render media on page load/reload

Jump to solution

Hi Felix, hi Nitros,

Thanks for your help, I'll use it in a different way.

Kind regards,

Dragan

0 Kudos