docs(production): standardiserad produktionsberedskap och gap-analys

Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
2026-11-10 09:15:21 +01:00
parent 0fda8cae48
commit 2bee8eb778
6135 changed files with 4 additions and 1718997 deletions
-106
View File
@@ -1,106 +0,0 @@
'use client'
import { useEffect, useState } from 'react'
import { Button } from '@/components/ui/button'
import { SupportLink } from '@/components/ui/support-link'
/**
* App-wide error boundary (rendered from app/error.tsx). It catches any render
* error below the root layout that has no closer boundary, which includes the
* auth and onboarding segments AND their layouts (an error.tsx never catches
* its own sibling layout, only a parent boundary does).
*
* Those segments fire several Supabase auth/DB queries at the exact moment a
* session is established (BankID login -> /auth/callback -> the /select-company
* picker). A transient failure there, most often a Supabase refresh-token
* rotation race in the first request after the cookies are set, or a stale JS
* chunk right after a deploy, used to escape every boundary and hit
* app/global-error.tsx, which blanks the whole document with a bare "Nagot gick
* fel" screen for a second before the next request repainted and logged the
* user in normally.
*
* Recovery is a single hard reload, NOT React's reset(). A reload re-runs
* middleware (picking up the freshly rotated auth cookie) and fetches a fresh
* bundle (recovering a ChunkLoadError after a deploy): the same
* browser-navigation heal these transients already relied on. A soft reset()
* re-renders against the same stale server payload / bundle and just re-throws.
*
* A per-path, per-tab-session sessionStorage flag makes the auto-reload fire at
* most once per path, so a genuinely persistent error settles on the manual
* fallback instead of looping. This is a monotonic one-shot, not a time window:
* a time window can still loop if each failing render takes longer than the
* window (slow SSR that eventually throws), whereas the flag caps auto-reloads
* regardless of timing. sessionStorage is per-tab and cleared on tab close, so
* a fresh visit gets a fresh auto-recovery; a different path keeps its own flag.
*/
const RELOAD_FLAG_PREFIX = 'accounted:app-error-reloaded:'
function reloadKey(): string {
return (
RELOAD_FLAG_PREFIX +
(typeof window !== 'undefined' ? window.location.pathname : '')
)
}
// Decided once at mount via a lazy state initializer, never in an effect, so
// the auto-reload stays off React's setState-in-effect path. 'reloading'
// renders nothing while the single hard reload is issued; 'fallback' shows the
// manual UI. On the server (root layout threw during SSR) there is no window,
// so it starts in 'reloading' -> renders nothing and defers to the client,
// avoiding both a server crash and a flash in the common recover-invisibly case.
function decideInitialPhase(): 'reloading' | 'fallback' {
if (typeof window === 'undefined') return 'reloading'
try {
// Already auto-reloaded this path in this tab session: don't loop, show the
// manual fallback.
if (window.sessionStorage.getItem(reloadKey())) return 'fallback'
// Claim the one-shot and reload only if the flag actually persisted:
// writing here (not in the effect) keeps "mark reloaded" and "decide to
// reload" atomic, so a failed write (quota full / blocked) falls through to
// 'fallback' instead of reloading forever without ever recording it.
window.sessionStorage.setItem(reloadKey(), '1')
return 'reloading'
} catch {
// sessionStorage blocked or full: don't risk a reload loop, show the
// fallback so the user always has an explicit way forward.
return 'fallback'
}
}
export function AppErrorBoundary({
error,
scope,
}: {
error: Error & { digest?: string }
scope: string
}) {
const [phase] = useState<'reloading' | 'fallback'>(decideInitialPhase)
useEffect(() => {
console.error(
`[${scope}] Unhandled error${error.digest ? ` (digest ${error.digest})` : ''}:`,
error,
)
// The one-shot flag is already claimed in decideInitialPhase, so reaching
// 'reloading' guarantees it persisted: reload exactly once.
if (phase === 'reloading') window.location.reload()
}, [phase, error, scope])
// During the single automatic reload, render nothing so a transient error
// never flashes any UI at all.
if (phase === 'reloading') return null
return (
<div className="flex min-h-[50vh] flex-col items-center justify-center gap-4 px-6 text-center">
<h2 className="text-xl">Något gick fel</h2>
<p className="max-w-md text-sm text-muted-foreground">
Ett oväntat fel uppstod. Försök igen eller{' '}
<SupportLink variant="inline" subject="Oväntat fel">
kontakta support
</SupportLink>{' '}
om problemet kvarstår.
</p>
<Button onClick={() => window.location.reload()}>Försök igen</Button>
</div>
)
}
-70
View File
@@ -1,70 +0,0 @@
'use client'
import { useEffect, useState } from 'react'
import { useTranslations } from 'next-intl'
import { Button } from '@/components/ui/button'
// Inlined at build time from next.config's `env` (the deploy's commit SHA on
// Vercel; empty in dev / self-hosted, which turns the check off).
const BUILD_ID = process.env.NEXT_PUBLIC_BUILD_ID || ''
/**
* Detects when a newer deploy is live while this tab is still running an old JS
* bundle, and offers a one-click reload. This is why a just-shipped change can
* appear "missing" in a long-open tab until the whole app is reloaded.
*
* Compares the build id baked into this bundle against /api/version (the
* running deployment's id), re-checking when the tab regains focus plus a slow
* interval backstop. No-op when no build id is set.
*/
export function DeployReloadPrompt() {
const t = useTranslations('common')
const [stale, setStale] = useState(false)
useEffect(() => {
if (!BUILD_ID || stale) return
let cancelled = false
async function check() {
try {
const res = await fetch('/api/version', { cache: 'no-store' })
if (!res.ok) return
const { id } = await res.json()
if (!cancelled && id && id !== BUILD_ID) setStale(true)
} catch {
// Transient network error: ignore, the next trigger retries.
}
}
function onVisible() {
if (document.visibilityState === 'visible') check()
}
check()
document.addEventListener('visibilitychange', onVisible)
const interval = setInterval(check, 30 * 60 * 1000) // 30 min backstop
return () => {
cancelled = true
document.removeEventListener('visibilitychange', onVisible)
clearInterval(interval)
}
}, [stale])
if (!stale) return null
return (
// The wrapper spans the full width at the same z-index as the docked
// assistant panel and mounts after it, so without pointer-events-none it
// swallowed every click and tap in the strip behind it: the panel's
// composer sits exactly there, and "a new version is available" turned
// into "I can't type any more". Only the card itself takes input.
<div className="pointer-events-none fixed inset-x-0 bottom-4 z-[60] flex justify-center px-4">
<div className="pointer-events-auto flex items-center gap-3 rounded-lg border border-border bg-popover px-4 py-3 text-sm shadow-md">
<span className="text-foreground">{t('update_available')}</span>
<Button size="sm" onClick={() => window.location.reload()}>
{t('reload')}
</Button>
</div>
</div>
)
}
-28
View File
@@ -1,28 +0,0 @@
'use client'
import { useEffect } from 'react'
/**
* Mirrors the app's real background into <meta name="theme-color"> so the
* browser chrome (iOS status bar, Android address bar) always matches what
* is on screen. A static viewport color cannot: dark mode is a class toggle
* (next-themes) and color palettes swap --background at runtime via
* data-palette, both on <html>.
*/
export function ThemeColorSync() {
useEffect(() => {
const root = document.documentElement
const sync = () => {
const bg = getComputedStyle(root).getPropertyValue('--background').trim()
if (!bg) return
document
.querySelector('meta[name="theme-color"]')
?.setAttribute('content', `hsl(${bg})`)
}
sync()
const observer = new MutationObserver(sync)
observer.observe(root, { attributes: true, attributeFilter: ['class', 'data-palette'] })
return () => observer.disconnect()
}, [])
return null
}