openlayers: Issue with setUrl causing flickering when switching tile sources
Description
When using the setUrl function from the ol/source/Tile class to switch between different tile sources, I expect a smooth transition where the new tiles replace the old ones immediately upon loading. The expected behavior is that the previously displayed tiles remain visible until replaced by the new tiles, without any flickering or disappearance of data.
However, the actual behavior is that flickering occurs. Specifically:
-
The old tiles disappear briefly when switching to the new URL.
-
This issue happens when a stale tile from the previously cached source is used, and a flicker is observed while the new tiles load.
-
The problem appears to stem from the findStaleTile_ function in the Tile Layer code, which tries to locate and use stale tiles even when switching between different sources. This seems unnecessary, as tiles cached for the previous URL should not be considered valid for the new source.
To Reproduce
Steps to reproduce the behavior:
- Go to ‘https://codesandbox.io/p/sandbox/reusable-source-forked-x4f9p9’
- Click on the buttons to toggle between the two sources
- Pan the map slightly and observe the tiles disappearing momentarily before being replaced.
Expected behavior
Old tiles remain visible until replaced by the new tiles without flickering or glitches
Suspected Cause
I investigated the issue and found that the behavior seems to originate from the findStaleTile_ function in the Tile Layer code, the function attempts to locate previously loaded tiles when switching sources. In some cases, these tiles are displayed momentarily, leading to the flickering issue. My understanding is that this behavior might be intended for reusing tiles within the same source or for specific zoom levels, but it seems unnecessary when switching to a completely new URL. Cached tiles from the previous source should not be valid after the source URL changes.
Questions
- Is the findStaleTile_ function intended to handle scenarios where the source URL changes? If so, what is the expected behavior?
- Would it make sense to skip the use of stale tiles when the source URL is updated via setUrl?
This is my first time reporting a bug, and I’m not sure if this is the correct way to proceed or if additional steps are necessary. I am happy to contribute or provide further information, but I do not fully understand the context of how stale tiles are intended to work in OpenLayers. From my perspective, this behavior seems incompatible with the use case of switching sources using setUrl.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (10 by maintainers)
There is also a 100% API solution to meet the use case you are describing, @enieto-vex. It is as simple as setting
transition: 0on the source to avoid the tile area getting blank for a few milliseconds, andcacheSize: 0on the layer (i.e. minimal cache size, auto-growing to hold 2 zoom levels of tiles for any given viewport) to get rid of stale tiles from the previous source url at the first zoom level change.I moved your example to stackblitz since codesandbox requires an account for editing stuff. So i was missing something, just setting
getStaleKeys()is not enough, because here in therendercall the renderer will add the key back. The solution in this specific scenario seems to be to also resetrenderedSourceKey_. I edited your example to perform these two operations as a workaround (clear stalekey array and set renderedSourceKey to undefined) and it seems to work as expected now: https://stackblitz.com/edit/8t1y9wr7-znwcygda?file=main.jsNote that this has the potential to cause unexpected behavior in
prepareFrame(not in the scenario of your example tho), sinceprepareFramechecks therenderedSourceKey_to know if it should clear the tilecache (See here).An alternative workaround to this would be clearing the tilecache, but this might be not in your interest either, for example if your usecase is frequent switches back and forth.
So yeah, in my opinion there should be a flag that lets us control if the renderer should use stale tiles or not. But until this or another solution is available you can use the workaround (if you are aware of the implications)