diff --git a/services/site/script/i18n-slices.ts b/services/site/script/i18n-slices.ts index bf98b749d86..6dc138d419b 100644 --- a/services/site/script/i18n-slices.ts +++ b/services/site/script/i18n-slices.ts @@ -1,11 +1,11 @@ // Splits the translations by where the browser needs them, for the static pages' script // (script/static-client.ts, i18nSlices). The source files stay as they are (src/i18n/.ts, -// src/i18n/content/.ts); this reads them and the app's import graph and works out, for each +// src/i18n/content/.ts); this reads them and the script's import graph and works out, for each // locale: // -// - the shell slice: the dictionary keys and page copy the code every page loads can show -// (entry-client.tsx and app.tsx with their static imports), and what most page routes show; -// - one slice per lazy boundary, a module the browser loads on demand (each island and overlay, any +// - the shell slice: the dictionary keys and page copy the script every page loads can show +// (src/client/entry.ts with its static imports), and what most page routes show; +// - one slice per lazy boundary, a module the script loads on demand (each island and overlay, any // module only ever loaded with import()): what its static imports can show, minus the shell; // - the full file: everything any client code can show, for switching to the language in place. // @@ -114,30 +114,31 @@ function closure(roots: string[], dynamic = false) { const gather = (files: Iterable, field: "keys" | "areas") => new Set([...files].flatMap((file) => [...scan(file)[field]])) -const shellRoots = [path.join(src, "entry-client.tsx"), path.join(src, "app.tsx")] const routes = [...new Bun.Glob("routes/**/*.tsx").scanSync(src)] .map((file) => path.join(src, file)) .filter((file) => /export default/.test(readFileSync(file, "utf8"))) -// The static pages' script (src/client/entry.ts) too, so what its islands and overlays load is a boundary. -const client = closure([...shellRoots, ...routes, path.join(src, "client/entry.ts")], true) -// Lazy boundaries: every module the static pages' script loads with import(), even one that is also -// imported statically elsewhere (an import() can still be the first to load it). The development -// loader's imports of the source files, in src/i18n, don't count, and nothing loads a page route in -// the browser in production (the development server keeps the source files whole). +// The static pages' script, the only code production's browser runs (the development server keeps +// the source files whole). +const entry = path.join(src, "client/entry.ts") +const client = closure([entry], true) +// Lazy boundaries: every module the script loads with import(), even one that is also imported +// statically elsewhere (an import() can still be the first to load it). The development loader's +// imports of the source files, in src/i18n, don't count. const boundaries = [ ...new Set( [...client].flatMap((file) => scan(file).dynamic).filter((file) => !file.startsWith(path.join(src, "i18n"))), ), ].toSorted() -// The shell: what the code every page loads can show, plus what most page routes show (the site's -// header, footer and search), so it loads once rather than with each page. A page that doesn't show -// those (the 404 arcade) loads a few kilobytes it doesn't need. -const shell = closure(shellRoots) +// The shell: what the script every page loads can show, plus what of most page routes (the site's +// header, footer and search) the script can show too, so it loads once rather than with each page. A +// page that doesn't show those (the 404 arcade) loads a few kilobytes it doesn't need. +const shell = closure([entry]) const most = (field: "keys" | "areas") => { + const shown = gather(client, field) const counts = new Map() routes.forEach((file) => gather(closure([file]), field).forEach((item) => counts.set(item, (counts.get(item) ?? 0) + 1))) - return [...counts].filter(([, count]) => count > routes.length / 2).map(([item]) => item) + return [...counts].filter(([item, count]) => count > routes.length / 2 && shown.has(item)).map(([item]) => item) } const shellKeys = new Set([...gather(shell, "keys"), ...most("keys")]) const shellAreas = new Set([...gather(shell, "areas"), ...most("areas")]) diff --git a/services/site/src/client/islands/legal.ts b/services/site/src/client/islands/legal.ts index 5c07ca9c0a5..96e0599ecb4 100644 --- a/services/site/src/client/islands/legal.ts +++ b/services/site/src/client/islands/legal.ts @@ -157,12 +157,9 @@ function print() { void document.fonts.ready.then(() => setTimeout(() => window.print(), 800)) } -// The Privacy Policy's analytics control (analytics-choice.tsx) shows in the browser only: rendered +// The Privacy Policy's analytics control (analytics-choice.ts) shows in the browser only: rendered // into its host once the page is idle, as the hydrated page does after it loads. function analytics() { const host = $('[data-slot="analytics-choice-host"]') - if (!host) return - void Promise.all([import("solid-js/web"), import("~/component/site/analytics-choice")]).then(([web, choice]) => - web.render(() => web.createComponent(choice.AnalyticsChoice, {}), host), - ) + if (host) void import("~/component/site/analytics-choice").then((module) => module.analyticsChoice(host)) } diff --git a/services/site/src/component/site/analytics-choice.ts b/services/site/src/component/site/analytics-choice.ts new file mode 100644 index 00000000000..26e419d8e8e --- /dev/null +++ b/services/site/src/component/site/analytics-choice.ts @@ -0,0 +1,63 @@ +import "./analytics-choice.css" + +const YEAR_SECONDS = 31_536_000 + +const status = { + on: "Analytics are on in this browser.", + off: "Analytics are off in this browser.", + gpc: "Analytics are off in this browser because it sends a Global Privacy Control signal.", +} + +/** + * The Privacy Policy's on and off control for analytics in this browser, rendered into `host` in the + * browser only (legal.tsx, and src/client/islands/legal.ts on the static page). Off sets `oc_optout=1` + * for a year and deletes the analytics cookies (at `/` and at `/console`, where older pages wrote them) + * and the day's reported events in local storage; on deletes `oc_optout`. It writes the cookies itself, + * so it works without the tracker. A browser with Global Privacy Control is always off, so it shows no + * button. + */ +export function analyticsChoice(host: HTMLElement) { + const gpc = readState() === "gpc" + host.innerHTML = `

${ + gpc ? "" : '

' + }
` + const line = host.querySelector("p")! + const button = host.querySelector("button") + const show = () => { + const state = readState() + line.textContent = status[state] + if (button) button.textContent = state === "on" ? "Turn off analytics" : "Turn on analytics" + } + button?.addEventListener("click", () => { + setAnalytics(readState() === "off") + show() + }) + show() +} + +function readState() { + if ("globalPrivacyControl" in navigator && navigator.globalPrivacyControl === true) return "gpc" + return document.cookie.split(";").some((part) => part.trim() === "oc_optout=1") ? "off" : "on" +} + +function setAnalytics(on: boolean) { + const secure = location.protocol === "https:" ? "; Secure" : "" + if (on) { + document.cookie = `oc_optout=; Path=/; SameSite=Lax; Max-Age=0${secure}` + return + } + const cookies = [ + `oc_optout=1; Path=/; SameSite=Lax; Max-Age=${YEAR_SECONDS}${secure}`, + ...["oc_vid", "oc_sid", "oc_attr"].flatMap((name) => + ["/", "/console"].map((path) => `${name}=; Path=${path}; SameSite=Lax; Max-Age=0${secure}`), + ), + ] + cookies.forEach((cookie) => { + document.cookie = cookie + }) + try { + localStorage.removeItem("opencode-console.funnel.seen") + } catch { + // Storage the browser refuses holds nothing to clear. + } +} diff --git a/services/site/src/component/site/analytics-choice.tsx b/services/site/src/component/site/analytics-choice.tsx deleted file mode 100644 index 8829290bd7f..00000000000 --- a/services/site/src/component/site/analytics-choice.tsx +++ /dev/null @@ -1,68 +0,0 @@ -import { createSignal, onMount, Show } from "solid-js" -import "./analytics-choice.css" - -const YEAR_SECONDS = 31_536_000 - -/** - * The Privacy Policy's on and off control for analytics in this browser. Off sets `oc_optout=1` for a year and - * deletes the analytics cookies (at `/` and at `/console`, where older pages wrote them) and the day's reported - * events in local storage; on deletes `oc_optout`. It writes the cookies itself, so it works without the tracker. - * A browser with Global Privacy Control is always off, so it shows no button. - */ -export function AnalyticsChoice() { - const [state, setState] = createSignal<"on" | "off" | "gpc">() - onMount(() => setState(readState())) - const toggle = () => { - setAnalytics(state() === "off") - setState(readState()) - } - return ( - - {(current) => ( -
-

- {current() === "on" - ? "Analytics are on in this browser." - : current() === "off" - ? "Analytics are off in this browser." - : "Analytics are off in this browser because it sends a Global Privacy Control signal."} -

- -

- -

-
-
- )} -
- ) -} - -function readState() { - if ("globalPrivacyControl" in navigator && navigator.globalPrivacyControl === true) return "gpc" - return document.cookie.split(";").some((part) => part.trim() === "oc_optout=1") ? "off" : "on" -} - -function setAnalytics(on: boolean) { - const secure = location.protocol === "https:" ? "; Secure" : "" - if (on) { - document.cookie = `oc_optout=; Path=/; SameSite=Lax; Max-Age=0${secure}` - return - } - const cookies = [ - `oc_optout=1; Path=/; SameSite=Lax; Max-Age=${YEAR_SECONDS}${secure}`, - ...["oc_vid", "oc_sid", "oc_attr"].flatMap((name) => - ["/", "/console"].map((path) => `${name}=; Path=${path}; SameSite=Lax; Max-Age=0${secure}`), - ), - ] - cookies.forEach((cookie) => { - document.cookie = cookie - }) - try { - localStorage.removeItem("opencode-console.funnel.seen") - } catch { - // Storage the browser refuses holds nothing to clear. - } -} diff --git a/services/site/src/component/site/legal.tsx b/services/site/src/component/site/legal.tsx index 77def28f0dd..19ca6ff763d 100644 --- a/services/site/src/component/site/legal.tsx +++ b/services/site/src/component/site/legal.tsx @@ -21,7 +21,7 @@ import { type LegalNode, } from "~/lib/legal" import { legalSummaries } from "~/lib/legal/summaries" -import { AnalyticsChoice } from "./analytics-choice" +import { analyticsChoice } from "./analytics-choice" import { CheckIcon, Chevron, CopyIcon, DownloadIcon, ExternalArrow, NewTab } from "./icons" import { copySelection } from "~/lib/clipboard" import { architectLabelCases, architectRules, legalHistoryViews, legalPrintStyles, siteSettings } from "./settings" @@ -683,12 +683,15 @@ function LegalBody(props: { }) { if (typeof props.node === "string") return props.node const node = props.node - // Shown in the browser only; a static page's script renders it into this host (src/client/islands/legal.ts). + // Shown in the browser only, rendered into this host (analytics-choice.ts); a static page's script + // renders it from src/client/islands/legal.ts. if (node[0] === "analytics-choice") - return staticRender() ? ( -
- ) : ( - + return ( +
onMount(() => analyticsChoice(host))} + /> ) if (published(node)) return // A note to the legal reviewers in a draft: shown only with Settings › Legal review notes. diff --git a/services/site/src/component/site/site.tsx b/services/site/src/component/site/site.tsx index 2d87bd5550d..d9553ebfe96 100644 --- a/services/site/src/component/site/site.tsx +++ b/services/site/src/component/site/site.tsx @@ -125,9 +125,9 @@ const clientPages = /^\/(?:desktop|mobile|terminal|sdk|changelog(?:\/v\d[\w.+-]* function clientPage(pathname: string) { return clientPages.test(strip(pathname)) } -// The full page loads a link here can lead to on this domain (this site's other pages, in any -// language, the static ones among them, the docs and Stats), fetched once the pointer rests on the -// link so the page opens sooner. +// The full page loads a link here can lead to on this domain (this site's pages, in any language, +// the docs and Stats), fetched once the pointer rests on the link so the page opens sooner. The +// development server prefetches none of the site's pages. // Only addresses that are safe to request ahead of time: never the Console or a download, and never a // language link (hreflang), whose page sets the language cookie. Browsers without Speculation Rules // ignore it. @@ -140,8 +140,6 @@ const speculationRules = JSON.stringify({ and: [ { href_matches: [ - "{/:locale}?/:page(product|ide|mini|font)", - "{/:locale}?/product/*", "{/:locale}?/extensions/*", // The home page is "/" and "/ja", not "/ja/". ...(import.meta.env.DEV diff --git a/services/site/src/i18n/index.ts b/services/site/src/i18n/index.ts index e0ba6455d37..4ca29544451 100644 --- a/services/site/src/i18n/index.ts +++ b/services/site/src/i18n/index.ts @@ -18,9 +18,9 @@ type Load = () => Promise<{ default: Payload }> export type Table = { all: Load; slices?: Record } // The server registers every language at startup (i18n/server.ts). The browser loads only what the -// page can show, in the page's language: the shell's strings before it hydrates (entry-client.tsx), -// and each lazy boundary's (a page route, a module loaded with import()) before that module runs. A -// production build splits the strings that way (script/i18n-slices.ts); the development server +// page can show, in the page's language: the shell's strings before it renders a component +// (client/solid.tsx), and each lazy boundary's (a module loaded with import()) before that module +// runs. A production build splits the strings that way (script/i18n-slices.ts); the development server // loads whole languages (i18n/tables.ts). Switching to a language in place loads all of it first. const stores: Partial; content: Record; same: Set }>> = {} const complete = new Set() diff --git a/services/site/src/routes/video-editor/index.tsx b/services/site/src/routes/video-editor/index.tsx index f45c2ec0eec..24078f94394 100644 --- a/services/site/src/routes/video-editor/index.tsx +++ b/services/site/src/routes/video-editor/index.tsx @@ -1,17 +1,11 @@ import { Meta, Title } from "@solidjs/meta" -import { clientOnly, HttpStatusCode } from "@solidjs/start" +import { clientOnly } from "@solidjs/start" -// Development only: the product video editor (see product-videos/EDITOR.md). Production builds drop the editor. -const Editor = import.meta.env.DEV ? clientOnly(() => import("~/component/video-editor/editor")) : undefined +// Development only: the product video editor (see product-videos/EDITOR.md). Production answers this +// address with the not-found page (src/static/routes.ts), and its builds leave the editor out. +const Editor = import.meta.env.DEV ? clientOnly(() => import("~/component/video-editor/editor")) : () => null export default function VideoEditorPage() { - if (!Editor) - return ( - <> - -

Not found

- - ) return ( <> Video editor