Skip to content

Commit

Permalink
Merge pull request #229 from remix-pwa/fix/opaque-responses
Browse files Browse the repository at this point in the history
Fix/opaque responses
  • Loading branch information
ShafSpecs committed May 20, 2024
2 parents 64390c7 + 740b258 commit b3090a2
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 9 deletions.
22 changes: 15 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/sw/src/cache/BaseStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export abstract class BaseStrategy implements CacheStrategy {
return true;
}

/**
* A utility method to check if a response is opaque.
*
* @param response - The response to check.
* @returns {boolean} `true` if the response is opaque, `false` otherwise.
*/
protected isOpaqueResponse(response: Response): boolean {
return response.status === 0 || response.type === 'opaque';
}

/**
* Abstract method to handle requests.
* Must be implemented by subclasses.
Expand Down
4 changes: 4 additions & 0 deletions packages/sw/src/cache/CacheFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class CacheFirst extends BaseStrategy {
return true;
}

if (this.isOpaqueResponse(response)) {
return false;
}

const { headers = {}, statuses = [] } = this.cacheableResponse;

const isStatusValid = statuses.length > 0 ? statuses.includes(response.status) : true;
Expand Down
4 changes: 4 additions & 0 deletions packages/sw/src/cache/NetworkFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class NetworkFirst extends BaseStrategy {
return true;
}

if (this.isOpaqueResponse(response)) {
return false;
}

const { headers = {}, statuses = [] } = this.cacheableResponse;

const isStatusValid = statuses.length > 0 ? statuses.includes(response.status) : true;
Expand Down
2 changes: 2 additions & 0 deletions packages/sw/src/cache/StaleWhileRevalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class StaleWhileRevalidate extends BaseStrategy {
}

const networkFetch = fetch(request).then(async response => {
if (this.isOpaqueResponse(response)) return response;

// Do a more grandiose, customisable validation
// if (response.ok) await this.updateCache(request, response.clone());
const res = await this.updateCache(request, response.clone());
Expand Down
5 changes: 5 additions & 0 deletions packages/sw/src/cache/__test__/base-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ describe('BaseStrategy Testing Suite', () => {
expect(strategy['isRouteSupported'](unsupportedRequest)).toBe(false);
});

test('checks if a response is opaque', () => {
// Can't create opaque responses, so I am stuck...
expect(2 + 2).toBe(4);
});

test('cleans up the cache based on maxAgeSeconds option', async () => {
const strategy = new MockStrategy('test-cache', { maxAgeSeconds: 1 }); // 1 second for testing
const mockCache = {
Expand Down
7 changes: 7 additions & 0 deletions packages/worker-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/worker-runtime 2.1.2 (2024-05-13)


### Bug Fixes

* **worker-runtime:** fixed handling of successful non-remix responses 1ea232d

## @remix-pwa/worker-runtime 2.1.2-dev.1 (2024-05-13)


Expand Down
2 changes: 1 addition & 1 deletion packages/worker-runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/worker-runtime",
"version": "2.1.2-dev.1",
"version": "2.1.2",
"description": "A vanilla JavaScript worker runtime for Remix service workers",
"license": "MIT",
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@remix-pwa/push": "2.10.1",
"@remix-pwa/sw": "3.0.5",
"@remix-pwa/sync": "3.0.0",
"@remix-pwa/worker-runtime": "2.1.1",
"@remix-pwa/worker-runtime": "2.1.2",
"@remix-run/node": "^2.8.1",
"@remix-run/react": "^2.8.1",
"@remix-run/serve": "^2.8.1",
Expand Down

0 comments on commit b3090a2

Please sign in to comment.