mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
refactor(site): drop code only the hydrated production pages used (#63147)
This commit is contained in:
@@ -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/<locale>.ts,
|
||||
// src/i18n/content/<locale>.ts); this reads them and the app's import graph and works out, for each
|
||||
// src/i18n/content/<locale>.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<string>, 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<string, number>()
|
||||
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")])
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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 = `<div data-slot="analytics-choice"><p role="status"></p>${
|
||||
gpc ? "" : '<p><button type="button" data-component="site-button" data-variant="neutral"></button></p>'
|
||||
}</div>`
|
||||
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.
|
||||
}
|
||||
}
|
||||
@@ -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 (
|
||||
<Show when={state()}>
|
||||
{(current) => (
|
||||
<div data-slot="analytics-choice">
|
||||
<p role="status">
|
||||
{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."}
|
||||
</p>
|
||||
<Show when={current() !== "gpc"}>
|
||||
<p>
|
||||
<button type="button" data-component="site-button" data-variant="neutral" onClick={toggle}>
|
||||
{current() === "on" ? "Turn off analytics" : "Turn on analytics"}
|
||||
</button>
|
||||
</p>
|
||||
</Show>
|
||||
</div>
|
||||
)}
|
||||
</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.
|
||||
}
|
||||
}
|
||||
@@ -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() ? (
|
||||
<div data-slot="analytics-choice-host" style={{ display: "contents" }} />
|
||||
) : (
|
||||
<AnalyticsChoice />
|
||||
return (
|
||||
<div
|
||||
data-slot="analytics-choice-host"
|
||||
style={{ display: "contents" }}
|
||||
ref={(host) => onMount(() => analyticsChoice(host))}
|
||||
/>
|
||||
)
|
||||
if (published(node)) return <LegalElement node={node} anchors={props.anchors} />
|
||||
// A note to the legal reviewers in a draft: shown only with Settings › Legal review notes.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -18,9 +18,9 @@ type Load = () => Promise<{ default: Payload }>
|
||||
export type Table = { all: Load; slices?: Record<string, Load | Payload> }
|
||||
|
||||
// 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<Record<Locale, { dict: Partial<Dict>; content: Record<string, unknown>; same: Set<string> }>> = {}
|
||||
const complete = new Set<Locale>()
|
||||
|
||||
@@ -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 (
|
||||
<>
|
||||
<HttpStatusCode code={404} />
|
||||
<p>Not found</p>
|
||||
</>
|
||||
)
|
||||
return (
|
||||
<>
|
||||
<Title>Video editor</Title>
|
||||
|
||||
Reference in New Issue
Block a user