feat: auto-persist metadata and chapter list on first book preview visit
Some checks failed
Deploy / Deploy Production (push) Has been skipped
Deploy / Cleanup Preview (push) Has been skipped
Deploy / Deploy Preview (push) Failing after 0s
CI / Scraper / Test (pull_request) Successful in 10s
CI / Scraper / Lint (pull_request) Successful in 19s
CI / UI / Build (pull_request) Successful in 20s
CI / Scraper / Build (pull_request) Successful in 10s

When a book is not in the local DB and the preview endpoint is hit,
metadata and the chapter index are now saved to PocketBase in the
background. Subsequent visits load from the local store instead of
scraping live. Chapter text is not fetched until an explicit scrape job
is triggered.
This commit is contained in:
Admin
2026-03-06 17:09:57 +05:00
parent c0d33720e9
commit b3358ac1d2
5 changed files with 56 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ package server
// GET /api/chapter-text-preview/{slug}/{n} — scrape a single chapter text live
import (
"context"
"encoding/json"
"fmt"
"net/http"
@@ -75,6 +76,26 @@ func (s *Server) handleBookPreview(w http.ResponseWriter, r *http.Request) {
chapters = []scraper.ChapterRef{}
}
// If the book was not already in the library, persist the metadata and
// chapter list skeleton to PocketBase now so that subsequent visits load
// from the local store rather than scraping live again. Chapter text is
// NOT fetched here — that still requires an explicit scrape job.
if !inLib {
go func() {
bgCtx := context.Background()
if werr := s.store.WriteMetadata(bgCtx, meta); werr != nil {
s.log.Warn("book-preview: WriteMetadata failed (non-fatal)", "slug", slug, "err", werr)
}
if len(chapters) > 0 {
if werr := s.store.WriteChapterRefs(bgCtx, slug, chapters); werr != nil {
s.log.Warn("book-preview: WriteChapterRefs failed (non-fatal)", "slug", slug, "err", werr)
}
}
s.log.Info("book-preview: metadata+chapter list persisted", "slug", slug, "chapters", len(chapters))
}()
inLib = true // will be true by the time the client navigates back
}
resp := BookPreviewResponse{
InLib: inLib,
Meta: meta,