Jump to content

Cache downloaded image for next load


Recommended Posts

well  I don't think the Phaser.Cache works the same way as Browser cache does.  

https://phaser.io/docs/2.6.2/Phaser.Cache.html#getItem

" In a typical game set-up the cache is populated once after the main game has loaded and
then used as an asset store."

So the cache is populated as soon as the game starts , meaning all the game assets that are added later are at your disposal throughout game run-time. 

For most people, the way to enable caching is to add some code to a .htaccess on your web host/server (apache.).

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

Or if you would like to apply some global rule for all kind of resources then you can do something like this (Cache-Control):

# 1 Month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

The above code is setting a cache control header depending on file type. ( max-age is in seconds )

So once you have this setup on your webserver, then after visiting your page for second time you should be able to see in your Network tab (in Developer's tools) that images are being loaded from cache rather than directly from server. Also on the server side in the access.log you should be able to see that some requests returned with result code 304 (NOT MODIFIED), meaning that the resource was loaded from cache. 

Another way to check that the caching is working is by checking the chrome://cache/ or ( about:cache for Firefox) ,where you should be able to see a list of all cached resources. 

But be aware that I wouldn't recommend caching when your game isn't fully finished , cause it's highly possible that if you do any changes to your resources it may take some time for the resources to get updated on client side. 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...