adamcharnock / django-tz-detect

Easy user timezone detection for Django
https://pypi.python.org/pypi/django-tz-detect
MIT License
216 stars 38 forks source link

Force reload page on innitial load (opt-in) #72

Open abe-101 opened 4 months ago

abe-101 commented 4 months ago

First of all I would like to thank you for this wonderful package 2 projects I've worked on are used in multiple time-zones and both revolve around reservations so times and time-zones is very critical Storing everything in UTC and using this project to display in the users local TZ is a game changer!

One of the projects often has one time visitors so the Initial load limitation has become an issue - and when thinking of a solution I thought Why not just force a reload if the tz_detect cookie is not set - seems like a reasonable tradeoff for me I know not everyone would want such a behavior - so it should be opt-in (via a config setting)

abe-101 commented 4 months ago
{% if not request.session.detected_tz %}
<script>
    // Trigger a reload on the next tick after the session should be updated
    setTimeout(function() {
        window.location.reload();
    }, 1000); // Adjust the delay as necessary, but keep it reasonable
</script>
{% endif %}

{% load tz_detect %}

{% tz_detect %}

Something like this works - but only on fast internet when i applied some throttling the reload happened before the tz was set so that lead the page into an infinite reload

Perhaps there is a way to do the reload only after the tz is posted to the server