Liquid 'now' date not updating on page refresh

Highlighted
Shopify Partner
30 0 5

Hi, I'm working on something that will only allow customers to place orers on certain items after a certain date has passed, which is specified in the product metafields, if the current time is before the unix timestamp defined in the metafield the add to cart button should not work. I've written it out in Liquid and it mostly works fine, however the time returned by the 'now' object seems to be cached on Shopify's server. I've found an older thread where the last comment explains my issue very well: https://ecommerce.shopify.com/c/ecommerce-design/t/issue-with-getting-accurate-realtime-date-time-vi...

 

Here's the code I'm using to get the current timestamp: {% assign now_timestamp = 'now' | date: '%s' | plus: 0 %}

I have it write the value of this variable to the console on page load and sometimes it can go minutes, or even hours without changing. Keep in mind that this Liquid block is on the product page so it should be reflecting the accurate time. I don't want to use JS to get the current time locally as this would be far less secure. Is there any way that this can be fixed?

0 Likes
Shopify Partner
1843 172 547

Hi Adam Sanderson

Out of luck there. Unless you want to write an app, JavaScript will be your only friend :-(

I turn coffee in to code - since 1998
0 Likes
Shopify Partner
1737 48 156

About liquids filter "date" not showing correct current time, i.e. not updating every page load

This is ironically a feature of shopify's platform aggressive rendering and then caching of assets(pages,images) there's no simple way around this. 

So in liquid when you use the date filter the page is rendered on the server that page is cached with the moment in time of creation and wont change until either the template is edited again or something causes the shopify cache to invalidated and update.

I think there used to be liquid trick to get the cache to update more often but when you get down to it you

A) use javascript for simple frontend timer, B) have a custom app for time sensitive where time actually matters

If this not working like you thought it would annoyed you, please contact support@shopify.com and request they make the liquid date filter documentation better by explaining this gotcha as your not the first and wont be the last

Problem Solved? ✔️Accept the solution so you can help others.
Confused? Busy? ? Get me to solve it paull.newton+shopifyforum@gmail.com.
Buy me a coffee? ☕ paypal.me/paulnewton or donate to eff.org
0 Likes