Phase 8 part 3: lost links

Each known linking page (from Bing, Plausible or added by hand) is
opened once a week, at most 100 a day, to check it still links to the
site: kept, gone (the page no longer has the link), page gone (404, 410
or moved elsewhere) or pointing at a 404 (our page it links to is gone).
SEO → Links lists the lost ones with what happened, when last seen and
what the site brought; links can be added by hand (checked at once),
checked now, or forgotten. What to write next suggests a redirect for a
link to a 404, and asking for a lost link back when it brought visitors.
Links and What to write next now work for websites without Search
Console too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 08:43:30 -07:00
co-authored by Claude Opus 5.5
parent fc7dcf1f66
commit a1409d91ad
9 changed files with 539 additions and 7 deletions
+49
View File
@@ -2,6 +2,7 @@ package web
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
@@ -10,6 +11,7 @@ import (
"testing"
"time"
"supermarket/internal/backlinks"
"supermarket/internal/oauth"
"supermarket/internal/profile"
"supermarket/internal/secrets"
@@ -203,3 +205,50 @@ func TestLinksFromBing(t *testing.T) {
}
}
}
func TestLostLinks(t *testing.T) {
var site string
links := `<a href="SITE/freescout/">FreeScout hosting</a>`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/freescout/":
fmt.Fprint(w, "<html><body>FreeScout</body></html>")
case "/their/list":
fmt.Fprint(w, "<html><body>"+strings.ReplaceAll(links, "SITE", site)+"</body></html>")
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
// Our site and theirs on the same test server, told apart by host.
site = strings.Replace(srv.URL, "127.0.0.1", "localhost", 1)
theirs := srv.URL + "/their/list"
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Acme", Websites: []profile.Website{{URL: site + "/"}}})
if rec := postForm(h, "/seo/links/add?site=0", url.Values{"source": {theirs}}); rec.Code != http.StatusSeeOther {
t.Fatalf("add: %d %s", rec.Code, rec.Body.String())
}
p, _ := profile.Load(s.DB)
ls, _ := backlinks.Links(s.DB, p.Websites[0].URL)
if len(ls) != 1 || ls[0].Status != backlinks.StatusKept || ls[0].Target != "/freescout/" {
t.Fatalf("after adding: %+v", ls)
}
if body := postForm(h, "/seo/links/add?site=0", url.Values{"source": {site + "/blog/"}}).Body.String(); !strings.Contains(body, "a page of your own site") {
t.Error("took a page of the site itself")
}
// The page now links to a page of ours that's gone: a redirect fixes it.
links = `<a href="SITE/old-page/">old</a>`
s.DB.Exec(`UPDATE backlinks SET checked_at = '2020-01-01T00:00:00Z'`)
if rec := postForm(h, "/seo/links/check?site=0", nil); !strings.Contains(rec.Header().Get("Location"), "msg=links-checked&") && !strings.Contains(rec.Header().Get("Location"), "n1=1") {
t.Errorf("check: %s", rec.Header().Get("Location"))
}
body := do(h, http.MethodGet, "/seo/links", nil, nil).Body.String()
if !strings.Contains(body, "points at a 404") || !strings.Contains(body, "/old-page/") {
t.Errorf("links page: lost link not shown")
}
if body := do(h, http.MethodGet, "/seo/next", nil, nil).Body.String(); !strings.Contains(body, "Fix with a redirect") {
t.Errorf("next: no redirect suggestion")
}
}