Using/Getting timestamp inside template
-
Hi nodeBB-Community,
we are using nodebb 1.4.6 and I'd like to cache-bust the profile-avatars, so changes are immediately seen by every user.How can I use
Date.now()
inside a template? -
Do you have a custom theme? You can add a cache buster into all routes by using the hook
filter:middleware.render
here is a sample plugin function that uses that hook.myPlugin.onRenderPage = function(hookData, callback) { hookData.templateData.cacheBuster = Date.now(); callback(null, hookData); };
Now every page will have
cacheBaster
available for templates to use, in your template you can do<img src="someUrl?cacheBuster={cacheBuster}"/>
and it will use theDate.now()
value you passed in from the server side. -
Why are you in such an old version of NodeBB? You should really upgrade, there have been a couple of security issues fixed since that version.
-
@baris Thank you very much for the example. I use a custom theme and this works perfectly as expected in some parts of the template. I changed it in the following files:
modules/nodebb-theme-name/templates/partials/menu.tpl
modules/nodebb-theme-name/templates/partials/account/header.tpl
modules/nodebb-theme-name/templates/account/edit.tplAnd it's working. A new uploaded profile-picture is immediately shown after upload, also without deleting the browser-cache.
Now in the upload-modal:
modules/nodebb-theme-name/templates/partials/modals/change_picture_modal.tpl
the old picture is still shown. Adding thecacheBuster
-query-string there doesn't work, the timestamp is not appended. Is there a hook also for this case?Also is there a list of all possible hooks/events that are happening in the system?
Another thing I noticed is, that after uploading a new profile-picture, the success-callback changes the picture in the header-menu and in the edit-page immediately, but not in the profile-header part (after refreshing, with the new cacheBuster, it is changed).
I've traced it to this file: app/public/src/client/account/edit.js.
How can I now react on the image-change in my custom theme? -
I don't think we have a hook for that, I was also looking at the source code and seems like Date.now() is already appended to the url here https://github.com/NodeBB/NodeBB/blob/master/public/src/client/account/edit.js#L189, is your version of nodebb lacking that code?