-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preload extension preloads URLs but navigating there loads the data again #108
Comments
Hey, thanks for posting this. I'll try to look into it soon. Without doing research, my first thought is that there might be different headers between the two requests, which is why your browser is not using the preloaded/cached content. Do you have anything in your code that changes the request headers? |
Hi @benpate , actually I would have expected that HTMX would not send a new request to the server at all, when an URL was preloaded. That's how I usually do it in my setups. |
For this extension, we're are leaning on the browser's built-in caching mechanism. It does not store a separate copy of the server response, which could get cumbersome really quickly. How do you usually do this, then? Do you have sample code that you could share? If there's a better way, then we should do that :) I guess this means that other server headers could also be messing with the browser's cache -- for instance, a no-cache header would definitely prevent it from working. |
Sorry for my late reply. I understand. If this is out of scope for HTMX, I will continue using my own implementation. I am using a Map (caniuse) for that kind of functionality, with the URLs as keys and the document content, saved scroll positions etc. as data. I also have a cleanup mechanism running, so that I never have more than a certain amount of cached documents in this map (to prevent RAM issues). Here is the structure of my cache object, maybe this helps if you ever want to implement something like this in the future: 0: {
key: "https://mysite.test/"
value: {
data:
$document: "<!DOCTYPE html> ...",
openAccordeonItem: "",
scrollPositions: {root: 0},
url: "https://mysite.test/",
},
keepInCache: true,
state: "loaded"
}
}
1: {"https://mysite.test/news/" => {...}}
2: {"https://mysite.test/about/" => {...}}
3: {"https://mysite.test/members/" => {...}}
4: {"https://mysite.test/rooms/" => {...}}
5: {"https://mysite.test/more-information/" => {...}}
6: {"https://mysite.test/contact/" => {...}} EDIT: The above object would be the state after navigating 7 links in my site. |
Hey @hirasso - I have seen some odd examples where the preload extension's HTTP headers don't match those in the final request, leading to duplicate requests. I still want to track those down, along with updating the extension with some of the lessons I've learned from using it. But, I DO want to stick with using the Browser's built-in cache because browser vendors have put so much work into optimizing these tools, and I want to use the platform as much as possible. But htmx is immensely flexible, and you could certainly make an alternative extension tha stores content in a Map of some kind. Absolutely feel free to use the existing extension as a baseline for migrating your own tool into an extension. :) |
Sorry for my late reply. Got caught-up with live ;)
Today I started investigating this again. The request headers for the preload request:
The request headers if I actually click the link:
There actually is a difference, and it's the HX-* headers for the actual link click!
|
I've also been experiencing this with the latest version of Chrome on Windows 10 so I would say chances are good that this affects a large number of users. My findings are the same as @hirasso back in August regarding the headers. Having the preload extension working properly would be a huge boon for me! |
Absolutely. And, sorry I haven't been able to make much headway on this recently. The short answer is that it's probably something with the headers being sent -- the extension tries to mimic a regular HTMX request, but there must be differences. Could you open up the HTTP requests for a typical transaction and pull the headers for a) the preload request and b) the actual request? What headers are different? In hiraso's example above, it was One other hack is to try using the |
Hi, i'm having the same issues with using the preload extension. Resources are being downloaded twice. The issue seems to be a difference in these headers : hx-target: and hx-trigger. When id and name are set on the element these differ e.g one one request they are And on the other they match those on the element eg When no id and name are set on the el, the headers are only present on one of the requests, and are set then as |
Thanks for this report. Long-term, I think we'll need another way of handling preloads. For now, does the |
I have same issue. |
Sorry you're having trouble, @onemoreahmad. Can you provide more information? Have you tried working with the |
Hi @benpate, thanks for your reply. 1 instantclick.ioI've tried few similar tools to see how it works, first, I checked http://instantclick.io/ Eventhough maybe instantclick doesn't use ajax between pages, just a regular links. 2 quicklinkThen I checked quicklink, in there website when I open network tools, nothings shows up when I hover or click on links, but I tried it with my project which I use HTMX.
It work on my website only on the current links exists on the page when it first loaded, but next time I click on the link again, it just work normally with a full server request. 3 using HTMX Preload extensionwhen I install the plugin, when I hover the link, it gets loaded from the server, and then when I click on it it loads it again, no cache nothing. When I check headers requests both request it identical, the only difference is the xsrf token, and both has Vary key |
Hey.. sorry I couldn't dig into this until now. It's hard to see all the header information on your screenshot, but if your responses have Working with the browser's cache IS a little fiddly, and is outside of our control with this extension. I'm hoping that we get more access to the internals in htmx 2.0, which might give us more control over how this caching works. In the meantime, I'm using a more aggressive version of this code that basically just loads the content on mousedown (without waiting for the mouseup or click events). You're welcome to use this if it makes sense in your app. |
@onemoreahmad I'm noticing When I hit this, I added an appropriate |
I am having this issue when combining From documentation:
The issue combining
Here HTMX will make 1 request with Removing I think the right thing to do would be for this extension to check if the link is inside a |
Hey @yokomizor - I'm sorry you're having trouble with the extension. I don't have much experience using |
Hey @benpate, It's all good. The trouble is actually very minor and very easy to patch manually from my side. About your suggestion, let me try to explain why this is not what I was looking for. Take a look at this piece: // Special handling for HX-GET - use built-in htmx.ajax function
// so that headers match other htmx requests, then set
// node.preloadState = TRUE so that requests are not duplicated
// in the future
var hxGet = node.getAttribute("hx-get") || node.getAttribute("data-hx-get")
if (hxGet) {
htmx.ajax("GET", hxGet, {
source: node,
handler:function(elt, info) {
done(info.xhr.responseText);
}
});
return;
}
// Otherwise, perform a standard xhr request, then set
// node.preloadState = TRUE so that requests are not duplicated
// in the future.
if (node.getAttribute("href")) {
var r = new XMLHttpRequest();
r.open("GET", node.getAttribute("href"));
r.onload = function() {done(r.responseText);};
r.send();
return;
} Here we see that there are 2 different strategies for requesting content for preloading. For This is actually expected behavior according to preload extension docs. It seems reasonable to do this, since usually users clicking a However, using The issue is that preload is not aware of Does that make more sense? |
Hey @yokomizor, |
Same problem here... hx-boost and preload extension are not working together if you have partial renders. |
@hlozancic @fbinz @yokomizor I have implemented a change which fixes this issue in PR#106 of the extensions repository. When this change is merged, hx-boosted elements will include |
Fixed in #106 |
Hi there! I might be confused on how the preload extension is supposed to work. See this screenshot of the chrome dev tools inspecting the network activity when navigating htmx.org:
Starting from the front page I hovered over the
docs
link in the main navigation.preload.js
loaded the URL, as expected. Then I clicked ondocs
and nowhtmx.js
loaded the URL again from the server, seemingly without using the preloaded data. As soon as thedocs
page had been rendered, moving the cursor just a little bit (it was still resting on thedocs
link), triggeredpreload.js
again, resulting in loading the URL again from the server. Same scenario with thereference
link. Three server requests instead of one for each link withpreload
.Am I missing something here? I would have expected that after preloading a URL, navigating towards it would not need another server request.
P.s.: I don't think the error in the console is related, it just points out a
404
forhttps://unpkg.com/prismjs@1.20.0/components/prism-ecmascript.min.js
:The text was updated successfully, but these errors were encountered: