oficina / docs Public

Stop the document page's first paint from shifting, and a plain /signin forgets a stale return

The toolbar was laid out in the first paint (before ui/select.js ran)
as a content-sized flex item, and Chrome sizes a wrapping flex container
by its widest item, so the Style/Font/Size group stacked one select per
row and then collapsed: a 0.137 layout shift on about half of document
loads, on main as well. The toolbar takes the bar's width and the groups
hold one line from 40rem up; twelve reloads now measure at most 0.002.

Standalone GET /signin with no return_to now remembers the list, as
Home's /signin already did, so an abandoned deep-link attempt cannot win
over a later sign-in. The test that covers it was green under the
mutation at first (it dropped the stale cookie from its own jar) and is
fixed to carry the browser's jar.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 92e74ea310ec53cec85c0ab46a8b525e13332f54 parent 539f225
3 files changed, +59 −1
  • internal/docs/errors_test.go +26 −0
  • internal/docs/signin_return.go +10 −1
  • internal/docs/views/show.html +23 −0

CI passed — run details

diff --git a/internal/docs/errors_test.go b/internal/docs/errors_test.go
index b41f2fa..26d3b82 100644
--- a/internal/docs/errors_test.go
+++ b/internal/docs/errors_test.go
@@ -346,6 +346,32 @@ func TestSigningInReturnsToTheDeepLink(t *testing.T) {
if again := do("GET", localLinkCompletePath); again.Header().Get("Location") != "/" {
t.Fatalf("a second landing went to %q, want the list", again.Header().Get("Location"))
}
+ // An abandoned attempt does not win over a later one: a deep link
+ // remembered, then a plain /signin, lands on the list.
+ stale := httptest.NewRecorder()
+ app.Handler().ServeHTTP(stale, httptest.NewRequest(http.MethodGet, DefaultOrigin+"/signin?return_to="+url.QueryEscape(deep), nil))
+ fresh := httptest.NewRequest(http.MethodGet, DefaultOrigin+"/signin", nil)
+ for _, c := range stale.Result().Cookies() {
+ fresh.AddCookie(c)
+ }
+ freshRec := httptest.NewRecorder()
+ app.Handler().ServeHTTP(freshRec, fresh)
+ // The browser's jar: the stale attempt's cookie, replaced by whatever
+ // the plain /signin set.
+ cookies := map[string]*http.Cookie{}
+ for _, c := range append(stale.Result().Cookies(), freshRec.Result().Cookies()...) {
+ cookies[c.Name] = c
+ }
+ landing := httptest.NewRequest(http.MethodGet, DefaultOrigin+localLinkCompletePath, nil)
+ for _, c := range cookies {
+ landing.AddCookie(c)
+ }
+ landingRec := httptest.NewRecorder()
+ app.Handler().ServeHTTP(landingRec, landing)
+ if landingRec.Header().Get("Location") != "/" {
+ t.Fatalf("a plain /signin after an abandoned deep link lands on %q, want the list", landingRec.Header().Get("Location"))
+ }
+
// Signed in now: GET /signin goes straight back, with no form.
if back := do("GET", "/signin?return_to="+url.QueryEscape(deep)); back.Code != http.StatusSeeOther || back.Header().Get("Location") != deep {
t.Fatalf("already signed in: %d %q", back.Code, back.Header().Get("Location"))
diff --git a/internal/docs/signin_return.go b/internal/docs/signin_return.go
index ee12d9a..5cbfbb2 100644
--- a/internal/docs/signin_return.go
+++ b/internal/docs/signin_return.go
@@ -97,7 +97,16 @@ func (h *handlers) standaloneSignin(n *localLinkNavigation) http.HandlerFunc {
return
}
}
- if path, ok := requestedReturn(r); ok {
+ // Starting a sign-in remembers where to come back to, and a plain
+ // /signin remembers the list: a page remembered by an earlier,
+ // abandoned attempt must not win over the one being made now. The
+ // email flow's own outcomes (?sent, ?err) leave it alone. Home's
+ // /signin does the same (home_routes.go).
+ if !signinOutcome(r) {
+ path, ok := requestedReturn(r)
+ if !ok {
+ path = "/"
+ }
n.rememberReturn(w, path)
}
h.signin(w, r)
diff --git a/internal/docs/views/show.html b/internal/docs/views/show.html
index 8b6491a..e0ec203 100644
--- a/internal/docs/views/show.html
+++ b/internal/docs/views/show.html
@@ -806,8 +806,20 @@
[docs-editor]: ProseMirror's {mount: el} takes over everything
inside that element, so a toolbar rendered in there would be
replaced by the document on the first mount. */
+ /* flex: 1 1 auto is the CLS fix, measured. As a content-sized flex item
+ the toolbar was laid out in the first paint at 645px wide and three
+ rows tall at 1440px: that paint happens while the parser is still
+ running, before ui/select.js has swapped the three native selects for
+ their comboboxes, and a wrapping flex container sized to its content
+ wraps. Then it settled to one 28px row, and the paper below jumped
+ 60px: a layout shift of 0.137 on six or seven document loads in
+ twelve, on main as well as here (polish-audit/shots r2 notes). Taking
+ the bar's width from the start, it has room for the native selects
+ too; with the group rule below, the first paint is the last. */
[docs-toolbar] {
display: flex;
+ flex: 1 1 auto;
+ min-inline-size: 0;
flex-wrap: wrap;
align-items: center;
gap: var(--rst-sp-1);
@@ -837,6 +849,17 @@
align-items: center;
gap: 2px;
}
+ /* ...but only where it has to. Chrome sizes a wrapping flex container
+ by its widest item, not by the sum of them, so with wrap on, the Style,
+ Font and Size group was laid out one select per row in the first paint
+ (88px tall, before ui/select.js has run) and then collapsed to one row:
+ the 0.137 layout shift the toolbar rule above also answers. At 40rem
+ and wider the three always fit on a line, so they are held on one. */
+ @media (min-width: 40rem) {
+ [docs-toolbar-group] {
+ flex-wrap: nowrap;
+ }
+ }
/* A rule between groups rather than a wider gap: at 320px (§10.3's
reflow width) the bar wraps, and a gap that has wrapped tells a
reader nothing about which controls belong together. */