You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

53 lines
1.9 KiB

  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {createPartialResponse} from './createPartialResponse.mjs';
  14. import './_version.mjs';
  15. /**
  16. * The range request plugin makes it easy for a request with a 'Range' header to
  17. * be fulfilled by a cached response.
  18. *
  19. * It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
  20. * and returning the appropriate subset of the cached response body.
  21. *
  22. * @memberof workbox.rangeRequests
  23. */
  24. class Plugin {
  25. /**
  26. * @param {Object} options
  27. * @param {Request} options.request The original request, which may or may not
  28. * contain a Range: header.
  29. * @param {Response} options.cachedResponse The complete cached response.
  30. * @return {Promise<Response>} If request contains a 'Range' header, then a
  31. * new response with status 206 whose body is a subset of `cachedResponse` is
  32. * returned. Otherwise, `cachedResponse` is returned as-is.
  33. *
  34. * @private
  35. */
  36. async cachedResponseWillBeUsed({request, cachedResponse}) {
  37. // Only return a sliced response if there's something valid in the cache,
  38. // and there's a Range: header in the request.
  39. if (cachedResponse && request.headers.has('range')) {
  40. return await createPartialResponse(request, cachedResponse);
  41. }
  42. // If there was no Range: header, or if cachedResponse wasn't valid, just
  43. // pass it through as-is.
  44. return cachedResponse;
  45. }
  46. }
  47. export {Plugin};