Serverless Edge Functions
Relevant source files
The following files were used as context for generating this wiki page:
Serverless Edge Functions in this project are deployed via Supabase and Deno to handle backend logic that requires elevated permissions, external API integration, or dynamic content generation. These functions operate at the "edge," providing low-latency responses for critical tasks such as SEO optimization, automated news aggregation, and system health monitoring.
The architecture emphasizes security through Cross-Origin Resource Sharing (CORS) enforcement, rate limiting, and the use of service role keys for protected database operations. These functions bridge the gap between the client-side interface and secure backend resources like the PostgreSQL database and external RSS feeds.
Sources: supabase/functions/generate-sitemap/index.ts:1-5, supabase/functions/fetch-news/index.ts:1-10, DEPLOYMENT_GUIDE.md:1-20
Core Infrastructure and Shared Utilities
The edge functions leverage a shared directory to standardize request handling and security across different endpoints.
Request Middleware and Security
Every edge function implements standardized CORS handling to manage cross-origin requests from the frontend application. This includes handling preflight OPTIONS requests and injecting appropriate security headers into the response.
The diagram above illustrates the standard request lifecycle for an edge function, focusing on the security and validation middleware.
Sources: supabase/functions/_shared/cors.ts, supabase/functions/generate-sitemap/index.ts:31-36
Rate Limiting Logic
Functions utilize an in-memory rate limiting mechanism to prevent abuse and ensure service availability. This is particularly critical for functions that interface with external APIs or perform intensive database queries.
| Component | Description | Default Configuration |
|---|---|---|
RATE_LIMIT_WINDOW_MS | The time window for tracking requests. | 60,000ms (1 minute) |
MAX_REQUESTS_PER_WINDOW | Maximum allowed requests per IP within the window. | 10 - 20 requests |
rateLimitMap | In-memory Map storing request counts and reset timestamps per IP. | Map<string, { count, resetTime }> |
Sources: supabase/functions/generate-sitemap/index.ts:7-29, supabase/functions/fetch-news/index.ts:7-29
Functional Modules
SEO and Sitemap Generation
The generate-sitemap function dynamically builds an XML sitemap by querying the database for available properties and recent projects. This ensures that search engines always have the latest links to property listings and vendor project pages.
Data Flow:
- Fetch static routes (e.g.,
/properties,/amenities). - Query the
propertiestable for records wherestatus = 'available'. - Query the
projectstable for the top 100 records wherestatus = 'open'. - Construct the XML response using the base URL
https://coastal-charm-bnb.lovable.app.
Sources: supabase/functions/generate-sitemap/index.ts:51-86
News Aggregation Service
The fetch-news function serves as a centralized aggregator for real estate and property management news. It implements a multi-tiered fetching strategy to ensure data availability.
The diagram shows the fallback logic used to guarantee that the news feed is never empty, moving from high-priority APIs to RSS backups and finally local samples.
Sources: supabase/functions/fetch-news/index.ts:210-280
Content Sanitization
To prevent XSS and malicious injections from external RSS sources, the news service utilizes DOMPurify to strip all HTML tags and normalize text content before it is served to the frontend.
Sources: supabase/functions/fetch-news/index.ts:32-48
Verification and Monitoring
The system includes utilities to verify the health and performance of these serverless components.
Health Checks and Testing
Functional testing of edge functions is integrated into the project's testing utilities, allowing developers to measure query performance and validate environment configurations.
| Test Type | File Path | Target Metrics |
|---|---|---|
| Query Performance | src/utils/testUtilities.ts | Latency thresholds (1000ms - 2000ms) |
| Environment Validation | src/utils/testUtilities.ts | Verification of VITE_SUPABASE_URL and Keys |
| Deployment Verification | scripts/verify_deployment.sql | RLS status and Function Security Definer checks |
Sources: src/utils/testUtilities.ts:121-160, scripts/verify_deployment.sql:56-75
Summary
Serverless Edge Functions provide a secure and scalable backend layer for the Coastal Charm BnB platform. By combining shared security logic with specialized modules for news aggregation and SEO, the project maintains high performance while ensuring that sensitive database operations remain protected behind secure middleware and strict rate limiting.