Spaces:
Running
Running
File size: 7,153 Bytes
2de3d96 |
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# PWA on Localhost - Important Information
## Summary
**The PWA files were created successfully**, but they **won't work fully on `localhost:8501`** due to Streamlit's static file serving limitations.
---
## What You're Seeing (or Not Seeing)
### β
What DOES Work on Localhost:
1. **Game functionality**: Everything works normally
2. **Challenge Mode**: Loading `?game_id=...` works (if HF credentials configured)
3. **PWA meta tags**: Injected into HTML (check page source)
4. **Service worker registration attempt**: Runs in browser console
### β What DOESN'T Work on Localhost:
1. **`manifest.json` not accessible**:
```
http://localhost:8501/app/static/manifest.json
β Returns HTML instead of JSON (Streamlit doesn't serve /app/static/)
```
2. **Icons not accessible**:
```
http://localhost:8501/app/static/icon-192.png
β Returns 404 or HTML
```
3. **Service worker fails to register**:
```javascript
// Browser console shows:
Failed to register service worker: 404 Not Found
```
4. **No PWA install prompt**:
- No banner at bottom of screen
- No install icon in address bar
- PWA features disabled
---
## Why This Happens
**Streamlit's Static File Serving:**
- Streamlit only serves files from:
- `/.streamlit/static/` (internal Streamlit assets)
- Component assets via `declare_component()`
- NOT from arbitrary `battlewords/static/` directories
- On HuggingFace Spaces:
- `/app/static/` is mapped by HF infrastructure
- Files in `battlewords/static/` are accessible at `/app/static/`
- β
PWA works perfectly
- On localhost:
- No `/app/static/` mapping exists
- Streamlit returns HTML for all unrecognized paths
- β PWA files return 404
---
## How to Test PWA Locally
### Option 1: Use ngrok (HTTPS Tunnel) β **RECOMMENDED**
This is the **best way** to test PWA locally with full functionality:
```bash
# Terminal 1: Run Streamlit
streamlit run app.py
# Terminal 2: Expose with HTTPS
ngrok http 8501
# Output shows:
# Forwarding https://abc123.ngrok-free.app -> http://localhost:8501
```
**Then visit the HTTPS URL on your phone or desktop:**
- β
Full PWA functionality
- β
Install prompt appears
- β
manifest.json loads
- β
Service worker registers
- β
Icons display correctly
**ngrok Setup:**
1. Download: https://ngrok.com/download
2. Sign up for free account
3. Install: `unzip /path/to/ngrok.zip` (or chocolatey on Windows: `choco install ngrok`)
4. Authenticate: `ngrok config add-authtoken <your-token>`
5. Run: `ngrok http 8501`
---
### Option 2: Deploy to HuggingFace Spaces β **PRODUCTION**
PWA works out-of-the-box on HF Spaces:
```bash
git add battlewords/static/ battlewords/ui.py
git commit -m "Add PWA support"
git push
# HF Spaces auto-deploys
# Visit: https://surn-battlewords.hf.space
```
**Then test PWA:**
- Android Chrome: "Add to Home Screen" prompt appears
- iOS Safari: Share β "Add to Home Screen"
- Desktop Chrome: Install icon in address bar
β
**This is where PWA is meant to work!**
---
###Option 3: Manual Static File Server (Advanced)
You can serve the static files separately:
```bash
# Terminal 1: Run Streamlit
streamlit run app.py
# Terminal 2: Serve static files
cd battlewords/static
python3 -m http.server 8502
# Then access:
# Streamlit: http://localhost:8501
# Static files: http://localhost:8502/manifest.json
```
**Then modify the PWA paths in `ui.py`:**
```python
pwa_meta_tags = """
<link rel="manifest" href="http://localhost:8502/manifest.json">
<link rel="apple-touch-icon" href="http://localhost:8502/icon-192.png">
<!-- etc -->
"""
```
β **Not recommended**: Too complex, defeats the purpose
---
## What About Challenge Mode?
**Question:** "I loaded `localhost:8501/?game_id=hDjsB_dl` but don't see anything"
**Answer:** Challenge Mode is **separate from PWA**. You should see a blue banner at the top if:
### β
Requirements for Challenge Mode to Work:
1. **Environment variables configured** (`.env` file):
```bash
HF_API_TOKEN=hf_xxxxxxxxxxxxx
HF_REPO_ID=Surn/Storage
SPACE_NAME=Surn/BattleWords
```
2. **Valid game_id exists** in the HF repo:
- `hDjsB_dl` must be a real challenge created previously
- Check HuggingFace dataset repo: https://huggingface.co/datasets/Surn/Storage
- Look for: `games/<uid>/settings.json`
- Verify `shortener.json` has entry for `hDjsB_dl`
3. **Internet connection** (to fetch challenge data)
### If Challenge Mode ISN'T Working:
**Check browser console (F12 β Console):**
```javascript
// Look for errors:
"[game_storage] Could not resolve sid: hDjsB_dl" β Challenge not found
"Failed to load game from sid" β HF API error
"HF_API_TOKEN not configured" β Missing credentials
```
**If you see errors:**
1. Verify `.env` file exists with correct variables
2. Restart Streamlit (`Ctrl+C` and `streamlit run app.py` again)
3. Try a different `game_id` from a known challenge
4. Check HF repo has the challenge data
---
## Summary Table
| Feature | Localhost | Localhost + ngrok | HF Spaces (Production) |
|---------|-----------|-------------------|------------------------|
| **Game works** | β
| β
| β
|
| **Challenge Mode** | β
(if .env configured) | β
| β
|
| **PWA manifest loads** | β | β
| β
|
| **Service worker registers** | β | β
| β
|
| **Install prompt** | β | β
| β
|
| **Icons display** | β | β
| β
|
| **Full-screen mode** | β | β
| β
|
---
## What You Should Do
### For Development:
β
**Just develop normally on localhost**
- Game features work fine
- Challenge Mode works (if .env configured)
- PWA features won't work, but that's okay
- Test PWA when you deploy
### For PWA Testing:
β
**Use ngrok for quick local PWA testing**
- 5 minutes to setup
- Full PWA functionality
- Test on real phone
### For Production:
β
**Deploy to HuggingFace Spaces**
- PWA works automatically
- No configuration needed
- `/app/static/` path works out-of-the-box
---
## Bottom Line
**Your question:** "Should I see something at the bottom of the screen?"
**Answer:**
1. **PWA install prompt**: β Not on `localhost:8501` (Streamlit limitation)
- **Will work** on HF Spaces production deployment β
- **Will work** with ngrok HTTPS tunnel β
2. **Challenge Mode banner**: β
Should appear at TOP (not bottom)
- Check if `?game_id=hDjsB_dl` exists in your HF repo
- Check browser console for errors
- Verify `.env` has `HF_API_TOKEN` configured
The PWA implementation is **correct** and **ready for production**. It just won't work on bare localhost due to Streamlit's static file serving limitations. Once you deploy to HuggingFace Spaces, everything will work perfectly!
---
## Quick Test Command
```bash
# Check if .env is configured:
cat .env | grep HF_
# Should show:
# HF_API_TOKEN=hf_xxxxx
# HF_REPO_ID=Surn/Storage
# SPACE_NAME=Surn/BattleWords
# If missing, Challenge Mode won't work locally
```
---
**Next Steps:**
1. Test game functionality on localhost β
2. Deploy to HF Spaces for PWA testing β
3. Or install ngrok for local PWA testing β
|