Skip to content

Commit

Permalink
Merge pull request #220 from remix-pwa/dev
Browse files Browse the repository at this point in the history
Moving `sw` new patch upstream
  • Loading branch information
ShafSpecs committed May 6, 2024
2 parents 9b518eb + f30268c commit a1d1740
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 411 deletions.
461 changes: 61 additions & 400 deletions package-lock.json

Large diffs are not rendered by default.

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.5-dev.1 (2024-05-06)


### Bug Fixes

* **sw:** added `ignoreRoutes` option to strategy 7ea97c0

## @remix-pwa/sw 3.0.4 (2024-05-05)


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.4",
"version": "3.0.5-dev.1",
"description": "Service Worker APIs and utilities for Remix PWA",
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions packages/sw/src/cache/BaseStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export abstract class BaseStrategy implements CacheStrategy {
return request;
}

/**
* A utility method to ensure the route is supported by the strategy.
*
* @param request - The request to check.
* @returns {boolean} `true` if the route is supported, `false` otherwise.
*/
protected isRouteSupported(request: Request): boolean {
const url = new URL(request.url);

if (this.options.ignoreRoutes && this.options.ignoreRoutes.length) {
return !this.options.ignoreRoutes.some(pattern => url.pathname.match(pattern));
}

return true;
}

/**
* Abstract method to handle requests.
* Must be implemented by subclasses.
Expand Down
2 changes: 1 addition & 1 deletion packages/sw/src/cache/CacheFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CacheFirst extends BaseStrategy {
async handleRequest(req: Request | string): Promise<Response> {
const request = this.ensureRequest(req);

if (!isHttpRequest(request)) {
if (!isHttpRequest(request) || !this.isRouteSupported(request)) {
return fetch(request);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sw/src/cache/CacheOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CacheOnly extends BaseStrategy {
async handleRequest(req: Request | string): Promise<Response> {
const request = this.ensureRequest(req);

if (!isHttpRequest(request)) {
if (!isHttpRequest(request) || !this.isRouteSupported(request)) {
return fetch(request);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sw/src/cache/NetworkFirst.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isHttpRequest } from '../utils/utils.js';
import { BaseStrategy, CACHE_TIMESTAMP_HEADER } from './BaseStrategy.js';
import { BaseStrategy } from './BaseStrategy.js';
import type { CacheOptions, CacheableResponseOptions, NetworkFriendlyOptions } from './types.js';
import { mergeHeaders } from './utils.js';

Expand All @@ -25,7 +25,7 @@ export class NetworkFirst extends BaseStrategy {
async handleRequest(req: Request | string): Promise<Response> {
const request = this.ensureRequest(req);

if (!isHttpRequest(request)) {
if (!isHttpRequest(request) || !this.isRouteSupported(request)) {
return fetch(request);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sw/src/cache/StaleWhileRevalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class StaleWhileRevalidate extends BaseStrategy {
async handleRequest(req: Request | string): Promise<Response> {
const request = this.ensureRequest(req);

if (!isHttpRequest(request)) {
if (!isHttpRequest(request) || !this.isRouteSupported(request)) {
return fetch(request);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/sw/src/cache/__test__/base-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable dot-notation */
import { afterAll, afterEach, beforeEach, describe, expect, test, vi } from 'vitest';

import { BaseStrategy } from '../BaseStrategy.js';
Expand Down Expand Up @@ -75,6 +76,15 @@ describe('BaseStrategy Testing Suite', () => {
expect(strategy._ensureRequest(urlRequest)).toBeInstanceOf(Request);
});

test('checks if the route is supported by the strategy', () => {
const strategy = new MockStrategy('test-cache', { ignoreRoutes: [/\/api\//] });
const request = new Request('https://example.com/assets/image.jpg');
const unsupportedRequest = new Request('https://example.com/api/data');

expect(strategy['isRouteSupported'](request)).toBe(true);
expect(strategy['isRouteSupported'](unsupportedRequest)).toBe(false);
});

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/sw/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
* Defines the options for cache strategies.
*/
export interface CacheOptions {
/**
* The maximum age of a cached item in seconds.
*/
maxAgeSeconds?: number;
/**
* The maximum number of items to store in the cache.
*/
maxEntries?: number;
/**
* A list of routes to ignore when caching.
*/
ignoreRoutes?: string[] | RegExp[];
}

export type CacheableResponseOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/sw/src/message/NavigationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class NavigationHandler extends MessageHandler {
private async handleNavigation(event: any) {
const { data } = event;
const { isSsr, location } = data.payload;
const documentUrl = location.pathname + location.search + location.hash;
const documentUrl: string = location.pathname + location.search + location.hash;

if (
(this.allowList.length > 0 && !this.allowList.some(pattern => documentUrl.match(pattern))) ||
Expand Down
4 changes: 1 addition & 3 deletions packages/sw/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { ActionFunction, AppLoadContext, DataFunctionArgs, LoaderFunction }
import type { ServerRouteModule } from '@remix-run/server-runtime/dist/routeModules.js';
import type { ServerRoute } from '@remix-run/server-runtime/dist/routes.js';

import type { Logger } from './logger/logger.js';

/**
* An object of unknown type for routes worker actions and loaders provided
* by the worker's `getLoadContext` function.
Expand Down Expand Up @@ -97,7 +95,7 @@ export type DefaultErrorHandler = (error: Error, args: WorkerDataFunctionArgs) =
* The `getLoadContext` function used to create a globally accessible
* `context` object for worker actions and loaders.
*/
export type GetLoadContextFunction = (event: FetchEvent) => WorkerLoadContext;
export type GetLoadContextFunction = (event: FetchEvent) => AppLoadContext;

declare global {
interface ServiceWorkerGlobalScope {
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@remix-pwa/client": "3.0.4",
"@remix-pwa/push": "2.10.0",
"@remix-pwa/sw": "3.0.3",
"@remix-pwa/sw": "3.0.4",
"@remix-pwa/sync": "2.0.2",
"@remix-pwa/worker-runtime": "2.1.1",
"@remix-run/node": "^2.8.1",
Expand Down

0 comments on commit a1d1740

Please sign in to comment.