Skip to content

Commit

Permalink
Merge pull request #230 from remix-pwa/dev
Browse files Browse the repository at this point in the history
Merging `sw` fixes upstream
  • Loading branch information
ShafSpecs committed May 20, 2024
2 parents cdb9f8a + 5b868ae commit dc1ba17
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 11 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.

7 changes: 7 additions & 0 deletions packages/sw/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/sw 3.0.6-dev.1 (2024-05-20)


### Bug Fixes

* **sw:** added a "No Cache" policy to cache strategies 740b258

## @remix-pwa/sw 3.0.5 (2024-05-06)


Expand Down
2 changes: 1 addition & 1 deletion packages/sw/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/sw",
"version": "3.0.5",
"version": "3.0.6-dev.1",
"description": "Service Worker APIs and utilities for Remix PWA",
"repository": {
"type": "git",
Expand Down
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
10 changes: 10 additions & 0 deletions packages/sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## @remix-pwa/sync 3.0.1-dev.1 (2024-05-20)





### Dependencies

* **@remix-pwa/sw:** upgraded to 3.0.6-dev.1

# @remix-pwa/sync 3.0.0 (2024-05-06)


Expand Down
4 changes: 2 additions & 2 deletions packages/sync/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/sync",
"version": "3.0.0",
"version": "3.0.1-dev.1",
"description": "A Background Sync addon for Remix PWA",
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,7 +34,7 @@
"rimraf": "^5.0.5"
},
"dependencies": {
"@remix-pwa/sw": "^3.0.5",
"@remix-pwa/sw": "^3.0.6-dev.1",
"idb": "^8.0.0"
}
}
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 dc1ba17

Please sign in to comment.