kniEngine / kni

KNI is a cross-platform C# game framework.
Other
127 stars 9 forks source link

Files can get cached by the XMLHttpRequest when loading through TitleContainer #1763

Closed vchelaru closed 1 day ago

vchelaru commented 1 month ago

Internally the TitleContainer on BlazorGL uses XMLHttpRequest. Many web requests are cached, and I've experienced this with calls on my game, even if I stop/restart the game.

When loading files locally, all calls should actually load the file rather than relying on cached calls. This can be addressed by adding a parameter to the end of the call that forces a new call.

This could get changed: https://github.com/kniEngine/kni/blob/ffae6ab3ea93904c07b3ce4489b15e0dfa290ccb/Platforms/.Blazor/ConcreteTitleContainer.cs#L27

with this:

var suffix = "?token=" + DateTime.Now.Ticks;
request.Open("GET", name + suffix, false);

I'm not familiar enough to know if we really want this or if this will introduce other problems so I'm opening this issue for discussion rather than making a PR. Thoughts?

What version of KNI does the bug occur on:

What operating system are you using:

What KNI platform are you using:

jordi-z80 commented 1 month ago

A few thoughts, does passing the file with a ?token= suffix to OpenStream works?

I'm saying this because if it does, then you wouldn't need to modify the library, you could let the user specify it.

Given that, you could also pass a hash of the file (managed by the user, if they want) instead of a random seed: This would cache the files with the same hash, but skip cache on modified files.

vchelaru commented 1 month ago

I believe that passing in a token=suffix would in fact work; however, it's also worth noting that (as I understand it) Kni intends to abstract as much away to be XNA-like, so this change would in fact make Kni browser work the same as other platforms. Just like other platforms, the developer is encouraged to either cache their own content in code, or use content managers which do that. If you bypass the XNA-style caching, I think that all platforms should work consistently without having to #if a suffix on your load calls for web only.

Perhaps I'm missing a reason why putting the suffix internally is a bad idea?

jordi-z80 commented 1 month ago

No, I'm not saying it was a bad idea (actually mine is probably worse unless you discard everything after the "?" in the other platforms, most of which can't have a '?' in the filename anyways), just brainstorming ;)

nkast commented 4 days ago

The way to do tell the browser to check for file updates is via the 'Cache-Control: no-cache' or by setting a very small expire date. That is a response header and has to be set on the server. I don't know if it can be set in the default debug webserver of VS, but more likely it can be set when setting up IIS as the debug server.

It's also possible to add the header in the XMLHttpRequest from the client side, but I don't like the idea. If you own the server, then the right way is to set those files as dynamic to send a 'no-cache' and server 200s or 302s. If you don't and you published the game on a hosting service like github pages, or itch.io, you have to respect the settings of that server. If the games of a particular engine starts making 100x more requests, that will upset someone somewhere.

After all, that's an issue during development, and it's part of the life of webdev. if there is a solution to this, it has to be enabled during DEBUG builds automatically.

vchelaru commented 1 day ago

Makes sense. I have added a token in FlatRedBall which solves it, so I'll close the issue.