Skip to content

Install Vercel Speed Insights for project - #6

Draft
vercel[bot] wants to merge 1 commit into
mainfrom
vercel/install-vercel-speed-insights-96yte7
Draft

Install Vercel Speed Insights for project#6
vercel[bot] wants to merge 1 commit into
mainfrom
vercel/install-vercel-speed-insights-96yte7

Conversation

@vercel

@vercel vercel Bot commented Sep 5, 2026

Copy link
Copy Markdown

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:

<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

View Project · Speed Insights

Created by cryptofixyuup (cryptofixyuup-labs) with Vercel Agent

# 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>
@vercel

vercel Bot commented Sep 5, 2026

Copy link
Copy Markdown
Author

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
api Ready Ready Preview Sep 5, 2026 11:34pm UTC

@ecc-tools

ecc-tools Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

ECC bundle files are already tracked in this repository. Skipping generation of another bundle PR.

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.

0 participants