Cloudflare Edge Router for Multi-Project Documentation

2026-06-27MODULE: edge_infrastructure

Consolidating multiple independent software products into a cohesive documentation portal presents significant architectural friction. Instead of deploying disparate subdomains for every boilerplate repository, I engineered a highly performant, edge-deployed routing layer utilizing Cloudflare Workers. This approach transparently proxies multiple isolated Cloudflare Pages environments through a single unified endpoint.

Architecture

Source repositories: Multi-repository ecosystem (Apptork Labs).

System Specifications

  • Core Protocol: A lightweight traffic interception script executing at the network edge via Cloudflare's v8 isolate environment.
  • Routing Engine: Dynamically analyzes incoming HTTP requests and selectively reverse-proxies them to isolated .pages.dev environments based on path prefixes.
  • Build Optimization: Leverages Cloudflare Pages' native "Build watch paths" to violently terminate unnecessary rebuilds, preventing CI/CD cycle burn on non-documentation commits.

Core Capabilities

The engine executes a flawless routing and optimization pipeline:

  • Path-Based Edge Routing: Intercepts requests to docs.apptork.com/* and parses the URL vector. Requests targeted at /backend, /mobile or /frontend are intercepted, rewritten, and fetched from their underlying isolated Pages environments in under 10ms.
  • Dynamic Subpath Stripping: Native Pages environments expect traffic at the root (/). The worker automatically strips the top-level identifier (/backend) prior to executing the upstream fetch(), preventing immediate 404 anomalies.
  • Contextual Resource Resolution: By forcefully injecting the site_url: "https://docs.apptork.com/backend/" parameter directly into mkdocs.yml, the static generator is tricked into rendering absolute URIs for local assets, preventing CSS/JS failures upon edge interception.
  • Git-Aware CI/CD Optimization: Commits to the API logic should never trigger a static documentation rebuild. A pre-build shell process validates the repository delta explicitly for docs/ and mkdocs.yml targets, executing an immediate exit process (0) to cancel execution if the delta is empty.

Execution Protocol

Deployment requires strict environment configuration to ensure secure API interactions within the Cloudflare ecosystem.

  1. Static Generator Configuration: Inject the absolute URI target into the MkDocs pipeline to prevent relative asset degradation.
    site_url: "https://docs.apptork.com/backend/"
    
  2. Edge Worker Deployment: Provision the Cloudflare Worker and bind it to your primary subdomain (e.g., docs.apptork.com) utilizing Cloudflare's Custom Domains feature (do not use legacy Routes). This ensures Cloudflare automatically provisions the requisite DNS A-records and SSL certificates. The routing engine utilizes environment variables (e.g., URL_BACKEND = backend-boilerplate.pages.dev) to dynamically resolve upstream targets without requiring code deployments for new products.
    export default {
      async fetch(request, env) {
        const url = new URL(request.url);
        const path = url.pathname;
    
        // Extract the first path segment (e.g., 'backend' from '/backend/assets/...')
        const project = path.split('/')[1]; 
    
        if (!project) {
          const html = `
          <!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Apptork Docs Gateway</title>
            <meta name="description" content="Official documentation portal for Apptork boilerplates. Production-ready backends, frontends, and mobile ecosystems.">
            <meta name="robots" content="index, follow">
          </head>
          <body style="font-family: monospace; padding: 3rem; background: #0a0a0a; color: #eaeaea; max-width: 600px; margin: 0 auto;">
            <h2>Apptork Documentation Gateway</h2>
            <ul style="line-height: 2.5; list-style-type: square;">
              <li><a href="/backend/" style="color: #4ade80; text-decoration: none;">Backend Boilerplate</a></li>
              <li><a href="/frontend/" style="color: #4ade80; text-decoration: none;">Frontend Boilerplate</a></li>
              <li><a href="/mobile/" style="color: #4ade80; text-decoration: none;">Mobile Boilerplate</a></li>
            </ul>
          </body>
          </html>
          `;
          return new Response(html, { status: 200, headers: { "Content-Type": "text/html" } });
        }
    
        // Force trailing slash for the base project URL to ensure relative CSS/JS paths resolve correctly
        if (path === `/${project}`) {
          return Response.redirect(`${url.origin}/${project}/${url.search}`, 301);
        }
    
        // Dynamically resolve target hostname via Environment Variables
        const targetHost = env[`URL_${project.toUpperCase()}`];
    
        if (targetHost) {
          const targetUrl = new URL(request.url);
          targetUrl.hostname = targetHost;
          
          // Strip the top-level identifier to route to the Pages root
          targetUrl.pathname = targetUrl.pathname.substring(`/${project}`.length);
          
          return fetch(new Request(targetUrl, request));
        }
    
        return new Response('Target repository not found.', { status: 404 });
      }
    };
    
  3. CI/CD Build Exclusion: Provision the Build watch paths within the Cloudflare Pages UI (Settings > Builds & deployments > Build watch paths) to enforce strict commit evaluation.
     Build watch paths
     Include paths:
     docs/*
     docs-requirements.txt
     mkdocs.yml
    
    Execution of this command yielding 0 (clean delta) correctly aborts the heavy worker allocation.
> SYSTEM_ARCHITECTURE_ACCESS_

the underlying technology used to scale clothshift and handle 400,000+ users on past exits is available as a boilerplate.

the 'operator_arsenal' and 'root_system' packages include a built-in backend wallet and transaction system fully synchronized across mobile and frontend out of the box.