Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit cfc690d

Browse files
authored
fix(console): self-host monaco and brand icons instead of CDN (#23)
The YAML view on deploy/edit forms intermittently hung on a "Loading…" placeholder forever. @monaco-editor/react, with no loader configured and no bundled monaco, falls back to fetching the whole editor from jsDelivr at runtime; on a slow, proxied, or air-gapped network the multi-megabyte editor.main.js stalls and the fallback never clears. The sidebar brand icons (simple-icons) were pulled from the same CDN, so neither subsystem worked without reliable egress to cdn.jsdelivr.net. Bundle both instead so nothing is fetched from a CDN at runtime: - Add monaco-editor as a direct dep and configure @monaco-editor/react via loader.config({ monaco }) with the language workers wired to the bundled copies (monaco-setup.ts). loader.init() short-circuits on the provided instance before it would ever inject the jsDelivr script. - Lazy-load YamlEditor so monaco and its workers are code-split into their own chunk, fetched from the app's own origin only when the YAML view is opened — the initial bundle is unaffected. - Inline simple-icons SVGs as data URIs from the bundled package. Amazon S3 was removed upstream from simple-icons, so the Bucket kind now falls back to a generic Lucide storage glyph. A production build shows simple-icons fully inlined and the only remaining jsDelivr string is the unreachable default paths.vs inside the third-party loader, which loader.init() never reaches once monaco is provided. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent fc63158 commit cfc690d

8 files changed

Lines changed: 211 additions & 31 deletions

File tree

‎apps/console/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
"@tanstack/react-query": "^5.96.2",
2626
"js-yaml": "^4.1.0",
2727
"lucide-react": "^0.484.0",
28+
"monaco-editor": "^0.55.1",
2829
"react": "^19.2.4",
2930
"react-dom": "^19.2.4",
30-
"react-router": "^7.6.1"
31+
"react-router": "^7.6.1",
32+
"simple-icons": "^16.22.0"
3133
},
3234
"devDependencies": {
3335
"@tailwindcss/vite": "^4.2.2",

‎apps/console/src/components/YamlEditor.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useRef } from "react"
22
import Editor, { type OnMount } from "@monaco-editor/react"
3+
import "../lib/monaco-setup.ts"
34

45
interface YamlEditorProps {
56
value: string
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, it, expect, vi } from "vitest"
2+
3+
// Mock the heavy/browser-only modules so the setup can be imported in jsdom and
4+
// we can assert how it wires things — without loading real monaco or workers.
5+
const configSpy = vi.fn()
6+
vi.mock("@monaco-editor/react", () => ({ loader: { config: configSpy } }))
7+
vi.mock("monaco-editor", () => ({ editor: {} }))
8+
9+
class EditorWorker {}
10+
class JsonWorker {}
11+
class OtherWorker {}
12+
vi.mock("monaco-editor/esm/vs/editor/editor.worker?worker", () => ({ default: EditorWorker }))
13+
vi.mock("monaco-editor/esm/vs/language/json/json.worker?worker", () => ({ default: JsonWorker }))
14+
vi.mock("monaco-editor/esm/vs/language/css/css.worker?worker", () => ({ default: OtherWorker }))
15+
vi.mock("monaco-editor/esm/vs/language/html/html.worker?worker", () => ({ default: OtherWorker }))
16+
vi.mock("monaco-editor/esm/vs/language/typescript/ts.worker?worker", () => ({
17+
default: OtherWorker,
18+
}))
19+
20+
interface MonacoEnv {
21+
getWorker(workerId: string, label: string): Worker
22+
}
23+
24+
describe("monaco-setup", () => {
25+
it("points @monaco-editor/react at the bundled monaco so it never loads from a CDN", async () => {
26+
await import("./monaco-setup.ts")
27+
expect(configSpy).toHaveBeenCalledWith(
28+
expect.objectContaining({ monaco: expect.anything() }),
29+
)
30+
})
31+
32+
it("routes language workers to the bundled copies and falls back to the editor worker", async () => {
33+
await import("./monaco-setup.ts")
34+
const env = (self as unknown as { MonacoEnvironment?: MonacoEnv }).MonacoEnvironment
35+
if (!env) throw new Error("monaco-setup did not install MonacoEnvironment")
36+
expect(env.getWorker("x", "json")).toBeInstanceOf(JsonWorker)
37+
// unknown labels (e.g. yaml) get the base editor worker
38+
expect(env.getWorker("x", "yaml")).toBeInstanceOf(EditorWorker)
39+
})
40+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as monaco from "monaco-editor"
2+
import { loader } from "@monaco-editor/react"
3+
import EditorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker"
4+
import JsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker"
5+
import CssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker"
6+
import HtmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker"
7+
import TsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker"
8+
9+
// Self-host monaco. Without loader.config the @monaco-editor/react default
10+
// loader fetches the whole editor from jsDelivr at runtime, which stalls
11+
// forever on air-gapped or flaky networks. Pointing it at the bundled copy and
12+
// routing the language workers to bundled copies keeps everything on the app's
13+
// own origin.
14+
const WORKERS: Record<string, new () => Worker> = {
15+
json: JsonWorker,
16+
css: CssWorker,
17+
scss: CssWorker,
18+
less: CssWorker,
19+
html: HtmlWorker,
20+
handlebars: HtmlWorker,
21+
razor: HtmlWorker,
22+
typescript: TsWorker,
23+
javascript: TsWorker,
24+
}
25+
26+
self.MonacoEnvironment = {
27+
getWorker(_workerId: string, label: string): Worker {
28+
const WorkerCtor = WORKERS[label] ?? EditorWorker
29+
return new WorkerCtor()
30+
},
31+
}
32+
33+
loader.config({ monaco })
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, it, expect } from "vitest"
2+
import { render } from "@testing-library/react"
3+
import { simpleIconSlug, lucideIcon, simpleIconComponent } from "./sidebar-icons.tsx"
4+
5+
describe("simpleIconSlug", () => {
6+
it("returns a Simple Icons slug for a known brand kind", () => {
7+
expect(simpleIconSlug("Postgres")).toBe("postgresql")
8+
})
9+
10+
it("returns undefined for a kind with no brand logo", () => {
11+
expect(simpleIconSlug("Tenant")).toBeUndefined()
12+
})
13+
14+
it("returns undefined for Bucket — Amazon S3 was removed from Simple Icons", () => {
15+
expect(simpleIconSlug("Bucket")).toBeUndefined()
16+
})
17+
})
18+
19+
describe("lucideIcon", () => {
20+
it("provides a Lucide fallback for Bucket", () => {
21+
expect(lucideIcon("Bucket")).toBeDefined()
22+
})
23+
})
24+
25+
describe("simpleIconComponent", () => {
26+
it("renders the icon from a bundled data URI, never a CDN", () => {
27+
const slug = simpleIconSlug("Postgres")
28+
if (!slug) throw new Error("expected Postgres to have a slug")
29+
const Icon = simpleIconComponent(slug)
30+
const { container } = render(<Icon />)
31+
32+
const style = container.querySelector("span")?.getAttribute("style") ?? ""
33+
expect(style).toContain("data:image/svg+xml")
34+
// The mask must be a local data URI — no remote fetch of any kind.
35+
expect(style).not.toContain("jsdelivr")
36+
expect(style).not.toContain("cdn")
37+
expect(style).not.toContain("https://")
38+
})
39+
40+
it("renders an empty placeholder for an unknown slug instead of throwing", () => {
41+
const Icon = simpleIconComponent("definitely-not-a-real-slug")
42+
const { container } = render(<Icon />)
43+
const span = container.querySelector("span")
44+
expect(span).not.toBeNull()
45+
expect(span?.getAttribute("style") ?? "").not.toContain("data:image/svg+xml")
46+
})
47+
})

‎apps/console/src/lib/sidebar-icons.tsx‎

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ import {
1010
Users,
1111
type LucideIcon,
1212
} from "lucide-react"
13+
import {
14+
siApachekafka,
15+
siClickhouse,
16+
siEtcd,
17+
siHarbor,
18+
siKubernetes,
19+
siMariadb,
20+
siMongodb,
21+
siNatsdotio,
22+
siNginx,
23+
siOpensearch,
24+
siPostgresql,
25+
siPrometheus,
26+
siQdrant,
27+
siRabbitmq,
28+
siRedis,
29+
siVault,
30+
siWireguard,
31+
type SimpleIcon as SimpleIconData,
32+
} from "simple-icons"
1333

1434
/**
1535
* TODO(bff): move both of these mappings to the server. Ideally each
@@ -18,41 +38,45 @@ import {
1838
* doesn't need a hardcoded table. Until then we keep the mapping here and
1939
* fall through to the generic Lucide fallback.
2040
*/
21-
const KIND_TO_SIMPLE_ICON: Record<string, string> = {
22-
// IaaS
23-
Bucket: "amazons3",
24-
41+
const KIND_TO_SIMPLE_ICON: Record<string, SimpleIconData> = {
2542
// PaaS
26-
ClickHouse: "clickhouse",
27-
Harbor: "harbor",
28-
Kafka: "apachekafka",
29-
MariaDB: "mariadb",
30-
MongoDB: "mongodb",
31-
NATS: "natsdotio",
32-
OpenBAO: "vault",
33-
Postgres: "postgresql",
34-
Qdrant: "qdrant",
35-
RabbitMQ: "rabbitmq",
36-
Redis: "redis",
43+
ClickHouse: siClickhouse,
44+
Harbor: siHarbor,
45+
Kafka: siApachekafka,
46+
MariaDB: siMariadb,
47+
MongoDB: siMongodb,
48+
NATS: siNatsdotio,
49+
OpenBAO: siVault,
50+
Postgres: siPostgresql,
51+
Qdrant: siQdrant,
52+
RabbitMQ: siRabbitmq,
53+
Redis: siRedis,
3754

3855
// NaaS
39-
HTTPCache: "nginx",
40-
VPN: "wireguard",
56+
HTTPCache: siNginx,
57+
VPN: siWireguard,
4158

4259
// Administration
43-
Etcd: "etcd",
44-
Ingress: "nginx",
45-
Kubernetes: "kubernetes",
46-
Monitoring: "prometheus",
47-
OpenSearch: "opensearch",
60+
Etcd: siEtcd,
61+
Ingress: siNginx,
62+
Kubernetes: siKubernetes,
63+
Monitoring: siPrometheus,
64+
OpenSearch: siOpensearch,
4865
}
4966

67+
const ICON_BY_SLUG: Record<string, SimpleIconData> = Object.fromEntries(
68+
Object.values(KIND_TO_SIMPLE_ICON).map((icon) => [icon.slug, icon]),
69+
)
70+
5071
/**
5172
* Lucide fallbacks for kinds that don't have a canonical brand logo in
5273
* Simple Icons. These use the same pack as cozyportal-ui.
5374
*/
5475
const KIND_TO_LUCIDE_ICON: Record<string, LucideIcon | ComponentType<{ className?: string }>> = {
5576
BootBox: Server,
77+
// Amazon S3's logo was removed from Simple Icons, so the S3-compatible
78+
// Bucket kind falls back to a generic storage glyph.
79+
Bucket: Database,
5680
ExternalDNS: Globe,
5781
TCPBalancer: Network,
5882
FoundationDB: Database,
@@ -65,7 +89,7 @@ const KIND_TO_LUCIDE_ICON: Record<string, LucideIcon | ComponentType<{ className
6589
}
6690

6791
export function simpleIconSlug(kind: string): string | undefined {
68-
return KIND_TO_SIMPLE_ICON[kind]
92+
return KIND_TO_SIMPLE_ICON[kind]?.slug
6993
}
7094

7195
export function lucideIcon(
@@ -79,12 +103,20 @@ export function lucideIcon(
79103
* CSS `mask-image`. The span takes its colour from `currentColor`, so active
80104
* sidebar items pick up the blue accent and inactive ones stay slate-400 —
81105
* exactly like the Lucide icons next to them.
106+
*
107+
* The SVG is inlined from the bundled simple-icons package as a data URI, so it
108+
* is served from the app's own origin — nothing is fetched from a CDN.
82109
*/
83110
export function simpleIconComponent(slug: string): ComponentType<{ className?: string }> {
84-
// cdn.simpleicons.org rate-limits aggressively (returns 403 from many
85-
// browsers and crawlers). Pull the SVG straight out of the simple-icons
86-
// npm package via jsDelivr — same files, no rate limits.
87-
const url = `url(https://cdn.jsdelivr.net/npm/simple-icons@latest/icons/${slug}.svg)`
111+
const icon: SimpleIconData | undefined = ICON_BY_SLUG[slug]
112+
if (!icon) {
113+
// Unknown slug — render an empty placeholder rather than throwing.
114+
return function MissingIcon({ className }) {
115+
return <span aria-hidden className={className} />
116+
}
117+
}
118+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="${icon.path}"/></svg>`
119+
const url = `url("data:image/svg+xml,${encodeURIComponent(svg)}")`
88120
return function SimpleIcon({ className }) {
89121
return (
90122
<span

‎apps/console/src/routes/ApplicationOrderPage.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useRef, useState } from "react"
1+
import { lazy, Suspense, useEffect, useMemo, useRef, useState } from "react"
22
import { useNavigate, useParams, useSearchParams } from "react-router"
33
import { ChevronLeft, FileCode, FormInput } from "lucide-react"
44
import yaml from "js-yaml"
@@ -14,7 +14,12 @@ import { useTenantContext } from "../lib/tenant-context.tsx"
1414
import { composeResource } from "../lib/app-resource.ts"
1515
import { prepareUpdateSpec } from "../lib/prepare-update.ts"
1616
import { SchemaForm } from "../components/SchemaForm.tsx"
17-
import { YamlEditor } from "../components/YamlEditor.tsx"
17+
18+
// Lazy-load the YAML editor so monaco and its workers are code-split into their
19+
// own chunk, fetched only when the YAML view is opened.
20+
const YamlEditor = lazy(() =>
21+
import("../components/YamlEditor.tsx").then((m) => ({ default: m.YamlEditor })),
22+
)
1823

1924
type Mode = "form" | "yaml"
2025

@@ -286,7 +291,15 @@ export function ApplicationOrderPage({
286291
</div>
287292
)}
288293
<div className="flex-1">
289-
<YamlEditor value={yamlText} onChange={setYamlText} />
294+
<Suspense
295+
fallback={
296+
<div className="flex h-full items-center justify-center">
297+
<Spinner />
298+
</div>
299+
}
300+
>
301+
<YamlEditor value={yamlText} onChange={setYamlText} />
302+
</Suspense>
290303
</div>
291304
</div>
292305
)}

‎pnpm-lock.yaml‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)