Skip to content

Conversation

@focuzd
Copy link
Collaborator

@focuzd focuzd commented Jan 7, 2026

This PR migrates the maintenance mode configuration from environment variables (.env) to the database (Supabase).
Changes have been tested in local development and deployed instance.

Key Changes

1. Database Schema (supabase_schema.sql)

  • Added a singleton table maintenance_config to store maintenance settings (enabled status, message, duration, contact email).

2. Core Logic (lib/maintenance.ts)

  • Updated logic to fetch configuration from Supabase instead of environment variables.
  • Implemented Caching Strategy:
    • Uses os.tmpdir() to cache the maintenance configuration, to reduce the number of api calls to Supabase.

3. API & Admin (app/api/admin/maintenance/route.ts)

  • Updated the API to read/write from the maintenance_config table using upsert.
  • Implemented Instant Cache Update: Upon a successful DB update, the API immediately updates the local instance's cache to reflect changes instantly for the administrator.

4. Frontend & Build

  • Updated app/maintenance/page.tsx to display dynamic content from the database (Message, Duration, Email).

5. Site-Wide Redirection (app/layout.tsx & middleware.ts)

  • Middleware: Injects x-current-path header to allow Server Components to know the current URL.
  • Root Layout: Checks cached maintenance status. If enabled and the user is not an Admin (and not on an allowed route), redirects to /maintenance.

⚠️ Important Note on Caching & Propagation

To prevent overwhelming the database with requests from every site visitor, the redirection logic uses a file-based cache with a Time-To-Live (TTL) of 5 minutes.

  • Immediate Updates: When an admin updates the settings, the changes are applied immediately to the database and the current server instance's cache.
  • Propagation Delay: In a serverless environment (e.g., Vercel) with multiple running instances (lambdas), other instances may continue to serve cached data for up to 5 minutes until their cache expires and they fetch fresh data from the database.
  • Configuration: This duration can be adjusted by modifying CACHE_TTL in @lib/maintenance.ts.
// lib/maintenance.ts
const CACHE_TTL = 5 * 60 * 1000 // 5 minutes

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.

1 participant