Re-render media on page load/reload
Author: draganr
Publication Date: 5/8/2019 15:55
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
-
Author: felix_reinhold - 5/9/2019 8:24
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 -
Author: draganr - 5/10/2019 9:44
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 -
Author: felix_reinhold - 5/10/2019 10:00
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 -
Author: draganr - 5/10/2019 11:55
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 -
Author: felix_reinhold - 5/10/2019 12:37
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
0 -
Author: Nitros - 5/13/2019 8:43
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 -
Author: draganr - 5/29/2019 10:06
Hi Felix, hi Nitros,
Thanks for your help, I'll use it in a different way.
Kind regards,
Dragan
0
Vous devez vous connecter pour laisser un commentaire.
Commentaires
7 commentaires