Git Product home page Git Product logo

offline's Introduction


This project isn't actively maintained.


Offline

Note to users pre-0.6.0: Offline previously used a cloudfront hosted file as one of it's methods of detecting the connection status. This method is now deprecated and the image has been removed. Please upgrade to Offline 0.7.0+.

Improve the experience of your app when your users lose connection.

  • Monitors ajax requests looking for failure
  • Confirms the connection status by requesting an image or fake resource
  • Automatically grabs ajax requests made while the connection is down and remakes them after the connection is restored.
  • Simple UI with beautiful themes
  • 3kb minified and compressed

Installation

Include the javascript, one of the themes, and one of the languages on your site. You're done!

To use only the JavaScript API without a UI indicator, simply leave out the CSS file.

If you'd like to get a peek at how it looks on your site, disconnect your internet, or try out the simulator.

Advanced

Optionally, you can provide some configuration by setting Offline.options after bringing in the script.

Options (any can be provided as a function), with their defaults:

{
  // Should we check the connection status immediatly on page load.
  checkOnLoad: false,

  // Should we monitor AJAX requests to help decide if we have a connection.
  interceptRequests: true,

  // Should we automatically retest periodically when the connection is down (set to false to disable).
  reconnect: {
    // How many seconds should we wait before rechecking.
    initialDelay: 3,

    // How long should we wait between retries.
    delay: (1.5 * last delay, capped at 1 hour)
  },

  // Should we store and attempt to remake requests which fail while the connection is down.
  requests: true,

  // Should we show a snake game while the connection is down to keep the user entertained?
  // It's not included in the normal build, you should bring in js/snake.js in addition to
  // offline.min.js.
  game: false
}

Properties

Offline.check(): Check the current status of the connection.

Offline.state: The current state of the connection 'up' or 'down'

Offline.on(event, handler, context): Bind an event. Events:

  • up: The connection has gone from down to up
  • down: The connection has gone from up to down
  • confirmed-up: A connection test has succeeded, fired even if the connection was already up
  • confirmed-down: A connection test has failed, fired even if the connection was already down
  • checking: We are testing the connection
  • reconnect:started: We are beginning the reconnect process
  • reconnect:stopped: We are done attempting to reconnect
  • reconnect:tick: Fired every second during a reconnect attempt, when a check is not happening
  • reconnect:connecting: We are reconnecting now
  • reconnect:failure: A reconnect check attempt failed
  • requests:flush: Any pending requests have been remade
  • requests:capture: A new request is being held

Offline.off(event, handler): Unbind an event

Checking

By default, Offline makes an XHR request to load your /favicon.ico to check the connection. If you don't have such a file, it will 404 in the console, but otherwise work fine (even a 404 means the connection is up). You can change the URL it hits (an endpoint which will respond with a quick 204 is perfect):

Offline.options = {checks: {xhr: {url: '/connection-test'}}};

Make sure that the URL you check has the same origin as your page (the connection method, domain and port all must be the same), or you will run into CORS issues. You can add Access-Control headers to the endpoint to fix it on modern browsers, but it will still cause issues on IE9 and below.

If you do want to run tests on a different domain, try the image method. It loads an image, which are allowed to cross domains.

Offline.options = {checks: {image: {url: 'my-image.gif'}, active: 'image'}}

The one caveat is that with the image method, we can't distinguish a 404 from a genuine connection issue, so any error at all will appear to Offline as a connection issue.

Offline also includes a check called 'up' and another called 'down' which will always report being up or down respectively for testing. You can activate them by setting the active option, adding a data attribute to your script tag with the name data-simulate and value 'up' or 'down', or by setting localStorage.OFFLINE_SIMULATE to 'up' or 'down'.

Reconnect

The reconnect module automatically retests the connection periodically when it is down. A successful AJAX request will also trigger a silent recheck (if interceptRequests is not false).

You can disable the reconnect module by setting the reconnect to false. Reconnect can be configured by setting options on the reconnect setting.

Requests

The requests module holds any failed AJAX requests and, after deduping them, remakes them when the connection is restored.

You can disable it by setting the requests setting to false.

You can also set deDupBody to be true if you want deduping to also take into account the content of the request.

Dependencies

None!

Browser Support

Modern Chrome, Firefox, Safari and IE8+

Note that not all browsers (including Safari and old IE) support the offline events, forcing Offline to use less accurate methods of detection.

offline's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

offline's Issues

How to set Offline.options

I am a newbie, please let me know the code to put after the script to disable requests (false) thanks.

Texts are not modifiable

From an i18n approach texts should be modifiable in config like other parameters, for using in foreign languages. I didn't find the non minimized version to work on, so I made an issue.

Remake ajax requests on restored connection does not happening

When i go offline i see the offline ui message, try to make some requests (getting exceptions in console) and when i connect my internet connection i don't see the ajax requests being remade, although i see the message disappears. Working in Rails/Angularjs environment.

Thanks in advance.

Asynchronous support

Hi,

I'm trying to include this library into my project which uses requirejs to asynchronously download dependencies.

I've noticed that in the ui.coffee file it makes an assumption that the file will be downloaded before the domcontentloaded event is fired.

if document.addEventListener?
   document.addEventListener 'DOMContentLoaded', init, false
else
  # IE8
 _onreadystatechange = document.onreadystatechange
 document.onreadystatechange = ->
     if document.readyState is 'complete'
        init()
_onreadystatechange?(arguments...)

This is not always the case if you downloading your assets asynchronously. Was this is a deliberate design issue? Or would you be happy to change the logic so that it first checks if the DOM has already been loaded?

Help with turbolinks

Hi

can you please help to get offline work with turbolinks https://github.com/rails/turbolinks/. when i use turbolinks to fetch pages there is no offline-ui class in body tag. i know this could be a problem with how turbolinks work, but is there any work around to get it working.

Thanks

Custom image url does not use cachebreaker

Hi all,
I'm trying to set a custom image URL in the Offline.options. Just to test, I'm using the same cloudfront gif Offline uses, but with https because I host an ssl site.

Offline.options = {checks: {image: {url: 'https://dqakt69vkj09v.cloudfront.net/are-we-online.gif'}}};

The problem seems to be that, with a custom image url, there seems to be no "cachebreaker" query string, while I can see it with the default image url. This makes the browser respond a 304 - not modified and offline.js stops detecting a connection lost.

Please note that the problem is not https. Even with this gif, the cachebreaker is not sent and offline does not work:

Offline.options = {checks: {image: {url: 'http://dqakt69vkj09v.cloudfront.net/are-we-online.gif'}}};

bootstrap modal

Hello, i would like, on connection lost, fire a bootstrap 3 modal preventing user to continue. In modal there will be the connection status info.

Is it possible in some way?

Riccardo

Only last XHR request being sent

When sending multiple XRH requests during a period of no connection, only the last one is being sent to the flush function. I believe that the requests array being set on line 53 (of requests.js) is being overwritten with each pass of the for loop.

To temporarily fix this on my site, I replaced that line with:

makeRequest(request);

and removed the second for loop from the function (lines 55-58 of requests.js)

Localizing UI

Can you move localization strings from CSS and JS to one place?
Texts for ui and time before reconnecting now in separate place and this is not comfortably, especially when files not in UTF8 encoding,

Thanks!

Compatibility with angular

I found this strange bug, when I insert the offlinejs lib I get the following error from angular:

illegal access

I think one of the global window methods is causing this, but can't figure out which.

Can I completely override the default check for favicon.ico?

I have the latest version of the library and when the application first loads I'd like it to not check for favicon.ico. Is that possible?

I have reasons for asking this, but it's a side issue and not worth discussing here.

Here's the code pattern I'm using and I would expect this to only use the url I have specified:

        Offline.options = {
            checks: {
                image: {
                    url: function() {
                        return 'http://etc.something.etc/subdir/tiny-image.png?_=' + (Math.floor(Math.random() * 1000000000));
                    }
                },
                active: 'image'
            }
        }

        Offline.check();

getting offline-test-request http 404 errors

In one of my applications I keep getting the following error right after the application loads. It doesn't seem to affect the library's functionality:

HEAD http://web.local/offline-test-request/330485281 404 (Not Found)

I noticed the error corresponds to this line of code:
https://github.com/HubSpot/offline/blob/master/js/offline.js#L32

Is this a bug? Maybe this is old code from when you hosted the tiny image in the cloud?

Here's my code:

    Offline.options = {
        checks: {
            image: {
                url: function() {
                    return 'http://something.etc.etc/tiny-image.png?_=' + (Math.floor(Math.random() * 1000000000));
                }
            },
            active: 'image'
        }
    }

Consider adding support for PhoneGap

Hi, I like your project. I don't know if you're interested in supporting PhoneGap, but if you are, there are several PhoneGap/Cordova APIs that are available to more reliably determine the state of the network connection:

  • The connection.type property,
  • the online event,
  • the offline event.

Detecting PhoneGap is also quite simple:
if(window.device && window.device.cordova) { ... }

xhr.onprogress is "undefined" in IE + jQuery

I use jQuery to make my AJAX requests, and found that offline.js was not working at all in IE9, yet FF works great.

I tracked it down to line 166 in offline.js: in my case xhr.onprogress was "undefined" as opposed to null. When I added the check for undefined, it works in IE.

Also, it looks like jQuery always sets readyState to "1" before sending the XHR, so the check for 0 on line 175 may not always work. I've seen other examples simply testing for != 4.

javascript error when running with IE9

it looks like IE9 doesn't have document.body initialized when document.readyState === 'interactive'. as a result, the reference to document.body.appendChild in ui.js#render SOMETIMES has a null reference error:

render = function() {
var button, handler;
el = createFromHTML(TEMPLATE);
document.body.appendChild(el);

we fixed it by making this modification in ui.js:
if (((_ref = document.readyState) === 'interactive' || _ref === 'complete' ) && document.body) {
init();

(we understand we'd need to modify the coffeescript to do this properly, but are unfamiliar with the build process/tools at this point.)

if this fix makes sense, let us know and we can submit a proper pull request. if the fix doesn't make sense, an alternate fix or suggestions would be great.

Make "your connection is lost" last longer

How can I make the first message, that your connection is lost last longer.

I feel that it disappears and moves to the reconnecting message too quickly. It doesn't give the user enough time to read and seems suspicious.

Website download link

Hello,

I downloaded the min file from the main offline.js site, however the link on http://github.hubspot.com/offline/docs/welcome/ links to:

https://raw.github.com/HubSpot/offline/v0.4.4/offline.min.js

Which differs from:

https://raw.github.com/HubSpot/offline/master/offline.min.js

This meant it didn't work as expected and as per the offline.js site when I installed the script i.e. not correctly picking up offline/online window events. Reconnect button not showing (Chrome 30)

Cheers

Strange looping issue with IE9

Hi guys and first of all thanks for this nice script.

Strangely, even tough everything works fine on Chrome, FF, IE10 & IE11, when I try to open my site using IE9 it goes into a reconnecting loop (which starts normally with the disconnected message), downloads the image I've set up fine (IE returns a proper 200 status), but all I get is the attempting to reconnect loop forever. Same goes with the settings I've given and with the default ones.
Unfortunately I'm not a Coffeescript guy so I can't mess with the code, so I'm just asking for ideas here...
I'll try to debug using the compiled JS files (God help me debug on IE). I know that I'll burn in hell or something for even mentioning IE9, but hey... customers still use it, so we gotta play along.

Thanks in advance for any help you can provide.

offline no longer working

I've updated to the newest version so that I am no longer referencing the cloudfront asset. In the console of all my browsers, I am seeing the request at the specified intervals, but once I disconnect my internet connection, the requests continue without error (and without an OK response as well) so I never see the offline banner. What could be causing this?

Document Check url

By default, it appears to send requests to someones cloudfront endpoint.

It'd probably be good if that configuration option was documented up front so folks know there users will be making requests to an "uncontrolled" domain. I don't see a mention of that behavior anywhere in the README as it stands.

Thank you! This is great.

After updating to v0.6.0 issues

HEAD http://[my domain]/offline-test-request/498128607

404 Not Found
36ms

I am constantly getting bombarded with those errors in firebug. Is this intended? IMHO it should not show errors when checking.

{active: 'image'} missing from documentation

If you want to use an image rather than the default offline-test-request url the documentation recommends:

Offline.options = {checks: {image: {url: 'my-image.gif'}}};

If you do that, the image URL is ignored and it just keeps trying the offline-test-request url. You need to add {active: 'image'} for it to work.

By the way, I use /favicon.ico as the image.

ability to disable UI

Allow for only the requests handling without any UI indicator.
Expose a config setting to launch a function when the UI would normally change.

Help: after certain time check

Hi, (sorry for english) how to use setTimeout do check internet connection after 45sec and, if connection is up, set window.location.reload(true);, else, recheck after 10sec until connection is up again, and so, reload page.

thanks!

offline.js doesn't work at all

when i include offline.min.js and theme css to my page, and shut down the server, nothing happend when ajax request was failed. what's wrong with it?

Issue with reconnecting

"Your computer lost its internet connection" message stays up after reconnecting to the internet.

this is the options:
Offline.options = {
checks: {
xhr: {url: 'http://google.com'}
},
active: 'xhr'
};

however offlinejs still tries to work with the image approach.

Default XHR URL should be something that exists

The default URL should be something we know exists, such as an empty HTML file or similar. Error handling is usually much heavier on the web server than serving an existing static file. I'm thinking about exception handling, logging and such, which may cause unnecessary syscalls and complex error routines. Looking for a new filename every time is even worse since it forces an fsync on the server to see if the file exists, circumventing any file caching on the server.

I understand that you try to circumvent client caching with the random filename, but then it's better to include a querystring parameter such as the one used by JQuery: ?_=1382690388689.

WebSockets support

Do you manage to add a WebSockets monitoring in addition to the Ajax monitoring ?

Maybe a proxy in front of the browser websockets API is necessary. Anyway, a «send again the websockets messages» feature would be fantastic.

There should be option for sync xhr

I could not find a way to modify this at a runtime. There should definitely be an option to check in synchronous mode, for example, check(true) or something. Therefore it would be possible to use Offline to prevent some user actions while offline.

Unable to avoid remaking requests

I dont want to remake requests which fail while the connection is down. So, I tried to disable the 'requests' option in my application, but i am unable do so.
My code looks like:

<script src="../offline.min.js"></script>
<link rel="stylesheet" href="../themes/offline-theme-chrome.css" />

<script>
Offline.options={
    requests : false,
    checkOnLoad: true,
    interceptRequests: true,
    reconnect: {
        initialDelay: 3,
        delay: 32
    }
}
</script>

I have tried to modify the offline.js and gave requests: false in defaultOptions, then i acheived it.

Can you please help me in doing this?

Using in interanet

hi , i want to know can i use this in interanet that havent any internet connection and only have local connections?

Https Support

Is there any way to make the request to check the connection through https?

Thank you.

Lots of issues

I was very excited to use this, and it seems to have a lot of potential, but there are a lot of small issues:

  • No text message
  • No retry button
  • Doesn't show in FF

Also, the documentation was confusing. Some important things left out, contradictions, etc.

Will come back in a month and check it out though :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.