You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the offline tile layer with useCache: true and useOnlyCache: true, network requests are still made for tiles before checking the cache. This happens because in the createTile method, super.createTile is called first, which initiates the network request immediately, before the cache is checked.
This issue was discovered during implementation of an offline-first map solution.
Expected Behavior
For a true offline-first approach:
Check cache first
If tile exists in cache and isn't expired, use it without making a network request
Only make network request if tile isn't in cache or is expired
Code Analysis
Current implementation in TileLayer.js:
createTile(coords,done){consttile=super.createTile(coords,done);// Network request starts hereif(this.options.useCache){this._db.get(this._getStorageKey(coords),(err,data)=>{if(data&&!err){this._setDataTile(tile,data);}// ...});}returntile;}ImpactThiscausesunnecessarynetworkrequestsevenwhentilesarecached,andresultsinerrorlogsintheconsolewhenoffline,eventhoughthecachedtilesareeventuallydisplayed.SuggestedSolutionModifythecreateTilemethodtocheckcachebeforeinitiatingany network requests:
jsCopycreateTile(coords,done){consttile=document.createElement('img');if(this.options.useCache){this._db.get(this._getStorageKey(coords),(err,data)=>{if(data&&!err){this._setDataTile(tile,data);done(null,tile);}else{// Only make network request if cache missreturnsuper.createTile(coords,done);}});}else{returnsuper.createTile(coords,done);}returntile;}Environmentleaflet.offline version: 3.1.0
Browser: LatestversionsofChrome/Firefox/Safari
OS: Windows/Mac/Linux
The text was updated successfully, but these errors were encountered:
Current Behavior
When using the offline tile layer with
useCache: true
anduseOnlyCache: true
, network requests are still made for tiles before checking the cache. This happens because in thecreateTile
method,super.createTile
is called first, which initiates the network request immediately, before the cache is checked.This issue was discovered during implementation of an offline-first map solution.
Expected Behavior
For a true offline-first approach:
Code Analysis
Current implementation in TileLayer.js:
The text was updated successfully, but these errors were encountered: