Overview
Campus is built on a modern, lightweight architecture that combines SvelteKit for the frontend with PocketBase as the backend-as-a-service. This design prioritizes rapid development, type safety, and real-time capabilities while maintaining a simple deployment model.Architecture Diagram
Core Design Patterns
1. Authentication Flow
Campus implements a secure, httpOnly cookie-based authentication system: Server-Side (hooks.server.ts:1)- HttpOnly cookies prevent XSS token theft
- Automatic token refresh on each request
- Server-to-client hydration maintains auth state
- Per-request PocketBase instances prevent state leakage
2. Request Lifecycle
3. Data Loading Strategy
Campus uses SvelteKit’s load functions for data fetching: Server Load (+page.server.ts)- Executes only on server
- Has access to
locals.pbPocketBase instance - Fetches sensitive data
- Example:
/src/routes/feed/+page.server.ts
- Runs on both server and client
- Uses client PocketBase instance
- For client-side navigation
- Example:
/src/routes/messages/+page.ts
4. File Structure
Key Architectural Decisions
Backend-as-a-Service with PocketBase
Why PocketBase?- Zero-config backend with built-in authentication
- Real-time subscriptions via WebSocket
- File storage with automatic thumbnails
- SQL-based access control rules
- Embedded SQLite database (simple deployment)
- Admin UI included
- Limited to SQLite (horizontal scaling requires replication)
- Access rules less flexible than custom middleware
- Schema changes require migrations
SvelteKit Routing
Campus leverages SvelteKit’s file-based routing: Page Routes:/feed- Global feed/spaces/[slug]- Space details/groups/[id]- Group details/events- Calendar view/materials- Resource library/profile/[username]- User profiles
/api/events- Event CRUD/api/materials- Material uploads/api/profiles- Profile management/api/threads- Messaging
State Management
Svelte Stores:currentUser- Authenticated user modelqueryCache- Client-side query cachingconnection- Network status monitoring
event.locals.pb- PocketBase clientevent.locals.user- Current userevent.locals.sessionToken- Auth token
Rate Limiting
Implemented inhooks.server.ts:30 for all mutating API requests:
Security Patterns
1. Cookie Security
2. Access Control
Enforced at the PocketBase level via collection rules:- Users can only create posts as themselves
- Space owners control membership
- Moderators can delete inappropriate content
- Private groups require membership to view
3. Input Validation
Dual-layer validation:- Client-side: Zod schemas in
lib/schemas/ - Server-side: PocketBase field validation + custom hooks
Performance Optimizations
Bundle Splitting (vite.config.ts:32)
Database Indexes
Critical indexes defined in migrations:idx_events_scope_window- Event queries by scopeidx_notifications_user_read- Unread notificationsidx_space_user_unique- Prevent duplicate memberships
Realtime Subscriptions
Selective subscriptions to minimize overhead:- Subscribe to posts in current scope (global/space/group)
- Subscribe to notifications for current user
- Unsubscribe when navigating away
Deployment Architecture
- Both apps run as separate processes
- PocketBase serves API and handles file storage
- SvelteKit adapter-node for production
- Shared file system for PocketBase uploads
- Backup
pb_data/directory regularly
Extension Points
PocketBase Hooks (pb_hooks/)
Custom server-side logic:- Pre/post CRUD operations
- Custom validation
- External integrations
- Email notifications
SvelteKit Middleware
Extendhooks.server.ts for:
- Custom authentication providers
- Request logging
- Error tracking
- Feature flags
Service Layer
Add new services inlib/services/:
- Encapsulate business logic
- Share code between client and server
- Mock for testing