Install Vercel Speed Insights for project - #6
Draft
vercel[bot] wants to merge 1 commit into
Draft
Conversation
# Vercel Speed Insights Integration for FastAPI
## Summary
Successfully integrated Vercel Speed Insights into the SentinelAI FastAPI project by customizing the API documentation pages to include the Speed Insights tracking script.
## Implementation Details
### Context
This is a **FastAPI Python project** (not a JavaScript framework). Vercel Speed Insights officially supports JavaScript frameworks like Next.js, React, Vue, etc., but NOT Python/FastAPI directly. However, Speed Insights can be integrated into any HTML pages using the vanilla JavaScript/HTML5 approach documented in the Vercel Speed Insights quickstart guide.
### What Was Modified
**File: `services/api/app/main.py`**
Added the following changes:
1. **New Imports**:
- `from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html`
- `from fastapi.responses import HTMLResponse`
2. **Modified FastAPI App Configuration**:
- Disabled default `/docs` and `/redoc` routes by setting `docs_url=None` and `redoc_url=None`
- This allows us to create custom documentation pages with Speed Insights integration
3. **Created Custom Documentation Endpoints**:
- **`/docs`** - Custom Swagger UI endpoint that injects Speed Insights script
- **`/redoc`** - Custom ReDoc endpoint that injects Speed Insights script
Both endpoints inject the following Speed Insights HTML snippet before the closing `</head>` tag:
```html
<script>
window.si = window.si || function () { (window.siq = window.siq || []).push(arguments); };
</script>
<script defer src="/_vercel/speed-insights/script.js"></script>
```
This follows the official Vercel documentation for HTML5/vanilla JavaScript integration found at: https://vercel.com/docs/speed-insights/quickstart
## Why This Approach
Since this FastAPI project serves a REST API without a frontend, the only HTML pages are the automatically generated API documentation pages (Swagger UI at `/docs` and ReDoc at `/redoc`). By injecting Speed Insights into these pages, we can track:
- Developer visits to the API documentation
- Page load performance for the documentation pages
- Core Web Vitals metrics when the docs are accessed
This is the appropriate integration method for a backend API project on Vercel.
## How It Works
When deployed to Vercel:
1. The `/_vercel/speed-insights/script.js` path is automatically served by Vercel's infrastructure
2. The script collects Web Vitals data (TTFB, LCP, CLS, FCP, FID)
3. Data is sent to Vercel's analytics endpoint
4. Metrics can be viewed in the Vercel dashboard under Speed Insights
## No Package Installation Required
Unlike JavaScript frameworks, this Python implementation:
- Does NOT require installing `@vercel/speed-insights` npm package (since there's no JavaScript build process)
- Uses the HTML/JavaScript snippet method directly
- Relies on Vercel's automatic script serving when deployed
## Testing
- ✅ Python syntax validation passed
- ✅ Import statements validated successfully
- ✅ Code follows FastAPI conventions and patterns
- ✅ Existing API endpoints remain unchanged
- ✅ Only documentation pages are modified
## Next Steps
After deployment to Vercel:
1. Visit `/docs` or `/redoc` to verify the pages load correctly
2. Check browser DevTools Network tab to confirm `/_vercel/speed-insights/script.js` loads
3. View Speed Insights metrics in the Vercel dashboard after some page visits
4. Core Web Vitals data will appear in the Vercel analytics
## Notes
- The Speed Insights integration only affects the API documentation pages
- The main API endpoints (`/health`, `/v1/check-tx`) are unchanged
- Speed Insights will NOT track API endpoint calls, only HTML page loads
- For API endpoint monitoring, consider using Vercel's Analytics or a different APM solution
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Author
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
ECC bundle files are already tracked in this repository. Skipping generation of another bundle PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vercel Speed Insights Integration for FastAPI
Summary
Successfully integrated Vercel Speed Insights into the SentinelAI FastAPI project by customizing the API documentation pages to include the Speed Insights tracking script.
Implementation Details
Context
This is a FastAPI Python project (not a JavaScript framework). Vercel Speed Insights officially supports JavaScript frameworks like Next.js, React, Vue, etc., but NOT Python/FastAPI directly. However, Speed Insights can be integrated into any HTML pages using the vanilla JavaScript/HTML5 approach documented in the Vercel Speed Insights quickstart guide.
What Was Modified
File:
services/api/app/main.pyAdded the following changes:
New Imports:
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_htmlfrom fastapi.responses import HTMLResponseModified FastAPI App Configuration:
/docsand/redocroutes by settingdocs_url=Noneandredoc_url=NoneCreated Custom Documentation Endpoints:
/docs- Custom Swagger UI endpoint that injects Speed Insights script/redoc- Custom ReDoc endpoint that injects Speed Insights scriptBoth endpoints inject the following Speed Insights HTML snippet before the closing
</head>tag:This follows the official Vercel documentation for HTML5/vanilla JavaScript integration found at: https://vercel.com/docs/speed-insights/quickstart
Why This Approach
Since this FastAPI project serves a REST API without a frontend, the only HTML pages are the automatically generated API documentation pages (Swagger UI at
/docsand ReDoc at/redoc). By injecting Speed Insights into these pages, we can track:This is the appropriate integration method for a backend API project on Vercel.
How It Works
When deployed to Vercel:
/_vercel/speed-insights/script.jspath is automatically served by Vercel's infrastructureNo Package Installation Required
Unlike JavaScript frameworks, this Python implementation:
@vercel/speed-insightsnpm package (since there's no JavaScript build process)Testing
Next Steps
After deployment to Vercel:
/docsor/redocto verify the pages load correctly/_vercel/speed-insights/script.jsloadsNotes
/health,/v1/check-tx) are unchangedView Project · Speed Insights
Created by cryptofixyuup (cryptofixyuup-labs) with Vercel Agent