Workspace ID Routing System
The workspace ID routing system provides a flexible and consistent way to handle workspace-scoped routes across the platform. This system supports multiple workspace identifier formats, automatic resolution, and intelligent middleware-based routing.Overview
The workspace routing system consists of several key components:- URL Pattern:
[locale]/(dashboard)/[wsId]/*
- Middleware: Handles workspace ID resolution and redirects
- WorkspaceWrapper: React component for workspace validation and context
- API Routes:
[wsId]
parameter for workspace-scoped API endpoints
Workspace Identifier Types
The system supports multiple workspace identifier formats that are automatically resolved:1. UUID Format
Standard UUID workspace identifiers:2. Personal Workspace
The special identifier'personal'
resolves to the user’s personal workspace:
3. Internal Workspace
The special identifier'internal'
resolves to the root workspace (system workspace):
URL Patterns
User-Facing URLs
The actual URLs users see in their browser are clean and simple:/personal/dashboard
→ Personal workspace dashboard/550e8400-e29b-41d4-a716-446655440000/tasks
→ Specific workspace tasks/internal/settings
→ Internal workspace settings
/en/personal/dashboard
, the middleware automatically normalizes this to /personal/dashboard
for a cleaner URL experience.
File Structure vs. URLs
The file structure uses[locale]/(dashboard)/[wsId]/...
but both locale and route groups are implementation details:
- File structure:
apps/web/src/app/[locale]/(dashboard)/[wsId]/...
- Browser URL:
/[wsId]/...
(both locale and route group handled transparently) - URL normalization:
/en/personal/dashboard
becomes/personal/dashboard
in browser
How It Works
- User visits
/[wsId]/...
in browser (this is what they see and type) - Middleware detects locale from cookie/browser preferences
- Next.js internally routes to
[locale]/(dashboard)/[wsId]/...
file structure - Middleware normalizes
/en/[wsId]/...
→/[wsId]/...
(transparent to user)
API Routes
API routes follow the same clean URL pattern:/api/550e8400-e29b-41d4-a716-446655440000/task/create
/api/personal/calendar/auto-schedule
/api/internal/workspace-settings
Middleware Implementation
The middleware inapps/web/src/middleware.ts
handles workspace routing with the following logic:
1. UUID Validation
2. Path Analysis
The middleware analyzes the URL path to identify:- Presence of locale prefix
- Position of workspace ID in path
- Whether it’s a special workspace identifier
3. Root Workspace Remapping
4. Personal Workspace Detection
5. Default Workspace Redirect
For authenticated users accessing root paths, the middleware automatically redirects to their default workspace:WorkspaceWrapper Component
TheWorkspaceWrapper
component provides a standardized way to handle workspace ID resolution and validation:
Basic Usage
Component API
Features
- Automatic Resolution: Converts legacy identifiers to validated UUIDs
- Type Safety: Provides fully typed workspace objects
- Error Handling: Automatically calls
notFound()
for invalid workspaces - Loading States: Built-in Suspense support
- Role Validation: Includes user role and membership status
API Routes with [wsId]
API routes use the[wsId]
parameter to scope operations to specific workspaces:
Route Structure
Implementation Pattern
Security Considerations
- Authentication: All workspace-scoped routes require authentication
- Authorization: Use
getPermissions()
to check workspace access - Validation: Workspace ID is validated before reaching API handlers
Helper Functions
Workspace Resolution
Workspace Validation
User Default Workspace
Constants
Key Constants
Error Handling
The workspace routing system includes comprehensive error handling:Invalid Workspace ID
- Returns
404 Not Found
for non-existent workspaces - Redirects to login for unauthenticated users
Permission Errors
- Returns
404 Not Found
for workspaces user doesn’t have access to - Use
getPermissions()
for fine-grained access control
Middleware Errors
- Gracefully handles database connection errors
- Continues with normal flow if workspace checks fail
Performance Considerations
Middleware Performance
- Workspace validation happens at the edge
- Database queries are optimized with single queries
- Caching is handled by Supabase client
Component Performance
- WorkspaceWrapper uses React Suspense for loading states
- Workspace data is fetched once and passed to children
- Consider using
fallback
prop for better UX
Migration Guide
Legacy URL Handling
The system automatically handles legacy workspace URLs:/workspaces/550e8400-e29b-41d4-a716-446655440000
→/550e8400-e29b-41d4-a716-446655440000
/personal
→/personal
(already correct)/internal
→/internal
(already correct)
Component Migration
Replace manual workspace resolution with WorkspaceWrapper:Testing
Middleware Testing
Test workspace routing with different identifier types:Component Testing
Test WorkspaceWrapper with different scenarios:Troubleshooting
Common Issues
Issue: Workspace not found errors Solution: Verify the workspace ID is valid and user has access Issue: Personal workspace redirects incorrectly Solution: Check that user has a personal workspace configured Issue: Internal workspace access denied Solution: Ensure user has admin/owner role in root workspaceDebug Tips
- Check browser Network tab for failed workspace requests
- Verify workspace ID in URL is correct
- Ensure user is authenticated and has workspace access
- Check console for middleware error messages
Security
Authentication
All workspace-scoped routes require authentication. Unauthenticated requests are redirected to login.Authorization
- Personal workspaces are only accessible to their owners
- Internal workspace requires admin/owner permissions
- Regular workspaces require membership
Data Isolation
Each workspace’s data is properly isolated through:- Database-level row-level security
- API route parameter validation
- Middleware workspace ID validation
Future Enhancements
Potential Improvements
- Workspace invitation flow via URL parameters
- Workspace-specific feature flags
- Enhanced caching for workspace metadata
- Workspace-specific theming and branding
Breaking Changes
Any changes to workspace identifier formats or URL patterns should be:- Clearly documented in release notes
- Include migration guides for existing URLs
- Provide backward compatibility where possible