Skip to content

Refactor monolithic worker into modular architecture#12

Draft
Genaker with Copilot wants to merge 5 commits into
mainfrom
copilot/suggest-architecture-improvements
Draft

Refactor monolithic worker into modular architecture#12
Genaker with Copilot wants to merge 5 commits into
mainfrom
copilot/suggest-architecture-improvements

Conversation

Copilot AI commented Feb 10, 2026

Copy link
Copy Markdown

The 1,717-line monolithic FPC.js lacked separation of concerns, making testing and extension difficult. This refactors it into focused modules while maintaining full backward compatibility.

Architecture

Module structure:

src/
├── config/          # ConfigManager, constants
├── cache/           # CacheManager (CDN, KV, R2 abstraction)
├── utils/           # URLHandler, CookieHandler, Logger
├── processors/      # ResponseProcessor (ESI, PWA, speculation)
└── RequestHandler   # Request orchestration

Entry point:

// worker.js - 60 lines vs 1,717
const config = getConfigManager();
const cache = new CacheManager(config);
const processor = new ResponseProcessor(config);
const handler = new RequestHandler(config, cache, processor);

addEventListener("fetch", event => {
    event.respondWith(handler.handleRequest(event.request, event));
});

Key changes

Configuration management

  • Centralized config in ConfigManager vs 45+ globals
  • Environment variable loading with type safety
  • Single source of truth for feature flags

Cache abstraction

  • CacheManager encapsulates CDN/KV/R2 operations
  • Consistent error handling across cache backends
  • Easy to mock for testing

URL/Cookie handling

  • Static utility classes vs inline parsing scattered throughout
  • Reusable functions: normalizeUrl(), extractWorkerParams(), generateCacheKey()

Response processing

  • Clean pipeline: ESI → PWA → Speculation
  • Each processor independently toggleable
  • No cross-contamination of concerns

Testability

  • Each module testable in isolation
  • Validation suite included (17 tests)
  • Clear boundaries enable mocking

Backward compatibility

Original FPC.js unchanged and functional. Users choose:

  • Keep using FPC.js (no changes)
  • Adopt worker.js (modular)
  • Gradual migration (import specific modules)

All environment variables, URL parameters, and features preserved.

Documentation

  • ARCHITECTURE.md - Module APIs and request flow
  • MIGRATION.md - Adoption strategies
  • EXAMPLES.md - Custom implementations
  • QUICKSTART.md - Reference guide

Validation

  • Structure validation: 17/17 tests pass
  • CodeQL scan: 0 vulnerabilities
  • Zero performance impact (ES modules bundle efficiently)
Original prompt

Suggest architecture improvements and better code structure


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits February 10, 2026 08:27
Co-authored-by: Genaker <9213670+Genaker@users.noreply.github.com>
Co-authored-by: Genaker <9213670+Genaker@users.noreply.github.com>
Co-authored-by: Genaker <9213670+Genaker@users.noreply.github.com>
Co-authored-by: Genaker <9213670+Genaker@users.noreply.github.com>
Copilot AI changed the title [WIP] Suggest architecture improvements and better code structure Refactor monolithic worker into modular architecture Feb 10, 2026
Copilot AI requested a review from Genaker February 10, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants