Spaces:
Sleeping
Sleeping
File size: 6,106 Bytes
f871fed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | # π Frontend Connection to Hugging Face Backend
## β
Configuration Complete!
Your Next.js frontend is now configured to connect to your Hugging Face Space backend.
---
## π Changes Made
### 1. β
Frontend Environment Configuration
**Created:** [`frontend/.env.local`](frontend/.env.local)
```env
NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space
INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space
```
This tells the frontend where to find your deployed backend API.
### 2. β
API Client Already Configured
**File:** [`frontend/src/lib/api/client.ts`](frontend/src/lib/api/client.ts)
The API client already uses dynamic configuration:
- β
Reads from `process.env.NEXT_PUBLIC_API_URL`
- β
Falls back to auto-detection
- β
Ultimate fallback to `http://127.0.0.1:5055`
**No changes needed!** The existing code will automatically use your `.env.local` settings.
### 3. β
CORS Configuration Updated
**File:** [`api/main.py`](api/main.py)
Updated CORS to allow requests from:
- β
`http://localhost:3000` (local development)
- β
`http://127.0.0.1:3000` (local development)
- β
`https://baveshraam-open-notebook.hf.space` (your HF Space)
- β
`*` (wildcard - allows any origin)
### 4. β
Hardcoded URLs Checked
All hardcoded `localhost:5055` references in the frontend are:
- β
**Fallback defaults only** (when env vars not set)
- β
**Example text in error messages** (documentation)
- β
**No action needed** - proper fallback behavior
---
## π― Next Steps
### Step 1: Deploy Backend Changes to Hugging Face
The CORS update needs to be deployed to your Hugging Face Space:
```bash
git add api/main.py
git commit -m "Update CORS for frontend connection"
git push
```
Wait for Hugging Face to rebuild (check the Space logs).
### Step 2: Start Your Frontend Locally
```bash
cd frontend
npm install # If not already done
npm run dev
```
Your frontend will start on `http://localhost:3000` and connect to:
- **Backend API:** `https://baveshraam-open-notebook.hf.space`
### Step 3: Test the Connection
1. **Open browser:** http://localhost:3000
2. **Check browser console** for API connection messages
3. **Try creating a notebook** or any API-dependent feature
4. **Check Network tab** to verify requests go to your HF Space URL
---
## π How It Works
### Configuration Priority
The frontend uses a smart fallback system:
```
1. Runtime config from /config endpoint (uses .env.local)
β
2. Build-time NEXT_PUBLIC_API_URL
β
3. Auto-detection from browser URL
β
4. Fallback to http://127.0.0.1:5055
```
### Environment Variables
| Variable | Used By | Purpose |
|----------|---------|---------|
| `NEXT_PUBLIC_API_URL` | Browser | Client-side API calls |
| `INTERNAL_API_URL` | Next.js Server | Server-side proxying |
### URL Structure
The frontend automatically constructs API URLs:
- Base URL: `https://baveshraam-open-notebook.hf.space`
- API Endpoint: `/api` (added automatically)
- Full API URL: `https://baveshraam-open-notebook.hf.space/api`
---
## π οΈ Troubleshooting
### Issue: "Failed to fetch" or CORS errors
**Solution:**
1. Verify backend is running: https://baveshraam-open-notebook.hf.space/health
2. Check backend logs for CORS rejections
3. Ensure CORS changes were deployed (check git commit)
### Issue: Frontend still connects to localhost
**Solution:**
1. Verify `.env.local` file exists in `frontend/` directory
2. Restart Next.js dev server: `npm run dev`
3. Check browser console for config messages
4. Clear browser cache and reload
### Issue: 404 errors on /api/* endpoints
**Solution:**
1. Check that backend is running: https://baveshraam-open-notebook.hf.space/api/config
2. Verify the URL doesn't have double `/api/api/`
3. Check Next.js rewrite configuration in `next.config.ts`
---
## π Environment Files Reference
### `.env.local` (active configuration)
Your current deployment settings.
### `.env.local.example` (template)
Copy this when deploying to new environments.
### `.env.example` (backend configuration)
Backend environment variables (already configured on HF Space).
---
## π Success Indicators
You'll know the connection works when:
1. β
Browser console shows: `β
[Config] Successfully loaded API config`
2. β
Network tab shows requests to `baveshraam-open-notebook.hf.space`
3. β
No CORS errors in browser console
4. β
Features like "Create Notebook" work correctly
5. β
Health check responds: https://baveshraam-open-notebook.hf.space/health
---
## π Deploy Frontend (Optional)
When ready to deploy your frontend:
### Vercel / Netlify
Add environment variables in dashboard:
```
NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space
INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space
```
### Docker
```bash
docker build -t open-notebook-frontend ./frontend
docker run -p 3000:3000 \
-e NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space \
-e INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space \
open-notebook-frontend
```
---
## π Architecture Overview
```
βββββββββββββββββββββββ
β Browser β
β localhost:3000 β
ββββββββββββ¬βββββββββββ
β NEXT_PUBLIC_API_URL
β
βββββββββββββββββββββββββββββββββββββββ
β Hugging Face Space Backend β
β baveshraam-open-notebook.hf.space β
β β
β ββββββββββββ ββββββββββββ β
β β FastAPI β ββββ β SurrealDBβ β
β ββββββββββββ ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
```
---
## π You're All Set!
Your frontend is now ready to connect to your Hugging Face deployed backend. Start the frontend with `npm run dev` and test away!
|