sw-best-practices

Configure Service Worker caching, API whitelists, and emergency rollback for Magic Web.

5.0k|557|Updated May 14, 2025
One-click install
npx skills add https://github.com/dtyq/magic --skill sw-best-practices
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sw-best-practices
Source: https://github.com/dtyq/magic/tree/main/frontend/magic-web/.agents/skills/sw-best-practices
Command: npx skills add https://github.com/dtyq/magic --skill sw-best-practices

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Service Worker misconfiguration causes cache pollution, stale API data, white screens, and login failures in production. This Skill provides the exact caching rules, file ownership map, and rollback procedures for the Magic Web Service Worker implementation so changes stay safe and verifiable.

Core Features & Use Cases

  • Cache Partitioning Rules: Defines CacheFirst buckets for hashed static assets, images, and fixed-path WASM/WebWorker resources, with explicit runtime cache exclusions.
  • Read-Only API Caching: Implements Stale-While-Revalidate for whitelisted GET endpoints with three-level request overrides via swCacheOption and strict response envelope validation (json.code === 1000).
  • Emergency Rollback SOPs: Server-side takeover of /sw.js using MAGIC_SW_MODE=kill|off and MAGIC_SW_CLEAR_CACHES to unregister workers or evict specific cache buckets.
  • Use Case: When adding a new settings API to the cache whitelist, follow SOP A to update CACHEABLE_API_RULES in sw-constants.ts, register the interceptor, then run the unit test trio and production build before submitting.

Quick Start

Ask the agent to add a new read-only API endpoint to the Service Worker cache whitelist and verify it with the required unit tests and production build.

Frequently Asked Questions about sw-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add an API endpoint to the Service Worker cache whitelist?

Append the path rule to CACHEABLE_API_RULES in sw-constants.ts, using a regex anchored with ^ and $. Ensure the calling HttpClient registers swCacheRequestInterceptor, then run the unit test trio and pnpm build to verify.

How do I disable or roll back a broken Service Worker in production?

Set MAGIC_SW_MODE=off so the server middleware serves an unregister-only /sw.js. For cache pollution, use MAGIC_SW_MODE=kill with MAGIC_SW_CLEAR_CACHES listing the buckets to delete, or ALL for a full reset.

Why is my API response not being cached by the Service Worker?

The cache plugin only stores responses where json.code equals 1000 or is undefined. Business errors returning HTTP 200 with other codes are rejected to prevent caching polluted data.

Which APIs should not be cached with Stale-While-Revalidate?

Never cache APIs with frequent writes or high consistency requirements. Use swCacheOption: "no-cache" for pull-to-refresh or post-submission fetches to bypass the SW cache entirely.

Why is the Service Worker not registering in local development?

In development the app unregisters the normal SW unless MAGIC_MOCK=true or MAGIC_FORCE_ENABLE_SW_IN_DEV=true is set. Set MAGIC_ENABLE_API_CACHE=false locally to avoid stale API reads while debugging.