diff --git a/services/site/perf/site.bench.ts b/services/site/perf/site.bench.ts index 9c4c4851a5e..ea6f92fe91e 100644 --- a/services/site/perf/site.bench.ts +++ b/services/site/perf/site.bench.ts @@ -6,7 +6,7 @@ // Loads are returning visits: each run navigates from about:blank with the HTTP cache warm, so they // measure parsing, running and hydrating the page's code, not the network. Interactions start from a // fresh load of the home page, so each run is the first ⌘K, hover or click after a load. -import { bench, url, web, type Context, type RunCase } from "@hona/strobe" +import { bench, url, web, type Context, type PrepareContext, type RunCase } from "@hona/strobe" const desktop = { width: 1440, height: 900 } const phone = { width: 390, height: 844 } @@ -22,6 +22,17 @@ const load = (path: string): RunCase => ({ reset: (ctx) => ctx.page.goto("about:blank"), }) +// Each target's input: a desktop has a mouse, a phone a touch screen, and the site takes different +// paths for each (preloading links as they scroll into view, autoplaying card videos). Headless +// Chromium as strobe 0.14 launches it has neither and matches (hover: none), so until strobe gives it +// a mouse, point STROBE_BROWSER at a wrapper that runs Chrome with +// --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4. +// The phone target turns touch emulation on, and setup refuses a page with the wrong input. +const touch = async (ctx: PrepareContext) => { + const session = await ctx.playwright.context.newCDPSession(ctx.playwright.page) + await session.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 5 }) +} + const reload = async (ctx: Context) => { await ctx.page.goto(new URL("/", ctx.url).href) await ctx.page.idle() @@ -30,11 +41,14 @@ const reload = async (ctx: Context) => { export default bench({ targets: { desktop: web({ url: "/", viewport: desktop }), - phone: web({ url: "/", viewport: phone, scale: 3 }), + phone: web({ url: "/", viewport: phone, scale: 3, prepare: touch }), // The live site, for reference only: `--target production --blocks 5 --head .` (nothing to build). production: url("https://opencode.ai/", { viewport: desktop }), }, - async setup({ page }) { + async setup({ page, target }) { + const mouse = await page.eval(() => matchMedia("(hover: hover) and (pointer: fine)").matches) + if (mouse !== (target !== "phone")) + throw new Error(`the ${target} page ${mouse ? "has" : "lacks"} a mouse; see the input note in site.bench.ts`) await page.idle() }, cases: { @@ -44,7 +58,7 @@ export default bench({ "load-docs": { ...load("/docs/"), targets: ["desktop", "production"] }, "scroll-home": { primary: "main.work", - targets: ["desktop", "production"], + targets: ["desktop", "phone", "production"], run: ({ page }) => page.scroll("main", { by: 12000, speed: 6000 }), reset: reload, }, diff --git a/services/site/src/app.tsx b/services/site/src/app.tsx index c45ea10fcba..309c8b5294b 100644 --- a/services/site/src/app.tsx +++ b/services/site/src/app.tsx @@ -1,7 +1,7 @@ import { Link, Title, Meta } from "@solidjs/meta" -import { Router, useCurrentMatches, useLocation } from "@solidjs/router" +import { Router, useBeforeLeave, useCurrentMatches, useIsRouting, useLocation } from "@solidjs/router" import { FileRoutes } from "@solidjs/start/router" -import { Show, Suspense } from "solid-js" +import { createEffect, Show, Suspense } from "solid-js" import { isServer } from "solid-js/web" import { Favicon } from "@opencode/ui/favicon" import { Font } from "@opencode/ui/font" @@ -15,7 +15,7 @@ import { ogImage } from "~/lib/og" import { strip } from "~/lib/language" import { HeadMetaProvider, LiveMeta } from "~/component/head-meta" import { viewportContent } from "~/lib/viewport" -import { quietPreloads } from "~/lib/route-preloads" +import { quietPreloads, setNavigating } from "~/lib/route-preloads" // Before the router, so its popstate listener comes first. import "~/component/site/popstate" import { monoFontFaces } from "~/component/site/mono-fonts" @@ -50,6 +50,16 @@ function AppMeta() { // The 404 route and its /dev/404 previews (the dev server shows one for bad URLs). const notFound = () => matches().some((match) => match.route.pattern.includes("404")) preloadStrings() + // Preloaded pages' stylesheets wait for a navigation (lib/route-preloads.ts). + if (!isServer) { + const routing = useIsRouting() + // A leave that another listener blocked, or that stays on this page, never starts routing. + useBeforeLeave(() => { + setNavigating(true) + queueMicrotask(() => routing() || setNavigating(false)) + }) + createEffect(() => setNavigating(routing())) + } return ( <> opencode diff --git a/services/site/src/entry-server.tsx b/services/site/src/entry-server.tsx index 5005909f891..5889b90babd 100644 --- a/services/site/src/entry-server.tsx +++ b/services/site/src/entry-server.tsx @@ -10,7 +10,16 @@ import preloads from "virtual:i18n-preload" // Every language, before any page renders. import "~/i18n/server" -const criticalCSS = `[data-component="top"]{min-height:80px;display:flex;align-items:center}` +const criticalCSS = `[data-component="top"]{min-height:80px;display:flex;align-items:center}html[data-scroll-restore] #app{opacity:0}` + +// A reload, or a return from another site, lands on a history entry the router left a scroll position +// for (createScrollRestoration in @solidjs/router keeps them in sessionStorage by the entry's depth). +// WebKit paints the top of the page while it's still parsing and restores only once the page has +// loaded, so the reader saw the top and then a jump. When there's a position to go back to, the page +// stays hidden (its background still paints) until the document has been parsed, and the script after +// #app scrolls there before anything paints; the timer shows it anyway if that never runs. +const scrollHoldScript = `try{const n=performance.getEntriesByType("navigation")[0],d=history.state&&history.state._depth,y=n&&n.type!=="navigate"&&d!=null&&JSON.parse(sessionStorage.getItem("solid-router:scroll")||"{}")[d];if(y>0){const r=document.documentElement;r.dataset.scrollRestore=y;setTimeout(()=>delete r.dataset.scrollRestore,2000)}}catch{}` +const scrollRestoreScript = `{const r=document.documentElement,y=+r.dataset.scrollRestore;if(y){scrollTo(0,y);delete r.dataset.scrollRestore}}` // A reload in the Markdown view keeps the page hidden until the view shows (site.tsx), so the view's // code (markdown-pages.ts, loaded on first use) and its strings in the page's language are fetched @@ -51,6 +60,7 @@ export default createHandler( {/* After the charset meta: the art is over 1 KB, and the meta must come in the first 1024 bytes. */} {ssr([`\n${sourceLogo}\n`]) as unknown as Element} +