Refresh forced with gulp and browser-sync - gulp

I use browser sync and gulp to reload my page on change of every file in my project. It half works : sometimes I have to ctrl+F5 for the changes on my page to be displayed and then I just have to save for next changes and the browser sync reload is enough. Here my code :
var cache = require('gulp-cache');
var browserSync = require('browser-sync').create();
gulp.task('browser-sync', function() {
browserSync.init({
injectChanges: true,
proxy: "http://localhost:8888/project/source/"
});
gulp.watch("source/**/*").on("change", function(e) {
cache.clearAll();
return gulp.src(e.path)
.pipe(browserSync.reload({stream: true}));
});
});
I want the browser to be refresh on every change on my project (every file in every folder/subfolder).
I thought the stream parameter to true found an another topic will do the job but I still have the same problem...And the gulp clear cache doesn't change anything.
On each save, my page is refreshed but the changes not always displayed, so I have to hard refresh with a ctrl+F5.
It's a small project that's why I don't wanna use webpack. And it's on MAMP server.
Any idea ?

The most likely culprit is asset-cache. A hard reload removes any cached data from the browser, and also unregisters service workers, indexeddb, etc.
go into devtools, click the application tab, select service worker, and check the box that says, "update on reload". I am pretty sure this does a hard reload on page refresh.

Related

HTML5, how exactly does appcache work?

so ive got a file-server thats listening on my IP.
I created a test.html, test.js and a manifest.appcache.
My appcache says :
CACHE MANIFEST
CACHE:
test.html
test.js
NETWORK :
*
Meanwhile, my test.html :
<html manifest="manifest.appcache">
If i access on the test.html, everything is working, however if i want to change something in my html file and reload the page, nothing changed(the old html file is cached).
If i change something on my appcache file, for example just edit a space behind the variables and reload my page, it accepts all changes from my html file and is not ignoring the changes anymore.
Why is that?
How can i make it listen to my html file everytime i reload the page?
This is how the cache works. If you want to refresh the cache as you already said you need to change your manifest file.
You must modify the manifest file itself to inform the browser to refresh cached files.
But you can use JS to check if the cache is changed.
You can get the cache object like this: window.applicationCache
window.addEventListener('load', function(e) {
// Add an event listener to the cache
window.applicationCache
.addEventListener('updateready', function(e) {
// Check if ready to update (The update ready status is 4)
if (
window.applicationCache.status ==
window.applicationCache.UPDATEREADY) {
// Get new app chache
// window.location.reload();
window.applicationCache.swapCache()
}
}, false);
}, false);
Check the official docs here: https://html.spec.whatwg.org/#applicationcache
Please note this feature is deprecated an you shouldn't use it
From MDN:
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
What should I do?
You should be using the new Service Worker API, it is a direct replacement for the Application cache. Service workers have all the features off App Cache and even more.
For Service Worker API check these:
MDN
Google Dev
W3C (Github)

HTML5 App Cache: Manifest ist updated but files are taken from appcache one more time

I have a cache manifest with a comment in it
# Version 3.2
in order to update all the App I simply change the Version number. It works, but:
When I update the manifest, everything is updated correctly (new cache is filled) but the actual files are taken ONE more time from the (old) cache. when I reload twice everything is updated. Is this behaviour correct? Using chrome 21.
Thanks
Yes, this is the current "correct" behaviour. This is what happens:
When you just made changes to the manifest file, and you refresh the browser, this is what happens (assuming you're online)
the browser first loads back all the files in the cache
then the browser check online for your manifest file
it detects that the manifest file has changed, it will then proceed to download the new files
however, keep in mind, at this time, you will still be looking at your 'old files' because the browser has loaded the old files before going online to download the 'new files'
if at this point, if you hit refresh again (2nd time), you should get the 'new files'
This is currently the standard behaviour. Some people put some event handlers to prompt the user to do another refresh (after the 1st refresh)
Personally, I think the browser should be responsible to alert the user to make another refresh after finish downloading the new files, but right now, most people put in event handlers from the "window.applicationCache" to fire events to help manage this.
To look at an example of using window.applicationCache, go here : http://www.html5rocks.com/en/tutorials/appcache/beginner/
it's under the "Updating the Cache" section.
It is possible to instantly swap the cache as described here:
function updateSite(event) {
window.applicationCache.swapCache();
}
window.applicationCache.addEventListener('updateready', updateSite, false);

HTML5 offline appcache: Force refresh of all content?

My small HTML5 app needs to be re-deployed on a server. I understood I only had to touch the .appcache file to have every browser update to the last version of the files upon next visit.
The manifest looks like this:
CACHE MANIFEST
#Version: 201209251353
index.html
apple-touch-icon.png
css/styles.css
data/dump.bin
img/background.png
img/sprites.png
js/core.js
js/jquery-1.8.1.min.js
vid/walkthrough.mov
To "touch", I added a comment (#Version:) which I mean to be updated any time the content changes.
Strangely, some files are updated. Not all, for isntance, a colleague gets the latest core.js, but still displays an old walkthrough.mov.
Is there a simple mechanism to force update all files from the cache?
I found the following code on SO and included it in the scripts, expecting it to help. Not sure how needful it is :(
// Application cache refresh
window.addEventListener('load', function(e) {
window.applicationCache.addEventListener('updateready', function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
// Swap it in and reload the page to get the new hotness.
window.applicationCache.swapCache();
if (confirm('A new version of this site is available. Load it?')) {
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
}, false);
What expiry headers are you setting on your manifest and content?
If you've set (for example) the expiry on vid/walkthrough.mov to be one month in the future and that file is small enough to go in the browser cache (note: not the application cache) then the browser will refresh the application cache from the version in the browser cache rather than requesting it across the network again (if it's less than a month since it fetched the file).
If you've set a future expiry date on the manifest file then in some versions of some browsers this can cause an updated manifest not to be detected.
Another solution, that I am using, but is not pretty is to rename the appcache file. Then all content will be refreshed as the appcache will be lost.

Offline caching is not loading updated files

I have created a web app that can be accessed offline. However the problem I am having is getting the browser to display the new version of the page.
I can see that the files are being downloaded again:
Application Cache Downloading event
Application Cache Progress event (0 of 3) pad/jquery.min.js
Application Cache Progress event (1 of 3) pad/index.html
Application Cache Progress event (2 of 3) pad/
Application Cache Progress event (3 of 3)
Application Cache UpdateReady event
However upon a refresh of the page, the old index.html file is still being displayed.
I have set up my offline.manifest file properly and referenced it in the html tag. I am not sure what could be the problem here.
In some cases you may have to manually swap the new cache in using javascript. Here's what I have on my pages to deal with the issue:
<script>window.addEventListener('load', function(e) {window.applicationCache.addEventListener('updateready', function(e) {if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {window.applicationCache.swapCache();window.location.reload();} else {}}, false);}, false);</script>
This will automatically swap in the new cache and reload the page when it's available. You can get more info on using javascript with appcache here at html5rocks

HTML5 offline "Application Cache Error event: Manifest fetch failed (-1)"

I'm trying to write an HTML5 offline application but can't seem to get Chrome to accept the cache manifest file.
Chrome logs the following output to its console while loading the application:
Creating Application Cache with manifest http://localhost/cache.manifest
Application Cache Checking event
Application Cache Error event: Manifest fetch failed (-1) http://localhost/cache.manifest
However, if I remove all lines from the manifest file except for the first line (i.e. "CACHE MANIFEST") Chrome accepts the manifest:
Creating Application Cache with manifest http://localhost/cache.manifest
Application Cache Checking event
Application Cache Downloading event
Application Cache Progress event (0 of 0)
Application Cache Cached event
But, as soon as I add a new line to the manifest (even if that next line is empty) Chrome reverts to complaining that the fetch failed.
All files are being served locally from a Windows 7 PC via Python using SimpleHTTPServer on port 80. I've updated the types_map in %PYTHON%/Lib/mimetypes.py with the following line:
'.manifest': 'text/cache-manifest',
The manifest should contain the following:
CACHE MANIFEST
scripts/africa.js
scripts/main.js
scripts/offline.js
scripts/libs/raphael-min.js
favicon.ico
apple-touch-icon.png
To cache a website offline (HTML5) you need to specify all the files needed for it to run. In short specify the site main components needed.
Easy way to create a manifest is in Note Pad.
Note: CACHE MANIFEST needs to be first line and your files will follow after a line space as follows:
CACHE MANIFEST
Scripts/script.js
Content/Site.css
Scripts/jquery-ui-1.8.20.min.js
Scripts/modernizr-2.5.3.js
SESOL.png
Scripts/jquery.formatCurrency-1.4.0.min.js
http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css
http://code.jquery.com/jquery-1.8.2.min.js
http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js
Content/themes/images/icons-18-white.png
Controllers/AccountController
Controllers/HomeController
Models/AccountModels
Account/Login
Home/CheckOut
Note2: remove all spaces after each line.
Note:3 you need to follow the exact format FOLDER/File or FOLDER/FOLDER/FILE ect....
Just because you have a manifest file doesnt mean it will load. you need to add the following to the Tag:
<html manifest="~/cache.manifest" type="text/cache-manifest">
Don't forget that after you add this it's cached the first time the page loads. So you need to register a cache event in the 'mobileinit' event.
$(document).on("mobileinit", function () {
//register event to cache site for offline use
cache = window.applicationCache;
cache.addEventListener('updateready', cacheUpdatereadyListener, false);
cache.addEventListener('error', cacheErrorListener, false);
function cacheUpdatereadyListener (){
window.applicationCache.update();
window.applicationCache.swapCache();
}
function cacheErrorListener() {
alert('site not availble offline')
}
}
Download Safari and use the web inspector to find errors.
http://developer.apple.com/library/safari/#documentation/appleapplications/Conceptual/Safari_Developer_Guide/1Introduction/Introduction.html#//apple_ref/doc/uid/TP40007874-CH1-SW1
Tip: Chrome's developer tools "F12" will show you the errors in the manifest load. ie the files you still need to add.
Hope this helps, covers the entire process. I assuming if you are at this stage in development you new to add these to the mobile init:
$.mobile.allowCrossDomainPages = true; // cross domain page loading
$.mobile.phonegapNavigationEnabled = true; //Android enabled mobile
$.mobile.page.prototype.options.domCache = true; //page caching prefech rendering
$.support.touchOverflow = true; //Android enhanced scrolling
$.mobile.touchOverflowEnabled = true; // enhanced scrolling transition availible in iOS 5
Safari Developer Guide:
https://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/Client-SideStorage/Client-SideStorage.html#//apple_ref/doc/uid/TP40002051-CH4-SW4
Have you tried anything like https://manifest-validator.appspot.com/ to validate your manifest?
I've struggled with my manifest file for quite a while, it is really hard to pinpoint what is wrong. Could be something as simple as wrong encoding to an extra line break at the start.
Today I experienced exactly the same problem. After hours of working I came the the key point: the format of manifest file. In short, the file must begin a new line ONLY with ascii(0A), not ascii(0D), or ascii(0D + 0A). Only in this way can I live with Chrome, or I will get a blank page, and the error info in the console window.
According to w3c, (http://www.w3.org/TR/html5/offline.html), in “5.6.3.2 Writing cache manifests”,both 0A, 0D and 0D + 0A are all acceptable. So, my opinion is: Chrome is not compatible with w3c in the point.
Further more, say, if myapp.js is to be cached, it MUST follow the same rule: begins a new line only with ascii(0A), or Chrome will throw the same info in the console windows.
My Chrome is 13.0.782.107
I have now resolved this issue by switching to CherryPy for serving these files :)
If anyone else becomes similarly stuck but wants to keep the server part simple, the following Python may be sufficient for getting started:
import cherrypy
class SimpleStaticServer:
#cherrypy.expose
def index(self):
return '<html><body>Go to the static index page</body></html>'
cherrypy.config.update({
# global
'server.socket_host': '192.168.0.3',
'server.socket_port': 80,
# /static
'tools.staticdir.on': True,
'tools.staticdir.dir': "(directory where static files are stored)",
})
cherrypy.quickstart(SimpleStaticServer())
If you want to visit the "site" from another device, you'll need to use the external IP address (for me this was 192.168.0.3). Otherwise, you can just use '127.0.0.1' for the 'server.socket_host' value. I then point my browser to http://192.168.0.3/index.html to get my static index page.
I have resolved this issue in visual studio for MVC application.
follow below steps:
I have created .appcache file in notepad and copy manifest file content into it.
(you don't need to create .Manifest file OR not create Manifest.cshtml view. just create .appcache file in notepad.)
give reference as
<html manifest="~/example.appcache"> in view
and issue will be resolved
I think the line
CACHE:
is missing in the manifest file (should be the 2nd line, before the list of files.