-
I have a simple landing page with a navbar with 3 items "My Blog" "My Social" "My Contact". When I press on any of the nav items e.g. "My Blog" it makes an htmx request to fetch the blog data, populate a content div on the webpage, and update the Url to "www.mysite.com/blogs". Below is my <h3 id='header-blogs' hx-push-url="true"hx-get="/blogs" hx-swap="outerHTML" hx-target="#content_div">My Blog</h3> This all works good however whenever the user refreshes the browser it only loads the My current hack is to check to see if Here is my django backend logic: def blogs_view(request):
if request.META.get("HTTP_HX_REQUEST"):
blog_list =get_blog_list()
return render(request, 'blogs.html', context={'blogs': blog_list})
return render(request, 'index.html', context={'active': 'header-blogs'}) And my django template/javascript: {% if active %}
$(document).ready(function(){
$('#{{active}}').click();
});
{% endif %} This isn't ideal because I have to add unnecessary javascript and it also messes with the history since I've pushed www.mysite.com/blogs twice (once when I reload and another time when I programmatically click on I might just be completely using HTMX wrong.. any help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Perhaps there's an HTMX solution for this that someone will share, but I'd recommend looking at making a pwa. Or at least using a service worker. You could cache the app/site shell in thr browser such that it is always loaded instantly without network latency. Then the content could be fetched and swapped with htmx stuff |
Beta Was this translation helpful? Give feedback.
-
Hey, I think your issue here is not related to refreshing a page itself.
As such, it doesn't seem to be an ideal solution to me to generate first, an "empty page", then trigger some client side logic to only go back to the server again and fetch the content that user was asking for in the place, by accessing that specific page URL. It seems to me you'd have the same problem than the one you're having here, by opening a new tab instead of refreshing your current page; the situation is the same: the browser makes an initial request to the page, and it's indeed up to your backend to determine whether a full page should be rendered (with head body etc.) or just the partial content that you'd be after when using htmx. Solutions that I can think of:
<h3 id='header-blogs' hx-push-url="/blogs" hx-get="/partial/blogs_list" hx-swap="outerHTML" hx-target="#content_div">My Blog</h3> The URL in your website would still become
Hope that helps! |
Beta Was this translation helpful? Give feedback.
-
I created a middleware to handle this situation. So first you load full template (as the browser don't send the header), then user click on a hx-get control with push-url... the middleware will not wrap the content so the server send just the content needed... if the user refresh the website the middleware detects the lack of the header and wrap again the content with the template. This way your endpoints/templates/pages don't need to deal with custom flags / ifs in their logic. |
Beta Was this translation helpful? Give feedback.
Hey, I think your issue here is not related to refreshing a page itself.
If you're going to use push-url, it means the URL should be accessible from a very new tab without any history, right?
So you could like bookmark
www.mysite.com/blogs
and access that page directly, without going through the website's root first.As such, it doesn't seem to be an ideal solution to me to generate first, an "empty page", then trigger some client side logic to only go back to the server again and fetch the content that user was asking for in the place, by accessing that specific page URL.
It seems to me you'd have the …