Skip to content

ddttom/cloudflare-worker

Repository files navigation

Cloudflare Worker Project

Cloudflare Worker (v1.1.5) with CORS headers, JSON-LD structured data generation, Speculation Rules API, and HTML transformation features.

Key Features

  • CORS Headers - Universal cross-origin resource sharing for all responses
  • JSON-LD Generation - Automatic schema.org Article structured data from page metadata
  • Speculation Rules API - Near-instant navigation with prerender/prefetch
  • Picture Placeholder Replacement - Optimized image loading patterns
  • HTML Cleanup - Comment removal and metadata sanitization
  • Comprehensive Testing - 83 unit tests + 23 integration tests

Quick Start

# Install dependencies (in cloudflare/files/)
cd cloudflare/files && npm install

# Run unit tests
npm test

# Run integration tests
# Open cloudflare/test.html in a browser

# Local development server
npm run debug               # Starts server on port 3000

# Deploy to Cloudflare
npx wrangler deploy

Project Structure

cloudflare/
├── files/
│   ├── cloudflare-worker.js          # Main worker (v1.1.5)
│   ├── cloudflare-worker.test.js     # 83 unit tests
│   ├── test-local-html.js            # Integration test runner
│   ├── package.json                  # Dependencies & version sync
│   ├── README.md                     # Feature documentation
│   ├── TESTING.md                    # Testing guide
│   └── SETUP.md                      # Deployment guide
├── two-file-testing-tutorial.md      # Testing pattern tutorial ⭐
├── blog-post.md                      # Blog post about the project
├── cloudflare-worker-code-review.md  # Architecture review
├── test.html                         # 23 integration tests
└── test-coverage-report.md           # Coverage summary

docs/
└── for-ai/                           # Development documentation
    ├── index.md                      # Navigation hub
    └── document-relationship-mapping.md  # Cross-reference guide

Note: The root package.json and index.ts are minimal placeholders. The actual worker code and dependencies are in cloudflare/files/.

Testing

Unit Tests

cd cloudflare/files
npm test

Integration Tests

Version Management

Version is synchronized across 4 locations (enforced by git hooks):

  1. cloudflare-worker.js:20 - WORKER_VERSION constant
  2. cloudflare-worker.js:15 - @version JSDoc comment
  3. cloudflare/files/package.json:3 - version field
  4. cloudflare/test.html:395 - footer display

Current version: v1.1.5

Documentation

Worker Documentation

Development Documentation

Main Features Explained

CORS Headers

Adds universal CORS headers to all responses:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type

JSON-LD Structured Data

Automatically generates schema.org Article structured data from page metadata:

  • Extracts: og:title, og:description, og:url, og:image, author, dates
  • Supports 3 trigger mechanisms: clean metadata, legacy json-ld, existing JSON-LD
  • Automatic date formatting (2-digit/4-digit years, month names)

Speculation Rules API

Injects prerender/prefetch rules for near-instant navigation:

<script type="speculationrules">
{
  "prerender": [{"where": {"href_matches": "/*"}, "eagerness": "moderate"}],
  "prefetch": [{"where": {"href_matches": "/*"}, "eagerness": "moderate"}]
}
</script>

Bun Usage

While this project can use Bun for development, it's primarily Node.js-based for Cloudflare Worker compatibility. For Bun-specific patterns and guidelines, see CLAUDE.md.

Contributing & AI Development

Production Deployment

The worker is currently deployed at:

License

See LICENSE for details.

Additional Resources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors