Этап 5: офлайн (PWA)

- vite-plugin-pwa: манифест, иконки, app shell в precache, автообновление
- статьи и картинки кешируются при открытии (stale-while-revalidate)
- «Скачать раздел для офлайна» с прогрессом, экран «Офлайн-разделы»
- баннер «Нет сети: показана сохранённая версия от …», метка «Офлайн» у статьи
- scripts/check-offline.mjs: проверка в режиме без сети

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
deonisii
2026-10-04 12:44:50 +03:00
co-authored by Claude Opus 5.5
parent f2be0df17a
commit 79d49181a6
18 changed files with 635 additions and 7 deletions
+5 -3
View File
@@ -3,6 +3,8 @@ import { Link, Navigate, useParams } from 'react-router-dom';
import { ArticleBody, type Figure } from '../components/ArticleBody';
import { IconList, IconPrint, IconStar, IconTheme, IconUp } from '../components/Icons';
import { Lightbox } from '../components/Lightbox';
import { OfflinePill } from '../components/Offline';
import { useIsCached } from '../lib/offline';
import { PrintSheet } from '../components/PrintSheet';
import { Screen } from '../components/Shell';
import { BackLink, Notice, Sheet, Skeleton } from '../components/ui';
@@ -51,6 +53,7 @@ function ArticleView({ slug }: { slug: string }) {
const fav = useFavorites();
const theme = useTheme();
const { scrolled, progress } = useReadProgress();
const cached = useIsCached(art?.slug);
const [tocOpen, setTocOpen] = useState(false);
const [printOpen, setPrintOpen] = useState(false);
const [lightbox, setLightbox] = useState<{ figures: Figure[]; start: number } | null>(null);
@@ -184,7 +187,7 @@ function ArticleView({ slug }: { slug: string }) {
{nav.space?.name} / {section ? <Link to={`/s/${section.slug}`}>{section.name}</Link> : null} /{' '}
<strong>{nav.short(art)}</strong>
</div>
<span id="article-offline-slot-desk" />
{cached ? <OfflinePill long /> : null}
<button type="button" className="btn btn--ghost" onClick={() => fav.toggle(slug)} aria-pressed={isFav}>
{isFav ? '★ В избранном' : '☆ В избранное'}
</button>
@@ -192,14 +195,13 @@ function ArticleView({ slug }: { slug: string }) {
<IconPrint size={20} />
Печать / PDF
</button>
<span id="article-edit-slot" />
</div>
<div className="art-layout">
<article className="art">
<div className="topline art-topline">
<BackLink to={backTo}>{section?.name ?? 'Главная'}</BackLink>
<span id="article-offline-slot" />
{cached ? <OfflinePill /> : null}
</div>
<div className="art-over">{section?.name}</div>
<h1 className="art-title">{art.title}</h1>
+13
View File
@@ -180,6 +180,19 @@ export function HomePage() {
</div>
</section>
) : null}
{/* В макете этих ссылок нет: без них с телефона не попасть на экран офлайн-разделов */}
<nav className="list home-more" aria-label="Ещё">
<Link to="/offline" className="list-row">
<span className="list-row-main">
<span className="list-row-title">Офлайн-разделы</span>
<span className="list-row-sub">Скачать заранее, если на объекте нет связи</span>
</span>
<span className="list-row-chev" aria-hidden="true">
›
</span>
</Link>
</nav>
</Screen>
);
}
+29
View File
@@ -0,0 +1,29 @@
import { SectionOfflineCard } from '../components/Offline';
import { Screen } from '../components/Shell';
import { BackLink, Notice, ThemeButton } from '../components/ui';
import { cacheSupported } from '../lib/offline';
import { useNav } from '../lib/tree';
export function OfflinePage() {
const nav = useNav();
return (
<Screen tab="home">
<div className="topline">
<BackLink to="/">Главная</BackLink>
<ThemeButton />
</div>
<h1 className="off-title">Офлайн-разделы</h1>
<p className="cheat-intro">Скачайте разделы заранее: в подвалах и паркингах связи часто нет.</p>
{!cacheSupported() ? (
<Notice title="Браузер не поддерживает офлайн">
Откройте сайт по https в обычной вкладке Chrome или Safari.
</Notice>
) : (
nav.sections.map((s) => <SectionOfflineCard key={s.id} section={s} />)
)}
<p className="off-note">
Главная, шпаргалка и статьи, которые вы уже открывали, сохраняются сами. Исходные PDF офлайн недоступны.
</p>
</Screen>
);
}
+3 -2
View File
@@ -1,6 +1,8 @@
import { Link, useParams } from 'react-router-dom';
import { plural } from '@kb/shared';
import { SectionOfflineRow } from '../components/Offline';
import { Screen } from '../components/Shell';
import { cacheSupported } from '../lib/offline';
import { BackLink, Notice, Plaque, Skeleton, ThemeButton } from '../components/ui';
import { num2 } from '../lib/format';
import { articleHref, useNav } from '../lib/tree';
@@ -42,8 +44,7 @@ export function SectionPage() {
</div>
</div>
{/* Сюда на этапе 5 встаёт строка «Сохранено для офлайна» */}
<div id="section-offline-slot" data-section={section.id} />
{cacheSupported() ? <SectionOfflineRow section={section} /> : null}
<div className="art-cards">
{section.articles.map((a, i) => (