Private
Public Access
Phase 3: PWA offline caching (frontend only)
Adds a real Workbox runtime-caching strategy on top of the Phase 1 app-shell
precache: StaleWhileRevalidate (cache-and-refresh) for the five read
endpoints (rooms/mine, open rooms, room messages, room members, invites/mine)
with a bounded/expiring cache per endpoint, while /api/auth/* and all
mutations stay network-only. An OfflineBanner (navigator.onLine-driven) and
a clearer Composer status line ("Connecting..." vs "You're offline") surface
what's actually happening; api/client.ts gains a NetworkError distinct from
ApiError so a genuine cache-miss-while-offline shows a quiet empty state
instead of a red error.
Manual offline testing (backend stopped, `vite preview` against the real
production service worker) surfaced a real gap the plan hadn't accounted
for: GET /api/auth/me is intentionally NetworkOnly, but that meant
ProtectedRoute could never confirm a session while offline and always
bounced to /login -- none of the newly-cached room/message data was ever
reachable. Fixed by caching a minimal, non-sensitive "last known user" in
localStorage (lib/lastUser.ts) and having AuthContext fall back to it for
any *unconfirmed* auth check (network failure, or a down backend answering
through a live reverse proxy with its own 502/503/504 -- both happen in
real deployments, not just literal airplane-mode). Only a server-confirmed
401 still clears it and signs the user out; every real action still
re-checks the actual session cookie server-side, so this can't grant
anything -- it only keeps cached UI reachable.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -70,3 +70,7 @@
|
||||
padding: var(--sp-2) var(--sp-4) 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.chat-pane-note {
|
||||
color: var(--ds-muted);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { NetworkError } from '../api/client'
|
||||
import { getRoomMessages } from '../api/rooms'
|
||||
import { useChatSocket } from '../ws/useChatSocket'
|
||||
import type { ChatMessageEnvelope, Message, MyRoomItem, RoomMember, ServerEnvelope } from '../types'
|
||||
@@ -21,12 +22,22 @@ export function ChatPane({ room, members, isMobile, onBack, onToggleInfo, infoOp
|
||||
const [history, setHistory] = useState<Message[]>([])
|
||||
const [live, setLive] = useState<ChatMessageEnvelope[]>([])
|
||||
const [wsError, setWsError] = useState<string | null>(null)
|
||||
const [historyUnavailableOffline, setHistoryUnavailableOffline] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setHistory([])
|
||||
setLive([])
|
||||
setWsError(null)
|
||||
getRoomMessages(room.id).then(setHistory).catch((err) => setWsError(String(err)))
|
||||
setHistoryUnavailableOffline(false)
|
||||
getRoomMessages(room.id)
|
||||
.then(setHistory)
|
||||
.catch((err) => {
|
||||
if (err instanceof NetworkError) {
|
||||
setHistoryUnavailableOffline(true)
|
||||
} else {
|
||||
setWsError(String(err))
|
||||
}
|
||||
})
|
||||
}, [room.id])
|
||||
|
||||
const onMessage = useCallback((envelope: ServerEnvelope) => {
|
||||
@@ -71,6 +82,11 @@ export function ChatPane({ room, members, isMobile, onBack, onToggleInfo, infoOp
|
||||
</header>
|
||||
|
||||
{wsError && <p className="chat-pane-error">{wsError}</p>}
|
||||
{historyUnavailableOffline && (
|
||||
<p className="chat-pane-error chat-pane-note">
|
||||
Message history for this room isn't available offline yet.
|
||||
</p>
|
||||
)}
|
||||
|
||||
<MessageList messages={[...history, ...live]} members={members} />
|
||||
<Composer roomName={room.name} disabled={!connected} onSend={send} />
|
||||
|
||||
@@ -2,12 +2,18 @@
|
||||
padding: var(--sp-3) var(--sp-4);
|
||||
border-top: 1px solid var(--ds-border);
|
||||
background: var(--ds-surface);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.composer-box {
|
||||
display: flex;
|
||||
gap: var(--sp-2);
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.composer textarea {
|
||||
.composer-box textarea {
|
||||
flex: 1;
|
||||
resize: none;
|
||||
background: var(--ds-surface-2);
|
||||
@@ -20,7 +26,7 @@
|
||||
max-height: 120px;
|
||||
}
|
||||
|
||||
.composer textarea:focus {
|
||||
.composer-box textarea:focus {
|
||||
border-color: var(--ds-accent);
|
||||
}
|
||||
|
||||
@@ -42,3 +48,9 @@
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.composer-status {
|
||||
font-size: 0.76rem;
|
||||
color: var(--ds-muted);
|
||||
padding-left: 2px;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useRef, useState, type KeyboardEvent } from 'react'
|
||||
import { useOnlineStatus } from '../hooks/useOnlineStatus'
|
||||
import './Composer.css'
|
||||
|
||||
interface ComposerProps {
|
||||
@@ -10,6 +11,7 @@ interface ComposerProps {
|
||||
export function Composer({ roomName, disabled, onSend }: ComposerProps) {
|
||||
const [value, setValue] = useState('')
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
||||
const online = useOnlineStatus()
|
||||
|
||||
function autoGrow() {
|
||||
const el = textareaRef.current
|
||||
@@ -35,29 +37,34 @@ export function Composer({ roomName, disabled, onSend }: ComposerProps) {
|
||||
|
||||
return (
|
||||
<div className="composer">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
rows={1}
|
||||
value={value}
|
||||
disabled={disabled}
|
||||
onChange={(e) => {
|
||||
setValue(e.target.value)
|
||||
autoGrow()
|
||||
}}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={`Message #${roomName}`}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="composer-send"
|
||||
onClick={handleSend}
|
||||
disabled={disabled || !value.trim()}
|
||||
aria-label="Send message"
|
||||
>
|
||||
<svg width="15" height="15" viewBox="0 0 20 20" aria-hidden="true">
|
||||
<polygon points="2,2 18,10 2,18 6,10" fill="currentColor" />
|
||||
</svg>
|
||||
</button>
|
||||
<div className="composer-box">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
rows={1}
|
||||
value={value}
|
||||
disabled={disabled}
|
||||
onChange={(e) => {
|
||||
setValue(e.target.value)
|
||||
autoGrow()
|
||||
}}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={disabled ? (online ? 'Connecting…' : "You're offline") : `Message #${roomName}`}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="composer-send"
|
||||
onClick={handleSend}
|
||||
disabled={disabled || !value.trim()}
|
||||
aria-label="Send message"
|
||||
>
|
||||
<svg width="15" height="15" viewBox="0 0 20 20" aria-hidden="true">
|
||||
<polygon points="2,2 18,10 2,18 6,10" fill="currentColor" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
{disabled && (
|
||||
<div className="composer-status">{online ? 'Connecting…' : "You're offline — messages can't be sent right now"}</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
.offline-banner {
|
||||
flex: none;
|
||||
background: color-mix(in srgb, var(--ds-highlight) 16%, var(--ds-surface));
|
||||
color: var(--ds-text);
|
||||
border-bottom: 1px solid var(--ds-border);
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
padding: 6px var(--sp-4);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { useOnlineStatus } from '../hooks/useOnlineStatus'
|
||||
import './OfflineBanner.css'
|
||||
|
||||
export function OfflineBanner() {
|
||||
const online = useOnlineStatus()
|
||||
|
||||
if (online) return null
|
||||
|
||||
return (
|
||||
<div className="offline-banner" role="status">
|
||||
You're offline — showing cached data.
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -103,3 +103,11 @@
|
||||
color: var(--ds-muted);
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.sidebar-offline-note {
|
||||
color: var(--ds-muted);
|
||||
font-size: 0.82rem;
|
||||
padding: var(--sp-4);
|
||||
line-height: 1.5;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ interface SidebarProps {
|
||||
onOpenBrowse: () => void
|
||||
onOpenInvites: () => void
|
||||
inviteCount: number
|
||||
unavailableOffline?: boolean
|
||||
}
|
||||
|
||||
export function Sidebar({
|
||||
@@ -22,6 +23,7 @@ export function Sidebar({
|
||||
onOpenBrowse,
|
||||
onOpenInvites,
|
||||
inviteCount,
|
||||
unavailableOffline,
|
||||
}: SidebarProps) {
|
||||
const query = searchQuery.trim().toLowerCase()
|
||||
const filtered = query ? rooms.filter((r) => r.name.toLowerCase().includes(query)) : rooms
|
||||
@@ -67,17 +69,25 @@ export function Sidebar({
|
||||
Browse rooms
|
||||
</button>
|
||||
|
||||
{filtered.length > 0 && <div className="sidebar-section-label">Rooms</div>}
|
||||
<nav>
|
||||
{filtered.map((room, i) => (
|
||||
<RoomRow
|
||||
key={room.id}
|
||||
room={room}
|
||||
colorIndex={i}
|
||||
active={room.id === activeRoomId}
|
||||
/>
|
||||
))}
|
||||
</nav>
|
||||
{unavailableOffline ? (
|
||||
<p className="sidebar-offline-note">
|
||||
Your rooms aren't available offline yet. Reconnect to load them.
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
{filtered.length > 0 && <div className="sidebar-section-label">Rooms</div>}
|
||||
<nav>
|
||||
{filtered.map((room, i) => (
|
||||
<RoomRow
|
||||
key={room.id}
|
||||
room={room}
|
||||
colorIndex={i}
|
||||
active={room.id === activeRoomId}
|
||||
/>
|
||||
))}
|
||||
</nav>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</aside>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user