Skip to content

Commit 714d653

Browse files
authored
perf(site): load widgets and other pages' code only when visitors reach for them (#62393)
1 parent ffde0f7 commit 714d653

35 files changed

Lines changed: 429 additions & 223 deletions

‎services/site/script/check-client-bundle.ts‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,24 @@ console.log(
6565
// budget when a page gets lighter; a page without one fails until it has one.
6666
const budgets: Record<string, number> = {
6767
"[...404].tsx": 104,
68-
"brand/index.tsx": 111,
69-
"changelog/[tag].tsx": 118,
70-
"changelog/index.tsx": 117,
71-
"desktop.tsx": 133,
68+
"brand/index.tsx": 107,
69+
"changelog/[tag].tsx": 113,
70+
"changelog/index.tsx": 112,
71+
"desktop.tsx": 126,
7272
"devices.tsx": 51,
73-
"extensions/browse.tsx": 108,
74-
"font/index.tsx": 125,
75-
"ide.tsx": 133,
76-
"index.tsx": 129,
77-
"legal/index.tsx": 150,
78-
"legal/privacy-policy/index.tsx": 150,
79-
"legal/terms-of-service/index.tsx": 150,
80-
"mini.tsx": 133,
81-
"mobile.tsx": 133,
82-
"press/index.tsx": 116,
83-
"product/index.tsx": 121,
84-
"sdk.tsx": 133,
85-
"terminal.tsx": 133,
73+
"extensions/browse.tsx": 99,
74+
"font/index.tsx": 121,
75+
"ide.tsx": 126,
76+
"index.tsx": 118,
77+
"legal/index.tsx": 148,
78+
"legal/privacy-policy/index.tsx": 137,
79+
"legal/terms-of-service/index.tsx": 135,
80+
"mini.tsx": 126,
81+
"mobile.tsx": 126,
82+
"press/index.tsx": 112,
83+
"product/index.tsx": 113,
84+
"sdk.tsx": 126,
85+
"terminal.tsx": 126,
8686
"video-editor/index.tsx": 49,
8787
}
8888
type Chunk = { file: string; imports?: string[] }

‎services/site/script/fold-settings.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,18 @@ function plainRead(argument: types.Node): boolean {
560560
function known(expression: NodePath): unknown {
561561
if (!expression.isExpression()) return missing
562562
let effects = false
563+
// `void work()` evaluates to undefined, but the work still has to run.
564+
const discards = (node: types.Node) =>
565+
types.isUnaryExpression(node, { operator: "void" }) && !types.isLiteral(node.argument)
563566
expression.traverse({
564567
"SequenceExpression|AssignmentExpression|UpdateExpression|AwaitExpression|YieldExpression"() {
565568
effects = true
566569
},
570+
UnaryExpression(unary) {
571+
if (discards(unary.node)) effects = true
572+
},
567573
})
568-
if (effects || expression.isSequenceExpression()) return missing
574+
if (effects || expression.isSequenceExpression() || discards(expression.node)) return missing
569575
const result = expression.evaluate()
570576
if (!result.confident) return missing
571577
const value = result.value

‎services/site/script/i18n-plugin.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,24 @@ export function i18nSlices(): Plugin {
115115
},
116116
}
117117

118-
/** For each locale, the files a page in it loads before hydration: its table (with the shell's strings) and its route's slice. */
118+
/**
119+
* For each locale, the files a page in it loads before hydration: its table (with the shell's
120+
* strings) and its route's slice. `boundaries` has the other lazy boundaries' slices, for a page
121+
* that knows before hydration it will load one (the Markdown view on a reload in it).
122+
*/
119123
function preloads() {
120124
const data = analyze()
121125
const src = `${root}src/`
126+
const sliceFiles = (locale: string, routes: boolean) =>
127+
Object.fromEntries(
128+
Object.entries(data.boundaries)
129+
.filter(([file]) => file.startsWith(`${src}routes/`) === routes)
130+
.map(([file, slice]) => [file.slice(src.length), files.get(`virtual:i18n/${locale}/${data.shared[slice]}`) ?? null]),
131+
)
122132
return Object.fromEntries(
123133
Object.keys(data.locales).map((locale) => [
124134
locale,
125-
{
126-
table: files.get(`virtual:i18n/${locale}`),
127-
routes: Object.fromEntries(
128-
Object.entries(data.boundaries)
129-
.filter(([file]) => file.startsWith(`${src}routes/`))
130-
.map(([file, slice]) => [
131-
file.slice(src.length),
132-
files.get(`virtual:i18n/${locale}/${data.shared[slice]}`) ?? null,
133-
]),
134-
),
135-
},
135+
{ table: files.get(`virtual:i18n/${locale}`), routes: sliceFiles(locale, true), boundaries: sliceFiles(locale, false) },
136136
]),
137137
)
138138
}

‎services/site/script/legal-search.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Writes src/component/site/legal-search.json, the ⌘K search's entries for the legal documents
2-
// (legalSearch in lib/legal.ts), so the search doesn't load the documents themselves. Run after
2+
// (legalSearch in lib/legal-documents.ts), so the search doesn't load the documents themselves. Run after
33
// changing a legal document; test/legal-search.test.ts fails while the file is out of date.
44
import path from "node:path"
5-
import { legalSearch } from "../src/lib/legal"
5+
import { legalSearch } from "../src/lib/legal-documents"
66

77
const file = path.join(import.meta.dir, "../src/component/site/legal-search.json")
88
await Bun.write(file, JSON.stringify(legalSearch(), null, 2) + "\n")

‎services/site/src/component/site/docs-lockup.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Portal } from "solid-js/web"
44
import { docsProducts } from "~/lib/docs-content"
55
import { docsUrl, v1DocsUrl } from "~/lib/docs"
66
import { Chevron, ExternalArrow } from "./icons"
7-
import { fanMotion } from "./logo-fan"
7+
import { fanMotion } from "./fan-motion"
88
import { onCloseMenus } from "./menu-open"
99
import { siteSettings } from "./settings"
1010
import { inlineKey, isRtl } from "~/lib/direction"

‎services/site/src/component/site/extension-page.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { useI18n } from "~/context/i18n"
1010
import { useLanguage } from "~/context/language"
1111
import { useArea } from "~/i18n/content"
1212
import { extensionsSource } from "~/i18n/content/areas/extensions"
13-
import { extensionMarkdown } from "~/lib/product-markdown"
1413
import { ExtensionDownload } from "./download-sections"
1514
import { extensionPages, type ExtensionSlug } from "./extension-data"
1615
import { NewTab } from "./icons"
@@ -37,7 +36,7 @@ export function ExtensionPage(props: { slug: ExtensionSlug }) {
3736
const slug = extensionPages[(index + step + extensionPages.length) % extensionPages.length].slug
3837
return { slug, name: extensionCopy()[slug].name }
3938
}
40-
const markdown = useMarkdown(path(), extensionMarkdown(props.slug))
39+
const markdown = useMarkdown(path(), (_, pages) => pages.extensionMarkdown(props.slug)())
4140
const command = () => data().install.command
4241

4342
onMount(() => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// The logo fan's motion (logo-fan.tsx), shared with the section menus' fan style (docs-lockup.tsx)
2+
// and the press gallery, in its own module so they don't load the fan.
3+
4+
// Each item drops a little further than the one above it (12px, then 6px more per item, up to
5+
// 42px), so the column cascades without any item travelling far. Items also start a touch small
6+
// and turned, and settle square.
7+
const drop = (index: number) => Math.min(12 + index * 6, 42)
8+
const start = (index: number) => `translateY(-${drop(index)}px) rotate(-1deg) scale(0.97)`
9+
const rest = "translateY(0px) rotate(0deg) scale(1)"
10+
const stagger = 32
11+
12+
// A tight damped spring (about 7% overshoot, no visible second settle), sampled for linear().
13+
const spring = `linear(${Array.from({ length: 48 }, (_, index) => {
14+
const t = index / 47
15+
const damping = 0.65
16+
const frequency = 11
17+
const damped = frequency * Math.sqrt(1 - damping * damping)
18+
const value =
19+
1 -
20+
Math.exp(-damping * frequency * t) *
21+
(Math.cos(damped * t) + ((damping * frequency) / damped) * Math.sin(damped * t))
22+
return index === 47 ? "1" : value.toFixed(4)
23+
}).join(", ")})`
24+
25+
export const fanMotion = { start, rest, stagger, spring }

‎services/site/src/component/site/legal.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import { ShareMeta } from "~/component/share-meta"
88
import { ogImage } from "~/lib/og"
99
import { breadcrumbData, JsonLd } from "~/component/structured-data"
1010
import { useI18n } from "~/context/i18n"
11+
import type { MarkdownContext } from "~/lib/markdown"
1112
import { useLanguage } from "~/context/language"
1213
import {
1314
legalDate,
14-
legalHubMarkdown,
1515
legalMarkdown,
16-
legalPages,
1716
legalTextChanged,
1817
legalVersions,
1918
type LegalDocumentContent,
@@ -768,10 +767,13 @@ const legalGroups = [
768767
* picks how the documents are listed and Settings › Legal row hover how a row answers the pointer;
769768
* each document is a single link.
770769
*/
771-
export function LegalHub() {
770+
export function LegalHub(props: {
771+
documents: LegalDocumentContent[]
772+
markdown: (context: MarkdownContext) => string
773+
}) {
772774
const i18n = useI18n()
773775
const language = useLanguage()
774-
const markdown = useMarkdown("/legal", legalHubMarkdown)
776+
const markdown = useMarkdown("/legal", props.markdown)
775777
const title = "OpenCode | Privacy policy, terms and legal updates"
776778
const description =
777779
"Every OpenCode legal document in one place, including the Privacy Policy and the Terms of Use, with their dates, version history and a feed of changes."
@@ -781,10 +783,10 @@ export function LegalHub() {
781783
const groups = legalGroups
782784
.map((group) => ({
783785
title: group.title,
784-
documents: legalPages.filter((document) => group.paths.includes(document.path)),
786+
documents: props.documents.filter((document) => group.paths.includes(document.path)),
785787
}))
786788
.filter((group) => group.documents.length > 0)
787-
const grouped = () => style() === "grouped" && groups.length > 1 && legalPages.length > 3
789+
const grouped = () => style() === "grouped" && groups.length > 1 && props.documents.length > 3
788790
const Item = (props: { document: LegalDocumentContent }) => {
789791
const changed = legalTextChanged(props.document)
790792
return (
@@ -849,7 +851,7 @@ export function LegalHub() {
849851
</Show>
850852
{/* The list is the page: no section titles. In "grouped" the group names are its only headings. */}
851853
<div data-component="legal-hub" lang="en" dir="ltr">
852-
<Show when={grouped()} fallback={<List documents={legalPages} />}>
854+
<Show when={grouped()} fallback={<List documents={props.documents} />}>
853855
<For each={groups}>
854856
{(group) => (
855857
<section data-slot="group" aria-labelledby={slug(group.title)}>

‎services/site/src/component/site/logo-fan.tsx‎

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
import { useI18n } from "~/context/i18n"
2-
import "./logo-fan.css"
32
import { A } from "@solidjs/router"
43
import { createEffect, createMemo, createSignal, For, on, onCleanup, Show } from "solid-js"
54
import { useLanguage } from "~/context/language"
65
import { brandAssets } from "~/lib/media"
76
import { logoVariantSvgs } from "./logo-variants"
87
import { pressIcons } from "./press-icons"
8+
import { fanMotion } from "./fan-motion"
99
import { siteSettings } from "./settings"
1010

1111
type Position = { x: number; y: number }
1212

13-
// Each item drops a little further than the one above it (12px, then 6px more per item, up to
14-
// 42px), so the column cascades without any item travelling far. Items also start a touch small
15-
// and turned, and settle square.
16-
const drop = (index: number) => Math.min(12 + index * 6, 42)
17-
const start = (index: number) => `translateY(-${drop(index)}px) rotate(-1deg) scale(0.97)`
18-
const rest = "translateY(0px) rotate(0deg) scale(1)"
19-
const stagger = 32
20-
21-
// A tight damped spring (about 7% overshoot, no visible second settle), sampled for linear().
22-
const spring = `linear(${Array.from({ length: 48 }, (_, index) => {
23-
const t = index / 47
24-
const damping = 0.65
25-
const frequency = 11
26-
const damped = frequency * Math.sqrt(1 - damping * damping)
27-
const value =
28-
1 -
29-
Math.exp(-damping * frequency * t) *
30-
(Math.cos(damped * t) + ((damping * frequency) / damped) * Math.sin(damped * t))
31-
return index === 47 ? "1" : value.toFixed(4)
32-
}).join(", ")})`
33-
34-
// The fan's motion, shared with the section menus' fan style (docs-lockup.tsx).
35-
export const fanMotion = { start, rest, stagger, spring }
36-
3713
/**
3814
* The logo menu's fan option: like a Dock stack in Fan view, each brand asset springs out of the
3915
* logo, one after another, into a straight column of previews, each with its label beside it. The
@@ -90,11 +66,11 @@ export function LogoFan(props: { position?: Position; onSelect: () => void }) {
9066
rows().forEach((item, index) => {
9167
const fade = [...(rebound ? [] : [{ opacity: 0 }]), { opacity: 1 }]
9268
if (still()) return item.animate(fade, { duration: 150, easing: "ease-out" })
93-
const timing = { delay: index * stagger, fill: "backwards" as const }
94-
item.animate([...(rebound ? [] : [{ transform: start(index) }]), { transform: rest }], {
69+
const timing = { delay: index * fanMotion.stagger, fill: "backwards" as const }
70+
item.animate([...(rebound ? [] : [{ transform: fanMotion.start(index) }]), { transform: fanMotion.rest }], {
9571
...timing,
9672
duration: 450,
97-
easing: spring,
73+
easing: fanMotion.spring,
9874
})
9975
item.animate(fade, { ...timing, duration: 140, easing: "ease-out" })
10076
// A lifted shadow while the item drops, settling as it lands.
@@ -112,7 +88,7 @@ export function LogoFan(props: { position?: Position; onSelect: () => void }) {
11288
// A small dip before the items lift and fold back up, bottom item first.
11389
item.toggleAttribute("data-lift", true)
11490
return [
115-
item.animate([{ transform: start(index) }], {
91+
item.animate([{ transform: fanMotion.start(index) }], {
11692
delay,
11793
duration: 220,
11894
easing: "cubic-bezier(0.5, -0.5, 0.8, 0.3)",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { changelogEntryPageMarkdown, changelogMarkdown } from "~/lib/changelog-markdown"
2+
import { domMarkdown } from "~/lib/dom-markdown"
3+
import { homeMarkdown } from "~/lib/home-markdown"
4+
import { extensionMarkdown, productMarkdown, productsMarkdown } from "~/lib/product-markdown"
5+
import { MarkdownView } from "./markdown-view"
6+
7+
/**
8+
* The in-page Markdown view and the page Markdown only it needs, in one chunk that loads the first
9+
* time the view is wanted (use-markdown.ts). Pages whose Markdown is built from content they render
10+
* anyway (brand, press, font, legal) keep their generators.
11+
*/
12+
export const markdownPages = {
13+
MarkdownView,
14+
domMarkdown,
15+
homeMarkdown,
16+
productMarkdown,
17+
productsMarkdown,
18+
extensionMarkdown,
19+
changelogMarkdown,
20+
changelogEntryPageMarkdown,
21+
}
22+
23+
export type MarkdownPages = typeof markdownPages

0 commit comments

Comments
 (0)