@canonical/summon-application 0.36.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +57 -7
  2. package/dist/esm/application/react/index.js +97 -35
  3. package/dist/esm/application/react/index.js.map +1 -1
  4. package/dist/esm/application/react/templates/.storybook/decorators/withRouter.tsx +3 -2
  5. package/dist/esm/application/react/templates/.storybook/preview.ts.ejs +4 -2
  6. package/dist/esm/application/react/templates/README.md.ejs +88 -5
  7. package/dist/esm/application/react/templates/index.html.ejs +25 -3
  8. package/dist/esm/application/react/templates/package.json.ejs +22 -1
  9. package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +76 -17
  10. package/dist/esm/application/react/templates/src/domains/account/AccountPage.tsx.ejs +4 -5
  11. package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx.ejs +4 -5
  12. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +16 -5
  13. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx.ejs +2 -2
  14. package/dist/esm/application/react/templates/src/domains/contact/ContactPage.tsx.ejs +4 -5
  15. package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx.ejs +4 -10
  16. package/dist/esm/application/react/templates/src/domains/marketing/HomePage.tsx.ejs +13 -5
  17. package/dist/esm/application/react/templates/src/formatDocumentTitle.ts.ejs +21 -0
  18. package/dist/esm/application/react/templates/src/i18n/ar.ts +4 -5
  19. package/dist/esm/application/react/templates/src/i18n/en.ts +4 -5
  20. package/dist/esm/application/react/templates/src/i18n/fr.ts +4 -5
  21. package/dist/esm/application/react/templates/src/lib/ExampleComponent/styles.css +13 -3
  22. package/dist/esm/application/react/templates/src/lib/index.ts.ejs +1 -1
  23. package/dist/esm/application/react/templates/src/routes.tsx.ejs +25 -3
  24. package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +50 -16
  25. package/dist/esm/application/react/templates/src/styles/app.css +23 -13
  26. package/dist/esm/application/react/templates/src/styles/index.css.ejs +27 -0
  27. package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +58 -2
  28. package/dist/esm/application/react/templates/tsconfig.json.ejs +3 -0
  29. package/dist/esm/application/react/templates/vite-plugin-ds-icons.ts +69 -0
  30. package/dist/esm/application/react/templates/vite.config.ts.ejs +57 -3
  31. package/dist/esm/domain/index.js +3 -4
  32. package/dist/esm/domain/index.js.map +1 -1
  33. package/dist/esm/route/index.js +2 -3
  34. package/dist/esm/route/index.js.map +1 -1
  35. package/dist/types/application/react/index.d.ts +1 -0
  36. package/dist/types/application/react/index.d.ts.map +1 -1
  37. package/dist/types/domain/index.d.ts.map +1 -1
  38. package/dist/types/route/index.d.ts.map +1 -1
  39. package/package.json +8 -8
@@ -1,14 +1,36 @@
1
1
  <!doctype html>
2
- <html lang="en">
2
+ <!-- The root declaration: one context (`app`, `site` or `docs`) and one density
3
+ (`comfortable` or `dense`), which together choose the --density-* channel
4
+ every control reads. A colour scheme is optional — `light` or `dark` pins
5
+ one, leaving both off follows the reader's operating system.
6
+
7
+ Those classes are the whole of it. Nothing on this element declares the
8
+ design system itself: its stylesheet applies to the page as it stands. -->
9
+ <html lang="en" class="app comfortable">
3
10
  <head>
4
11
  <meta charset="UTF-8" />
5
12
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
13
+ <% if (spa) { -%>
14
+ <meta name="description" content="<%= name %> — a React application built with the Canonical design system and router packages." />
15
+ <% } else { -%>
6
16
  <meta name="description" content="<%= name %> — a React application built with the Canonical design system, router, and SSR packages." />
17
+ <% } -%>
7
18
  <link rel="icon" type="image/png" sizes="32x32" href="https://assets.ubuntu.com/v1/be7e4cc6-COF-favicon-32x32.png">
19
+ <% if (spa) { -%>
20
+ <!-- The client-only build serves this file as the document, so it carries a
21
+ title for the moment before React renders. The route page still owns
22
+ the real one: it renders <Head> from @canonical/react-head, and React
23
+ inserts that <title> ahead of this one in the head. -->
8
24
  <title><%= name %></title>
25
+ <% } else { -%>
26
+ <!-- No <title> here. The route page owns it, through <Head> from
27
+ @canonical/react-head, and React hoists that <title> into this head.
28
+ The server renderer re-emits this shell's head tags ahead of the
29
+ hoisted ones, and HTML takes the FIRST <title> in tree order — so a
30
+ <title> on this line would beat every page's. -->
31
+ <% } -%>
9
32
  </head>
10
- <!-- Surface declaration: context (app) + density (comfortable) — roots the design system's --density-* channel -->
11
- <body class="app comfortable">
33
+ <body>
12
34
  <div id="root"></div>
13
35
  <script type="module" src="/src/client/entry.tsx"></script>
14
36
  </body>
@@ -17,18 +17,28 @@
17
17
  },
18
18
  "scripts": {
19
19
  "dev": "vite",
20
+ <% if (!spa) { -%>
20
21
  "dev:express": "node --import tsx src/server/server.express.ts",
21
22
  "dev:bun": "bun src/server/server.bun.ts",
23
+ <% } -%>
22
24
  "preview": "bun run build:client && vite preview --outDir dist/client",
25
+ <% if (!spa) { -%>
23
26
  "preview:bun": "bun run build:client && bun run build:server && bun src/server/preview.bun.ts",
24
27
  "preview:express": "bun run build:client && bun run build:server && node --import tsx src/server/preview.express.ts",
28
+ <% } -%>
25
29
  "build": "bun run build:client",
26
30
  "build:all": "bun run build:client && bun run build:storybook",
27
31
  <% if (intl) { -%>
28
32
  "build:storybook": "storybook build",
29
33
  <% } -%>
34
+ <%# --ssrManifest exists for the server build's asset lookup; nothing else -%>
35
+ <%# reads the manifest, so the SPA build does not emit one. -%>
36
+ <% if (spa) { -%>
37
+ "build:client": "vite build --outDir dist/client",
38
+ <% } else { -%>
30
39
  "build:client": "vite build --ssrManifest --outDir dist/client",
31
40
  "build:server": "vite build --mode server",
41
+ <% } -%>
32
42
  <% if (!intl) { -%>
33
43
  "build:storybook": "storybook build",
34
44
  <% } -%>
@@ -55,6 +65,7 @@
55
65
  <% } -%>
56
66
  },
57
67
  "dependencies": {
68
+ "@canonical/ds-assets": "<%= pragmaVersion %>",
58
69
  <% if (intl) { -%>
59
70
  "@canonical/i18n-core": "<%= pragmaVersion %>",
60
71
  "@canonical/i18n-react": "<%= pragmaVersion %>",
@@ -65,7 +76,9 @@
65
76
  <% } -%>
66
77
  "@canonical/react-head": "<%= pragmaVersion %>",
67
78
  "@canonical/react-hooks": "<%= pragmaVersion %>",
79
+ <% if (!spa) { -%>
68
80
  "@canonical/react-ssr": "<%= pragmaVersion %>",
81
+ <% } -%>
69
82
  "@canonical/router-core": "<%= pragmaVersion %>",
70
83
  "@canonical/router-react": "<%= pragmaVersion %>",
71
84
  <% if (relay) { -%>
@@ -74,7 +87,9 @@
74
87
  "@canonical/storybook-addon-utils": "<%= pragmaVersion %>",
75
88
  "@canonical/storybook-config": "<%= pragmaVersion %>",
76
89
  "@canonical/styles": "<%= pragmaVersion %>",
90
+ <% if (!spa) { -%>
77
91
  "express": "^5.2.1",
92
+ <% } -%>
78
93
  <% if (relay) { -%>
79
94
  "graphql": "^16.11.0",
80
95
  <% } -%>
@@ -85,7 +100,7 @@
85
100
  "relay-runtime-network": "0.1.0"<% } %>
86
101
  },
87
102
  "devDependencies": {
88
- "@biomejs/biome": "2.4.9",
103
+ "@biomejs/biome": "2.5.13",
89
104
  "@canonical/biome-config": "<%= pragmaVersion %>",
90
105
  "@canonical/typescript-config-react": "<%= pragmaVersion %>",
91
106
  <% if (intl) { -%>
@@ -98,7 +113,9 @@
98
113
  "@testing-library/jest-dom": "^6.9.1",
99
114
  <% } -%>
100
115
  "@testing-library/react": "^16.3.2",
116
+ <% if (!spa) { -%>
101
117
  "@types/express": "^5.0.6",
118
+ <% } -%>
102
119
  <% if (intl) { -%>
103
120
  "@types/node": "^26.0.0",
104
121
  <% } else { -%>
@@ -114,7 +131,9 @@
114
131
  "@vitejs/plugin-react": "^6.0.0",
115
132
  "@vitest/coverage-v8": "^4.0.18",
116
133
  "browserslist": "^4.28.1",
134
+ <% if (!spa) { -%>
117
135
  "bun-types": "^1.3.9",
136
+ <% } -%>
118
137
  "jsdom": "^28.1.0",
119
138
  "lightningcss": "^1.32.0",
120
139
  <% if (relay) { -%>
@@ -122,7 +141,9 @@
122
141
  "relay-test-utils": "^21.0.1",
123
142
  <% } -%>
124
143
  "storybook": "^10.3.1",
144
+ <% if (!spa) { -%>
125
145
  "tsx": "^4.19.0",
146
+ <% } -%>
126
147
  "typescript": "^5.9.3",
127
148
  "vite": "^8.0.1",
128
149
  <% if (relay) { -%>
@@ -1,9 +1,25 @@
1
+ // The stylesheet comes first, before the routes and before every component
2
+ // import below. CSS is emitted in the order the bundler evaluates the modules
3
+ // that import it, so a component imported earlier would put its own rules —
4
+ // and its own `@layer` blocks — ahead of the design system's layer statement,
5
+ // and the layer order a browser applies is the one it reads first. Keeping
6
+ // this line at the top is what makes the built stylesheet deterministic.
7
+ import "#styles/index.css";
1
8
  <% if (intl) { -%>
9
+ <% if (spa) { -%>
10
+ import { negotiateLocale } from "@canonical/i18n-core";
11
+ <% } else { -%>
2
12
  import { isSupportedLocale, negotiateLocale } from "@canonical/i18n-core";
13
+ <% } -%>
3
14
  import { I18nProvider } from "@canonical/i18n-react";
4
15
  <% } -%>
5
16
  import { HeadProvider } from "@canonical/react-head";
6
17
  import { createBrowserAdapter, createRouter } from "@canonical/router-core";
18
+ <%# Without readDehydratedState the specifier list fits on one line, which is -%>
19
+ <%# what the formatter insists on — so the SPA arm cannot share this block. -%>
20
+ <% if (spa) { -%>
21
+ import { Outlet, RouterProvider } from "@canonical/router-react";
22
+ <% } else { -%>
7
23
  import {
8
24
  Outlet,
9
25
  <% if (intl) { -%>
@@ -14,7 +30,12 @@ import {
14
30
  RouterProvider,
15
31
  <% } -%>
16
32
  } from "@canonical/router-react";
33
+ <% } -%>
34
+ <% if (spa) { -%>
35
+ import { createRoot } from "react-dom/client";
36
+ <% } else { -%>
17
37
  import { hydrateRoot } from "react-dom/client";
38
+ <% } -%>
18
39
  <% if (relay) { -%>
19
40
  import { RelayEnvironmentProvider } from "react-relay";
20
41
  <% } -%>
@@ -24,6 +45,12 @@ import { catalogs, i18nConfig } from "#i18n/index.js";
24
45
  <% if (relay) { -%>
25
46
  import { getBrowserEnvironment } from "#relay/environment.js";
26
47
  <% } -%>
48
+ import formatDocumentTitle from "../formatDocumentTitle.js";
49
+ <%# Same story: the SPA arm never pulls the two SSR payload symbols, so the three -%>
50
+ <%# base names fit on one line and the formatter requires them there. -%>
51
+ <% if (spa) { -%>
52
+ import { appRoutes, middleware, notFoundRoute } from "../routes.js";
53
+ <% } else { -%>
27
54
  import {
28
55
  appRoutes,
29
56
  middleware,
@@ -33,8 +60,16 @@ import {
33
60
  type SerializedRelayPayload,
34
61
  <% } -%>
35
62
  } from "../routes.js";
36
- import "#styles/index.css";
37
- <% if (relay) { -%>
63
+ <% } -%>
64
+ <% if (relay && spa) { -%>
65
+
66
+ // Claim the browser-session Relay environment FIRST — before the router exists
67
+ // — so the first `useLazyLoadQuery` and any warm hook fired during router
68
+ // construction share one store. A client-only app has no server-captured
69
+ // payload to seed from, so the store starts empty and the first query fetches.
70
+ const relayEnvironment = getBrowserEnvironment();
71
+ <% } -%>
72
+ <% if (relay && !spa) { -%>
38
73
 
39
74
  // Seed the browser-session Relay environment FIRST — before the router exists
40
75
  // — from the server-captured payloads riding __INITIAL_DATA__ (SSR pages) so
@@ -54,6 +89,15 @@ const relayEnvironment = getBrowserEnvironment({
54
89
  });
55
90
  <% } -%>
56
91
 
92
+ <% if (spa) { -%>
93
+ // Nothing was rendered server-side, so there is no dehydrated state to resume
94
+ // from: the router always performs a normal initial load.
95
+ const router = createRouter(appRoutes, {
96
+ adapter: createBrowserAdapter(),
97
+ middleware: [...middleware],
98
+ notFound: notFoundRoute,
99
+ });
100
+ <% } else { -%>
57
101
  // On SSR pages __INITIAL_DATA__ carries the flat dehydrated router state
58
102
  // (href/kind/routeId/status); hydrating from it resumes the server-rendered
59
103
  // match and skips the duplicate initial load. In the SPA cells there is no
@@ -65,8 +109,24 @@ const router = createRouter(appRoutes, {
65
109
  notFound: notFoundRoute,
66
110
  hydratedState: readDehydratedState() ?? undefined,
67
111
  });
112
+ <% } -%>
68
113
 
69
- <% if (intl) { -%>
114
+ <% if (intl && spa) { -%>
115
+ /**
116
+ * Resolve the locale for the first client render. Nothing negotiated it
117
+ * server-side, so the pure negotiation runs here: the cookie wins, then the
118
+ * browser's language list (the client-side stand-in for the Accept-Language
119
+ * header).
120
+ */
121
+ function resolveInitialLocale(): string {
122
+ return negotiateLocale(i18nConfig, {
123
+ cookieHeader: document.cookie,
124
+ acceptLanguage: navigator.languages?.join(",") ?? navigator.language,
125
+ });
126
+ }
127
+
128
+ <% } -%>
129
+ <% if (intl && !spa) { -%>
70
130
  /**
71
131
  * Resolve the locale for the first client render.
72
132
  *
@@ -94,16 +154,21 @@ if (!root) {
94
154
  throw new Error('Root element "#root" not found');
95
155
  }
96
156
 
97
- <% if (relay) { -%>
157
+ <%# One mount site, four provider stacks. The stacks differ only by nesting -%>
158
+ <%# depth, so they cannot share a body without changing every indent. -%>
159
+ <% if (spa) { -%>
160
+ createRoot(root).render(
161
+ <% } else { -%>
98
162
  hydrateRoot(
99
163
  root,
100
- <% if (intl) { -%>
164
+ <% } -%>
165
+ <% if (relay && intl) { -%>
101
166
  <I18nProvider
102
167
  config={i18nConfig}
103
168
  catalogs={catalogs}
104
169
  locale={resolveInitialLocale()}
105
170
  >
106
- <HeadProvider>
171
+ <HeadProvider titleTemplate={formatDocumentTitle}>
107
172
  <RelayEnvironmentProvider environment={relayEnvironment}>
108
173
  <RouterProvider router={router}>
109
174
  <Outlet fallback={<p>Loading…</p>} />
@@ -111,37 +176,31 @@ hydrateRoot(
111
176
  </RelayEnvironmentProvider>
112
177
  </HeadProvider>
113
178
  </I18nProvider>,
114
- <% } else { -%>
115
- <HeadProvider>
179
+ <% } else if (relay) { -%>
180
+ <HeadProvider titleTemplate={formatDocumentTitle}>
116
181
  <RelayEnvironmentProvider environment={relayEnvironment}>
117
182
  <RouterProvider router={router}>
118
183
  <Outlet fallback={<p>Loading…</p>} />
119
184
  </RouterProvider>
120
185
  </RelayEnvironmentProvider>
121
186
  </HeadProvider>,
122
- <% } -%>
123
- );
124
- <% } else { -%>
125
- hydrateRoot(
126
- root,
127
- <% if (intl) { -%>
187
+ <% } else if (intl) { -%>
128
188
  <I18nProvider
129
189
  config={i18nConfig}
130
190
  catalogs={catalogs}
131
191
  locale={resolveInitialLocale()}
132
192
  >
133
- <HeadProvider>
193
+ <HeadProvider titleTemplate={formatDocumentTitle}>
134
194
  <RouterProvider router={router}>
135
195
  <Outlet fallback={<p>Loading…</p>} />
136
196
  </RouterProvider>
137
197
  </HeadProvider>
138
198
  </I18nProvider>,
139
199
  <% } else { -%>
140
- <HeadProvider>
200
+ <HeadProvider titleTemplate={formatDocumentTitle}>
141
201
  <RouterProvider router={router}>
142
202
  <Outlet fallback={<p>Loading…</p>} />
143
203
  </RouterProvider>
144
204
  </HeadProvider>,
145
205
  <% } -%>
146
206
  );
147
- <% } -%>
@@ -1,23 +1,22 @@
1
1
  <% if (intl) { -%>
2
2
  import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
- import { useHead } from "@canonical/react-head";
4
+ import { Head } from "@canonical/react-head";
5
5
  import type { ReactElement } from "react";
6
6
 
7
7
  export default function AccountPage(): ReactElement {
8
8
  <% if (intl) { -%>
9
9
  const { t } = useTranslation();
10
- useHead({ title: t("account.title") });
11
- <% } else { -%>
12
- useHead({ title: "Account — Boilerplate" });
13
- <% } -%>
14
10
 
11
+ <% } -%>
15
12
  return (
16
13
  <section aria-labelledby="account-title">
17
14
  <% if (intl) { -%>
15
+ <Head title={t("account.title")} />
18
16
  <h1 id="account-title">{t("account.heading")}</h1>
19
17
  <p>{t("account.body")}</p>
20
18
  <% } else { -%>
19
+ <Head title="Account" />
21
20
  <h1 id="account-title">Account</h1>
22
21
  <p>Protected account page. You are signed in.</p>
23
22
  <% } -%>
@@ -1,7 +1,7 @@
1
1
  <% if (intl) { -%>
2
2
  import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
- import { useHead } from "@canonical/react-head";
4
+ import { Head } from "@canonical/react-head";
5
5
  import type { ReactElement } from "react";
6
6
 
7
7
  interface LoginSearch {
@@ -15,20 +15,19 @@ export default function LoginPage({
15
15
  }): ReactElement {
16
16
  <% if (intl) { -%>
17
17
  const { t } = useTranslation();
18
- useHead({ title: t("login.title") });
19
- <% } else { -%>
20
- useHead({ title: "Login — Boilerplate" });
21
- <% } -%>
22
18
 
19
+ <% } -%>
23
20
  return (
24
21
  <section aria-labelledby="login-title">
25
22
  <% if (intl) { -%>
23
+ <Head title={t("login.title")} />
26
24
  <h1 id="login-title">{t("login.heading")}</h1>
27
25
  {/*
28
26
  The `?auth=1` literal is markup (<code>), which a plain message string
29
27
  cannot carry, so the sentence is catalogued as the fragments around it.
30
28
  */}
31
29
  <% } else { -%>
30
+ <Head title="Login" />
32
31
  <h1 id="login-title">Login</h1>
33
32
  <% } -%>
34
33
  <p>
@@ -1,7 +1,7 @@
1
1
  <% if (intl) { -%>
2
2
  import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
- import { useHead } from "@canonical/react-head";
4
+ import { Head } from "@canonical/react-head";
5
5
  import { type ReactElement, Suspense } from "react";
6
6
  import { useRelayEnvironment } from "react-relay";
7
7
  import { createOperationDescriptor, getRequest } from "relay-runtime";
@@ -11,11 +11,8 @@ import ProductList, { PAGE_SIZE, productListQuery } from "./ProductList.js";
11
11
  export default function CatalogPage(): ReactElement {
12
12
  <% if (intl) { -%>
13
13
  const { t } = useTranslation();
14
- useHead({ title: t("catalog.title") });
15
- <% } else { -%>
16
- useHead({ title: "Catalog — Boilerplate" });
17
- <% } -%>
18
14
 
15
+ <% } -%>
19
16
  const environment = useRelayEnvironment();
20
17
  const canRenderQuery =
21
18
  typeof document !== "undefined" ||
@@ -28,12 +25,14 @@ export default function CatalogPage(): ReactElement {
28
25
  return (
29
26
  <section aria-labelledby="catalog-title">
30
27
  <% if (intl) { -%>
28
+ <Head title={t("catalog.title")} />
31
29
  <h1 id="catalog-title">{t("catalog.heading")}</h1>
32
30
  {/*
33
31
  Developer documentation rather than user-facing copy (file names,
34
32
  APIs), so it deliberately stays out of the message catalogs.
35
33
  */}
36
34
  <% } else { -%>
35
+ <Head title="Catalog" />
37
36
  <h1 id="catalog-title">Catalog</h1>
38
37
  <% } -%>
39
38
  <p>
@@ -44,6 +43,15 @@ export default function CatalogPage(): ReactElement {
44
43
  <code>src/relay/schema.ts</code> — set <code>VITE_GRAPHQL_URL</code> to
45
44
  point it at a real endpoint instead.
46
45
  </p>
46
+ <% if (spa) { -%>
47
+ {/*
48
+ There is no server to prefetch against, so `useLazyLoadQuery`
49
+ (store-or-network) always goes to the network on first render and
50
+ suspends until it resolves. `canRenderQuery` below is therefore
51
+ always true here — it exists for the SSR arm, where the server may
52
+ have nothing seeded to render from.
53
+ */}
54
+ <% } else { -%>
47
55
  {/*
48
56
  SSR: the server prefetches this route's declared query (see
49
57
  `serverQueries` in src/routes.tsx) and seeds both render environments
@@ -53,6 +61,7 @@ export default function CatalogPage(): ReactElement {
53
61
  second fetch. If the prefetch failed, the query suspends and fetches
54
62
  through the active environment instead.
55
63
  */}
64
+ <% } -%>
56
65
  {/*
57
66
  The canonical Relay pairing: Suspense renders the pending state while
58
67
  `useLazyLoadQuery` is in flight, and the ErrorBoundary renders the
@@ -60,6 +69,7 @@ export default function CatalogPage(): ReactElement {
60
69
  `VITE_GRAPHQL_URL` mode) — without it a thrown query error would
61
70
  unmount the whole tree to a blank page.
62
71
  */}
72
+ <% if (!spa) { -%>
63
73
  {/*
64
74
  On the server, the query renders only when the SSR prefetch already
65
75
  seeded the store — after a failed or timed-out prefetch, querying here
@@ -68,6 +78,7 @@ export default function CatalogPage(): ReactElement {
68
78
  markup matches, since a client without payloads paints the same
69
79
  fallback via Suspense first.
70
80
  */}
81
+ <% } -%>
71
82
  {canRenderQuery ? (
72
83
  <% if (intl) { -%>
73
84
  <ErrorBoundary fallback={<p role="alert">{t("catalog.error")}</p>}>
@@ -34,8 +34,8 @@ export const productListQuery = graphql`
34
34
  * renders a `ProductCard` (a `useFragment` child) per edge.
35
35
  *
36
36
  * The hook suspends while the query is in flight, so render this component
37
- * inside a `Suspense` boundary — and, until the SSR data-serialization PR
38
- * lands, only on the client (see `CatalogPage`).
37
+ * inside a `Suspense` boundary. Whether it may render at all is
38
+ * `CatalogPage`'s call: on the server it renders only from a seeded store.
39
39
  */
40
40
  export default function ProductList(): ReactElement {
41
41
  <% if (intl) { -%>
@@ -3,7 +3,7 @@ import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
4
  import { Button } from "@canonical/react-ds-global";
5
5
  import { Field, Form } from "@canonical/react-ds-global-form";
6
- import { useHead } from "@canonical/react-head";
6
+ import { Head } from "@canonical/react-head";
7
7
  import type { ReactElement } from "react";
8
8
 
9
9
  function handleSubmit(data: Record<string, unknown>) {
@@ -13,16 +13,15 @@ function handleSubmit(data: Record<string, unknown>) {
13
13
  export default function ContactPage(): ReactElement {
14
14
  <% if (intl) { -%>
15
15
  const { t } = useTranslation();
16
- useHead({ title: t("contact.title") });
17
- <% } else { -%>
18
- useHead({ title: "Contact" });
19
- <% } -%>
20
16
 
17
+ <% } -%>
21
18
  return (
22
19
  <section aria-labelledby="contact-title">
23
20
  <% if (intl) { -%>
21
+ <Head title={t("contact.title")} />
24
22
  <h1 id="contact-title">{t("contact.heading")}</h1>
25
23
  <% } else { -%>
24
+ <Head title="Contact" />
26
25
  <h1 id="contact-title">Contact</h1>
27
26
  <% } -%>
28
27
  <Form onSubmit={handleSubmit}>
@@ -1,7 +1,7 @@
1
1
  <% if (intl) { -%>
2
2
  import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
- import { useHead } from "@canonical/react-head";
4
+ import { Head } from "@canonical/react-head";
5
5
  import type { ReactElement } from "react";
6
6
 
7
7
  export default function GuidePage({
@@ -10,18 +10,12 @@ export default function GuidePage({
10
10
  params: { slug: string };
11
11
  }): ReactElement {
12
12
  <% if (intl) { -%>
13
- const { t, locale } = useTranslation();
14
- // `locale` is a dependency: switching languages must retranslate the title.
15
- useHead({ title: t("guide.title", { slug: params.slug }) }, [
16
- params.slug,
17
- locale,
18
- ]);
19
- <% } else { -%>
20
- useHead({ title: `${params.slug} — Guides` }, [params.slug]);
21
- <% } -%>
13
+ const { t } = useTranslation();
22
14
 
15
+ <% } -%>
23
16
  return (
24
17
  <section aria-labelledby="guide-title">
18
+ <Head title={params.slug} />
25
19
  <h1 id="guide-title">{params.slug}</h1>
26
20
  <% if (intl) { -%>
27
21
  <p>{t("guide.body", { slug: params.slug })}</p>
@@ -1,24 +1,23 @@
1
1
  <% if (intl) { -%>
2
2
  import { useTranslation } from "@canonical/i18n-react";
3
3
  <% } -%>
4
- import { useHead } from "@canonical/react-head";
4
+ import { Head } from "@canonical/react-head";
5
5
  import { type ReactElement, Suspense } from "react";
6
6
  import { ExampleComponent, LazyComponent } from "#lib/index.js";
7
7
 
8
8
  export default function HomePage(): ReactElement {
9
9
  <% if (intl) { -%>
10
10
  const { t } = useTranslation();
11
- useHead({ title: t("home.title") });
12
- <% } else { -%>
13
- useHead({ title: "Home — Boilerplate" });
14
- <% } -%>
15
11
 
12
+ <% } -%>
16
13
  return (
17
14
  <section aria-labelledby="home-title">
18
15
  <% if (intl) { -%>
16
+ <Head title={t("home.title")} />
19
17
  <h1 id="home-title">{t("home.heading")}</h1>
20
18
  <p>{t("home.tagline")}</p>
21
19
  <% } else { -%>
20
+ <Head title="Home" />
22
21
  <h1 id="home-title">Home</h1>
23
22
  <p>Welcome to the pragma router boilerplate.</p>
24
23
  <% } -%>
@@ -45,12 +44,21 @@ export default function HomePage(): ReactElement {
45
44
  <% } else { -%>
46
45
  <h2>Streaming with Suspense</h2>
47
46
  <% } -%>
47
+ <% if (spa) { -%>
48
+ <p>
49
+ <code>LazyComponent</code> reads a synthetic delayed fetch with React 19{" "}
50
+ <code>use()</code>, so it suspends. Inside this <code>Suspense</code>{" "}
51
+ boundary the rest of the page renders immediately and the fallback
52
+ stands in until the resolved content swaps itself back in.
53
+ </p>
54
+ <% } else { -%>
48
55
  <p>
49
56
  <code>LazyComponent</code> reads a synthetic delayed fetch with React 19{" "}
50
57
  <code>use()</code>, so it suspends. Inside this <code>Suspense</code>{" "}
51
58
  boundary the server streams the page shell first, shows the fallback,
52
59
  then flushes the resolved content in — no full client round-trip.
53
60
  </p>
61
+ <% } -%>
54
62
  <% if (intl) { -%>
55
63
  <Suspense fallback={<p>{t("home.streamFallback")}</p>}>
56
64
  <% } else { -%>
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Compose a page's own title into the document title.
3
+ *
4
+ * Every page names only itself — `<Head title="Account" />` — and the
5
+ * application name is appended here, once. That keeps the name out of every
6
+ * message catalog, and it is what makes a page added later by
7
+ * `summon route <domain>/<name>` (whose scaffolded title is a bare noun) title
8
+ * itself consistently with the rest of the app.
9
+ <% if (spa) { -%>
10
+ *
11
+ * `HeadProvider` in the client entry is what installs it.
12
+ <% } else { -%>
13
+ *
14
+ * The client and the server entries must pass the same function to
15
+ * `HeadProvider`, or the server-rendered title would not match the one the
16
+ * client renders after hydration; both import this module for that reason.
17
+ <% } -%>
18
+ */
19
+ export default function formatDocumentTitle(pageTitle: string): string {
20
+ return `${pageTitle} — <%= name %>`;
21
+ }
@@ -23,18 +23,17 @@ const ar: Record<MessageKey, MessageValue> = {
23
23
  "locale.label": "اللغة",
24
24
  "notFound.heading": "الصفحة غير موجودة",
25
25
  "notFound.body": "الصفحة التي تبحث عنها غير موجودة.",
26
- "home.title": "الرئيسية — Boilerplate",
26
+ "home.title": "الرئيسية",
27
27
  "home.heading": "الرئيسية",
28
28
  "home.tagline": "مرحبًا بك في تطبيق pragma النموذجي.",
29
29
  "home.exampleHeading": "مكوّن مثال",
30
30
  "home.streamingHeading": "البث مع Suspense",
31
31
  "home.streamFallback": "جارٍ تحميل المحتوى المتدفق…",
32
- "guide.title": "{slug} — أدلة",
33
32
  "guide.body": "محتوى الدليل عن {slug}.",
34
- "account.title": "الحساب — Boilerplate",
33
+ "account.title": "الحساب",
35
34
  "account.heading": "الحساب",
36
35
  "account.body": "صفحة حساب محمية. أنت مسجّل الدخول.",
37
- "login.title": "تسجيل الدخول — Boilerplate",
36
+ "login.title": "تسجيل الدخول",
38
37
  "login.heading": "تسجيل الدخول",
39
38
  "login.hintBefore": "تسجيل دخول تجريبي. أضف",
40
39
  "login.hintAfter": "إلى أي عنوان محمي لمحاكاة المصادقة.",
@@ -51,7 +50,7 @@ const ar: Record<MessageKey, MessageValue> = {
51
50
  "contact.message": "الرسالة",
52
51
  "contact.messageHint": "500 حرف كحد أقصى",
53
52
  "contact.send": "إرسال الرسالة",
54
- "catalog.title": "الكتالوج — Boilerplate",
53
+ "catalog.title": "الكتالوج",
55
54
  "catalog.heading": "الكتالوج",
56
55
  "catalog.loading": "جارٍ تحميل الكتالوج…",
57
56
  "catalog.error": "تعذّر تحميل الكتالوج. أعد تحميل الصفحة للمحاولة مرة أخرى.",
@@ -23,18 +23,17 @@ const en = {
23
23
  "locale.label": "Language",
24
24
  "notFound.heading": "Page not found",
25
25
  "notFound.body": "The page you are looking for does not exist.",
26
- "home.title": "Home — Boilerplate",
26
+ "home.title": "Home",
27
27
  "home.heading": "Home",
28
28
  "home.tagline": "Welcome to the pragma router boilerplate.",
29
29
  "home.exampleHeading": "Example component",
30
30
  "home.streamingHeading": "Streaming with Suspense",
31
31
  "home.streamFallback": "Loading streamed content…",
32
- "guide.title": "{slug} — Guides",
33
32
  "guide.body": "Guide content for {slug}.",
34
- "account.title": "Account — Boilerplate",
33
+ "account.title": "Account",
35
34
  "account.heading": "Account",
36
35
  "account.body": "Protected account page. You are signed in.",
37
- "login.title": "Login — Boilerplate",
36
+ "login.title": "Login",
38
37
  "login.heading": "Login",
39
38
  "login.hintBefore": "Demo login. Add",
40
39
  "login.hintAfter": "to any protected URL to simulate authentication.",
@@ -51,7 +50,7 @@ const en = {
51
50
  "contact.message": "Message",
52
51
  "contact.messageHint": "Maximum 500 characters",
53
52
  "contact.send": "Send message",
54
- "catalog.title": "Catalog — Boilerplate",
53
+ "catalog.title": "Catalog",
55
54
  "catalog.heading": "Catalog",
56
55
  "catalog.loading": "Loading catalog…",
57
56
  "catalog.error": "The catalog failed to load. Reload the page to try again.",