New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline and Lie-Fi (help please) #109

Closed
ayumihamsaki opened this Issue Mar 31, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@ayumihamsaki
Copy link

ayumihamsaki commented Mar 31, 2016

Have the following code:

global.toolbox.router.get('/(.*)', function(request, values, options) { 
  // networkFirst will attempt to return a response from the network,
  // then attempt to return a response from the cache.
  return global.toolbox.fastest(request, values, options).catch(function(error) {
    // If both the network and the cache fail, then .catch() will be triggered,
    // and we get a chance to respond with our cached fallback page.
    // This would ideally check event.request.mode === 'navigate', but that isn't supported in
    // Chrome as of M48. See https://fetch.spec.whatwg.org/#concept-request-mode
    if (request.method === 'GET' && request.headers.get('accept').includes('text/html')) {
      return toolbox.cacheOnly(new Request('offline-page'), values, options);
    }
    throw error;
  });
});

Having problems trying to add this to the above code: {networkTimeoutSeconds: 3}

Hope someone can help me, thanks.

@jakearchibald @wanderview @slightlyoff

@wibblymat

This comment has been minimized.

Copy link
Contributor

wibblymat commented Apr 1, 2016

First, I edited your post slightly for formatting, sorry about that.

Options like networkTimeoutSeconds are passed as a third parameter to the router methods like toolbox.router.get. So you would do:

toolbox.router.get(myPattern, myHandler, {networkTimeoutSeconds: 3});

Or, in the full example (but with comments removed for clarity):

global.toolbox.router.get('/(.*)', function(request, values, options) { 
  return global.toolbox.fastest(request, values, options).catch(function(error) {
    if (request.method === 'GET' && request.headers.get('accept').includes('text/html')) {
      return toolbox.cacheOnly(new Request('offline-page'), values, options);
    }
    throw error;
  });
}, {networkTimeoutSeconds: 3});

Your code already handles propagating the options between handlers.

@ayumihamsaki

This comment has been minimized.

Copy link
Author

ayumihamsaki commented Apr 1, 2016

No need to say sorry, I have just started using github and need to learn how to format things better, thank you for fixing my comment and also for your comment. I was putting the networkTimeoutSeconds inside the code just like you said, I didn't click to think you had to put it at the end. Many thanks!

@gauntface gauntface closed this Apr 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment