Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
angus-c committed Mar 6, 2019
1 parent 7534210 commit f504b3c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export default function getHandlers(assetInfo: AssetInfo) {
onFetch: (event: FetchEvent) => {
try {
const expectsHtml = requestExpectsHtml(event.request);
debug.log(`(0) Any request: ${event.request.url}`);
if (expectsHtml) {
debug.log(`(1) HTML request: ${getRequestDetails(event.request)}`);
}
if (requestNotCacheable(expectsHtml, cacheablePaths, event)) {
// bypass service worker, use network
return;
Expand All @@ -59,6 +63,9 @@ export default function getHandlers(assetInfo: AssetInfo) {
if (expectsHtml) {
if (cacheHasExpired(cachedResponse, maxCacheDurationMs)) {
// if html cache has expired, clear all caches and refetch
debug.log(
`(2) HTML request: ${getRequestDetails(event.request)}`
);
return caches
.delete(cacheName)
.then(() =>
Expand Down Expand Up @@ -95,6 +102,8 @@ function fetchAndCache(request, expectsHtml) {
if (expectsHtml) {
// check we've got good html before caching
if (!responseIsOKAndHtml(resp)) {
debug.log(`(3a) HTML request: ${getRequestDetails(request)}`);
debug.log(`(3b) HTML response: ${JSON.stringify(resp, null, 2)}`);
debug.log(unexpectedResponseMessage(resp));
// Might be redirect due to session expiry or error
// Clear cache but still pass original response back to browser
Expand Down Expand Up @@ -156,3 +165,8 @@ function unexpectedResponseMessage(resp) {
resp.headers.get('content-type')) ||
'unknown'}`;
}

function getRequestDetails(request) {
const {url, referrer, redirect} = request;
return JSON.stringify({url, referrer, redirect}, null, 2);
}

0 comments on commit f504b3c

Please sign in to comment.