Overview
Add your Invent AI assistant to any Next.js application (App Router or Pages Router). Perfect for React-based applications with server-side rendering and static generation.Installation
App Router (Next.js 13+)
1
Create Assistant Component
Create a client component for the assistant:
2
Add to Root Layout
Import and use in your root layout:
3
Add Environment Variable
Add to your
.env.local:Pages Router (Next.js 12 and below)
1
Create Assistant Component
2
Add to _app.tsx
User Authentication
Server-Side Hash Generation
Next.js is perfect for secure hash generation with Server Components or API Routes.Using Server Components (App Router)
Hash Generation Helper
Using API Routes (Pages Router)
Integration with Auth Libraries
NextAuth.js
Clerk
TypeScript Support
Add type declarations for the custom element:Tips for Next.js
Server Components
Use Server Components for secure hash generation
Environment Variables
Keep INVENT_SECRET_KEY server-side only
TypeScript
Add type definitions for better DX
SEO Friendly
No impact on SEO or page performance
Troubleshooting
Bubble not appearing in development
Bubble not appearing in development
Solutions:
- Check that the script is loaded after the component mounts
- Verify assistant ID is correct
- Check browser console for errors
- Try clearing Next.js cache:
rm -rf .next
TypeScript errors with custom element
TypeScript errors with custom element
Solutions:
- Add type declarations file (see TypeScript Support section)
- Ensure file is included in tsconfig.json
- Restart TypeScript server
User hash not working
User hash not working
Solutions:
- Verify secret key is set in environment variables
- Ensure hash is generated server-side, not client-side
- Check that both user-id and user-hash are provided
- Validate HMAC-SHA256 implementation
Best Practices
- Always use Server Components or API Routes for hash generation
- Store secret key in
.env.local(never commit to git) - Use
NEXT_PUBLIC_prefix only for non-sensitive variables - Test authentication in both development and production
- Consider using middleware for auth logic