From f9a79f209eaa44d95063cc6140eac7749870b48b Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 15 Oct 2024 00:08:02 -0300 Subject: [PATCH] prevent caching of incomplete profile pages. --- internaldb.go | 4 ++-- nostr.go | 8 +++++--- render_profile.go | 14 +++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/internaldb.go b/internaldb.go index 449f4de..12a2944 100644 --- a/internaldb.go +++ b/internaldb.go @@ -152,8 +152,8 @@ func (internal *InternalDB) overwriteFollowListArchive(fla *FollowListArchive) e } func (internal *InternalDB) attachRelaysToEvent(eventId string, relays ...string) (allRelays []string) { - idb, _ := hex.DecodeString(eventId[0:16]) - if _, err := internal.DB.Upsert("cached-id", idb, TypeCachedEvent, func(t leafdb.DataType, value proto.Message) (proto.Message, error) { + idxkey, _ := hex.DecodeString(eventId[0:16]) + if _, err := internal.DB.Upsert("cached-id", idxkey, TypeCachedEvent, func(t leafdb.DataType, value proto.Message) (proto.Message, error) { var ee *CachedEvent if value == nil { ee = &CachedEvent{ diff --git a/nostr.go b/nostr.go index c957de0..bb6e90d 100644 --- a/nostr.go +++ b/nostr.go @@ -76,7 +76,7 @@ func getEvent(ctx context.Context, code string, withRelays bool) (*nostr.Event, return evt, allRelays, nil } -func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent { +func authorLastNotes(ctx context.Context, pubkey string) (lastNotes []EnhancedEvent, justFetched bool) { limit := 100 go sys.FetchProfileMetadata(ctx, pubkey) // fetch this before so the cache is filled for later @@ -87,7 +87,7 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent { Limit: limit, } - lastNotes := make([]EnhancedEvent, 0, filter.Limit) + lastNotes = make([]EnhancedEvent, 0, filter.Limit) latestTimestamp := nostr.Timestamp(0) // fetch from local store if available @@ -107,6 +107,8 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent { (len(lastNotes) < limit/5 && latestTimestamp > nostr.Now()-60*60*24*2) || (len(lastNotes) < limit/2 && latestTimestamp < nostr.Now()-60*60*24*2) { // if we didn't get enough notes then try to fetch from external relays (but do not wait for it) + justFetched = true + go func() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() @@ -138,7 +140,7 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent { }() } - return lastNotes + return lastNotes, justFetched } func relayLastNotes(ctx context.Context, hostname string, limit int) iter.Seq[*nostr.Event] { diff --git a/render_profile.go b/render_profile.go index 70173af..332c972 100644 --- a/render_profile.go +++ b/render_profile.go @@ -25,7 +25,7 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter, profile, err := sys.FetchProfileFromInput(ctx, code) if err != nil { - log.Warn().Err(err).Str("code", code).Msg("event not found on render_profile") + log.Warn().Err(err).Str("code", code).Msg("error fetching profile on render_profile") w.Header().Set("Cache-Control", "max-age=60") w.WriteHeader(http.StatusNotFound) @@ -41,13 +41,19 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter, } var lastNotes []EnhancedEvent + var cacheControl string = "max-age=86400" if !isEmbed { - lastNotes = authorLastNotes(ctx, profile.PubKey) + var justFetched bool + lastNotes, justFetched = authorLastNotes(ctx, profile.PubKey) + if justFetched && profile.Event != nil { + cacheControl = "only-if-cached" + } } + w.Header().Set("Cache-Control", cacheControl) + if isSitemap { w.Header().Add("content-type", "text/xml") - w.Header().Set("Cache-Control", "max-age=86400") w.Write([]byte(XML_HEADER)) err = SitemapTemplate.Render(w, &SitemapPage{ Host: s.Domain, @@ -56,7 +62,6 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter, }) } else if isRSS { w.Header().Add("content-type", "text/xml") - w.Header().Set("Cache-Control", "max-age=86400") w.Write([]byte(XML_HEADER)) err = RSSTemplate.Render(w, &RSSPage{ Host: s.Domain, @@ -66,7 +71,6 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter, }) } else { w.Header().Add("content-type", "text/html") - w.Header().Set("Cache-Control", "max-age=86400") nprofile := profile.Nprofile(ctx, sys, 2) params := ProfilePageParams{