Astro Integration
Spyglasses integrates with Astro applications through server-side middleware. Follow these steps to set up Spyglasses in your Astro project.
Important: SSR Required
⚠️ Astro middleware only runs in Server-Side Rendering (SSR) mode. Before installing Spyglasses, ensure your Astro app is configured for SSR:
- Install a server adapter. Astro maintains official adapters for Node.js, Netlify, Vercel, and [Cloudflare]https://docs.astro.build/en/guides/integrations-guide/cloudflare/. You can find both official and community adapters in their integrations directory. Choose the one that corresponds to your deployment environment.
- Configure your
astro.config.mjs
withoutput: 'server'
oroutput: 'hybrid'
- For hybrid mode, mark individual routes with
export const prerender = false
to enable SSR
Installation
-
Install the package:
with yarn:
with pnpm:
-
Install a server adapter (if not already installed):
For Node.js:
For Vercel:
For Netlify:
-
Add your API key: Create or update your
.env
file with your Spyglasses API key:
Configuration
Step 1: Configure SSR in Astro
Update your astro.config.mjs
to enable SSR:
Step 2: Create Middleware
Create a src/middleware.ts
(or .js
) file with the Spyglasses middleware:
Middleware Chaining
If you have existing middleware, you can chain it with Spyglasses using Astro's sequence
function:
Configuration Options
The createSpyglassesMiddleware
function accepts these configuration options:
Option | Type | Required | Description | Default |
---|---|---|---|---|
apiKey | string | Yes | Your Spyglasses API key | - |
debug | boolean | No | Enable debug logging | false |
collectorEndpoint | string | No | Custom collector endpoint | Spyglasses default |
excludePaths | string[] | No | Paths to exclude from monitoring | [] |
Advanced Configuration
SSR Modes
Astro supports three rendering modes. Spyglasses works with:
Server Mode (Recommended)
All pages are rendered on-demand:
Hybrid Mode
Most pages are pre-rendered, but you can opt specific pages into SSR:
Then in pages where you want Spyglasses to run:
Static Mode (Not Supported)
⚠️ Spyglasses does not work with fully static builds (output: 'static'
) because middleware only runs at build time, not at request time.
Adapters
Spyglasses works with all Astro server adapters:
Node.js
Vercel
Netlify
Cloudflare
Docker
If you deploy with Docker, set your API key in your Dockerfile or docker-compose:
Dockerfile:
docker-compose:
Deployment
When deploying your Astro application, make sure to add your Spyglasses API key to your environment variables:
Vercel
- Go to Project Settings > Environment Variables
- Add
SPYGLASSES_API_KEY
- Ensure you're using the Vercel adapter in
astro.config.mjs
- Deploy to apply changes
Netlify
- Go to Site Settings > Build & Deploy > Environment
- Add
SPYGLASSES_API_KEY
- Ensure you're using the Netlify adapter in
astro.config.mjs
- Trigger a new deploy
Cloudflare Pages
- Go to Workers & Pages > Your Project > Settings > Environment Variables
- Add
SPYGLASSES_API_KEY
- Ensure you're using the Cloudflare adapter
- Redeploy your application
Node.js Deployment
For self-hosted Node.js deployments:
Railway
- Add environment variables in your Railway dashboard
- Add
SPYGLASSES_API_KEY
- Trigger a new deployment
Verifying Installation
After deploying your application with Spyglasses:
- Check server logs for Spyglasses initialization (if debug enabled)
- Test SSR is working: View page source - if you see full HTML, SSR is working
- Monitor your dashboard at spyglasses.io for incoming data
- Test with a bot user agent:
Troubleshooting
Middleware Not Running
Most Common Issue: Astro is in static mode or page is pre-rendered
- Verify
output: 'server'
oroutput: 'hybrid'
inastro.config.mjs
- For hybrid mode, check pages have
export const prerender = false
- Ensure you have a server adapter installed and configured
- Check that middleware file is at
src/middleware.ts
orsrc/middleware.js
API Key Not Found
- Verify environment variable is set:
SPYGLASSES_API_KEY
- Check for typos in variable name
- Ensure deployment includes environment variables
- For local development, check
.env
file exists and is not in.gitignore
No Data in Dashboard
- Verify your API key is correct
- Enable debug mode to see detection logs
- Check server console for errors
- Ensure pages are actually using SSR (not pre-rendered)
Build Errors
- Ensure you have a server adapter installed:
npm install @astrojs/node
- Check Astro version compatibility (4.0.0 or higher recommended)
- Verify middleware file syntax is correct
TypeScript Errors
If you see TypeScript errors with the middleware:
Performance Issues
- Enable debug mode to check for errors
- Verify excludePaths configuration
- Check network latency to collector endpoint
- Monitor server response times
Adapter-Specific Issues
- Vercel: Ensure you're using
@astrojs/vercel/serverless
or@astrojs/vercel/edge
- Netlify: Ensure you're using
@astrojs/netlify
(not@astrojs/netlify/static
) - Cloudflare: Edge runtime has some limitations - test thoroughly
- Node.js: Ensure Node version is 18.0.0 or higher
Example Project Structure
Here's a complete example of a working Astro + Spyglasses setup:
src/middleware.ts:
astro.config.mjs:
.env:
Need help? Contact support@spyglasses.io