@canonical/summon-application 0.34.0 → 0.35.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.
- package/README.md +9 -9
- package/dist/esm/application/react/index.js +403 -277
- package/dist/esm/application/react/index.js.map +1 -1
- package/dist/esm/application/react/templates/.storybook/decorators/index.ts.ejs +4 -0
- package/dist/esm/application/react/templates/.storybook/decorators/withI18n.tsx +35 -0
- package/dist/esm/application/react/templates/.storybook/decorators/withRouter.tsx +18 -20
- package/dist/esm/application/react/templates/.storybook/preview.ts.ejs +35 -0
- package/dist/esm/application/react/templates/index.html.ejs +2 -1
- package/dist/esm/application/react/templates/package.json.ejs +34 -3
- package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +109 -10
- package/dist/esm/application/react/templates/src/domains/account/{AccountPage.tsx → AccountPage.tsx.ejs} +13 -0
- package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx.ejs +51 -0
- package/dist/esm/application/react/templates/src/domains/account/routes.ts +21 -8
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +97 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ErrorBoundary.tests.tsx → ErrorBoundary.tests.tsx.ejs} +24 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductCard.tsx → ProductCard.tsx.ejs} +31 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.stories.tsx → ProductList.stories.tsx.ejs} +63 -4
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tests.tsx → ProductList.tests.tsx.ejs} +44 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tsx → ProductList.tsx.ejs} +30 -2
- package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +25 -0
- package/dist/esm/application/react/templates/src/domains/contact/{ContactPage.tsx → ContactPage.tsx.ejs} +40 -0
- package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx.ejs +33 -0
- package/dist/esm/application/react/templates/src/domains/marketing/{HomePage.tsx → HomePage.tsx.ejs} +30 -0
- package/dist/esm/application/react/templates/src/domains/marketing/routes.ts +25 -0
- package/dist/esm/application/react/templates/src/i18n/ar.ts +74 -0
- package/dist/esm/application/react/templates/src/i18n/catalogs.ts +14 -0
- package/dist/esm/application/react/templates/src/i18n/config.ts +19 -0
- package/dist/esm/application/react/templates/src/i18n/en.ts +73 -0
- package/dist/esm/application/react/templates/src/i18n/fr.ts +70 -0
- package/dist/esm/application/react/templates/src/i18n/index.ts +5 -0
- package/dist/esm/application/react/templates/src/i18n/negotiation.tests.ts +92 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tests.tsx +60 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tsx +44 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/index.ts +1 -0
- package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +34 -0
- package/dist/esm/application/react/templates/src/lib/ThemeSelector/{ThemeSelector.tsx → ThemeSelector.tsx.ejs} +16 -0
- package/dist/esm/application/react/templates/src/lib/index.ts.ejs +3 -0
- package/dist/esm/application/react/templates/src/relay/environment.ts +153 -5
- package/dist/esm/application/react/templates/src/routes.tsx.ejs +137 -33
- package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +293 -11
- package/dist/esm/application/react/templates/src/server/preview.bun.ts +30 -7
- package/dist/esm/application/react/templates/src/server/preview.express.ts +19 -4
- package/dist/esm/application/react/templates/src/server/renderer.tsx.ejs +125 -0
- package/dist/esm/application/react/templates/src/server/{server.bun.ts → server.bun.ts.ejs} +56 -4
- package/dist/esm/application/react/templates/src/server/{server.express.ts → server.express.ts.ejs} +48 -4
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +197 -0
- package/dist/esm/application/react/templates/tsconfig.json.ejs +3 -0
- package/dist/esm/application/react/templates/vite.config.ts.ejs +5 -0
- package/dist/esm/domain/index.js +8 -1
- package/dist/esm/domain/index.js.map +1 -1
- package/dist/esm/route/index.js +12 -2
- package/dist/esm/route/index.js.map +1 -1
- package/dist/esm/route/insertRoute.js +39 -11
- package/dist/esm/route/insertRoute.js.map +1 -1
- package/dist/esm/shared/packageName.js +7 -0
- package/dist/esm/shared/packageName.js.map +1 -0
- package/dist/esm/shared/packageVersion.js +50 -0
- package/dist/esm/shared/packageVersion.js.map +1 -0
- package/dist/esm/shared/validators.js +61 -0
- package/dist/esm/shared/validators.js.map +1 -0
- package/dist/esm/shared/versions.js +87 -55
- package/dist/esm/shared/versions.js.map +1 -1
- package/dist/esm/shared/workspace.js +57 -0
- package/dist/esm/shared/workspace.js.map +1 -1
- package/dist/esm/wrapper/index.js +8 -1
- package/dist/esm/wrapper/index.js.map +1 -1
- package/dist/types/application/react/index.d.ts +1 -2
- package/dist/types/application/react/index.d.ts.map +1 -1
- package/dist/types/domain/index.d.ts.map +1 -1
- package/dist/types/route/index.d.ts.map +1 -1
- package/dist/types/shared/packageName.d.ts +7 -0
- package/dist/types/shared/packageName.d.ts.map +1 -0
- package/dist/types/shared/packageVersion.d.ts +15 -0
- package/dist/types/shared/packageVersion.d.ts.map +1 -0
- package/dist/types/shared/validators.d.ts +21 -0
- package/dist/types/shared/validators.d.ts.map +1 -0
- package/dist/types/shared/versions.d.ts +14 -5
- package/dist/types/shared/versions.d.ts.map +1 -1
- package/dist/types/shared/workspace.d.ts.map +1 -1
- package/dist/types/wrapper/index.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/esm/application/react/templates/.storybook/decorators/index.ts +0 -1
- package/dist/esm/application/react/templates/.storybook/preview.ts +0 -10
- package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx +0 -27
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +0 -50
- package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -17
- package/dist/esm/application/react/templates/src/server/renderer.tsx +0 -50
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts +0 -99
|
@@ -13,10 +13,14 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
|
+
createOperationDescriptor,
|
|
16
17
|
Environment,
|
|
17
18
|
type FetchFunction,
|
|
18
19
|
type GraphQLResponse,
|
|
20
|
+
type GraphQLTaggedNode,
|
|
21
|
+
getRequest,
|
|
19
22
|
Network,
|
|
23
|
+
type PayloadData,
|
|
20
24
|
RecordSource,
|
|
21
25
|
Store,
|
|
22
26
|
} from "relay-runtime";
|
|
@@ -25,10 +29,28 @@ import {
|
|
|
25
29
|
httpExecutor,
|
|
26
30
|
localGraphExecutor,
|
|
27
31
|
type RelayFetchContext,
|
|
32
|
+
type RelayFetchExecutor,
|
|
28
33
|
type RelayRuntimeFetch,
|
|
29
34
|
urlMiddleware,
|
|
30
35
|
} from "relay-runtime-network";
|
|
31
36
|
|
|
37
|
+
/**
|
|
38
|
+
* A server-captured operation ready to replay: the resolved query node plus
|
|
39
|
+
* the variables and raw response `data` captured when the server executed it.
|
|
40
|
+
*/
|
|
41
|
+
export interface RelaySeedPayload {
|
|
42
|
+
readonly query: GraphQLTaggedNode;
|
|
43
|
+
readonly variables: Record<string, unknown>;
|
|
44
|
+
readonly data: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A response observed by the network, in the serializable wire shape. */
|
|
48
|
+
export interface CapturedResponse {
|
|
49
|
+
readonly id: string;
|
|
50
|
+
readonly variables: Record<string, unknown>;
|
|
51
|
+
readonly data: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
|
|
32
54
|
/** Options for {@link createEnvironment}. */
|
|
33
55
|
export interface CreateEnvironmentOptions {
|
|
34
56
|
/**
|
|
@@ -36,6 +58,20 @@ export interface CreateEnvironmentOptions {
|
|
|
36
58
|
* neither is set the environment executes against the local mock schema.
|
|
37
59
|
*/
|
|
38
60
|
readonly graphqlUrl?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Server-captured operations replayed into the store at construction via
|
|
63
|
+
* Relay's public `commitPayload` — only these operations' data ever crosses
|
|
64
|
+
* the SSR boundary (no whole-store serialization, nothing to scrub). Each
|
|
65
|
+
* replayed operation is retained, so the seeded data cannot be GC'd before
|
|
66
|
+
* its first reader mounts.
|
|
67
|
+
*/
|
|
68
|
+
readonly payloads?: readonly RelaySeedPayload[];
|
|
69
|
+
/**
|
|
70
|
+
* Observe each successful single response the environment's network
|
|
71
|
+
* returns. The SSR prefetch uses this to tee responses into the
|
|
72
|
+
* serializable payload list while `fetchQuery` normalizes them as usual.
|
|
73
|
+
*/
|
|
74
|
+
readonly captureResponse?: (captured: CapturedResponse) => void;
|
|
39
75
|
}
|
|
40
76
|
|
|
41
77
|
/** Reads the endpoint URL from Vite's env, treating the empty string as unset. */
|
|
@@ -75,11 +111,46 @@ const createLocalNetwork = () =>
|
|
|
75
111
|
},
|
|
76
112
|
});
|
|
77
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Wraps an executor so a transport failure resolves as a well-formed GraphQL
|
|
116
|
+
* error payload instead of a thrown exception. The pipeline's fetch context
|
|
117
|
+
* creates an internal incremental-payload promise that no non-incremental
|
|
118
|
+
* consumer ever awaits; an executor throw rejects it unhandled (fatal under
|
|
119
|
+
* Bun). Converting the failure at the source means nothing in the pipeline
|
|
120
|
+
* ever rejects, while Relay still surfaces it as an operation error.
|
|
121
|
+
*/
|
|
122
|
+
const safeExecutor = (executor: RelayFetchExecutor): RelayFetchExecutor =>
|
|
123
|
+
Object.assign(
|
|
124
|
+
async (context: RelayFetchContext) => {
|
|
125
|
+
try {
|
|
126
|
+
return await executor(context);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.warn(
|
|
129
|
+
"[relay] transport failure converted to error payload:",
|
|
130
|
+
error,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
payload: {
|
|
135
|
+
errors: [
|
|
136
|
+
{
|
|
137
|
+
message: error instanceof Error ? error.message : String(error),
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
response: null,
|
|
142
|
+
status: null,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
{ descriptor: executor.descriptor },
|
|
147
|
+
);
|
|
148
|
+
|
|
78
149
|
/** Builds the HTTP network that posts operations to `graphqlUrl`. */
|
|
79
150
|
const createHttpNetwork = (graphqlUrl: string) =>
|
|
80
151
|
createRelayRuntimeNetwork({
|
|
81
152
|
fetch: {
|
|
82
|
-
executor: httpExecutor(),
|
|
153
|
+
executor: safeExecutor(httpExecutor()),
|
|
83
154
|
middlewares: [urlMiddleware({ url: graphqlUrl })],
|
|
84
155
|
},
|
|
85
156
|
});
|
|
@@ -93,11 +164,20 @@ const createHttpNetwork = (graphqlUrl: string) =>
|
|
|
93
164
|
*/
|
|
94
165
|
const toFetchFunction =
|
|
95
166
|
(fetchGraphQL: RelayRuntimeFetch): FetchFunction =>
|
|
96
|
-
(params, variables, cacheConfig) =>
|
|
97
|
-
fetchGraphQL(params, variables, {
|
|
167
|
+
(params, variables, cacheConfig) => {
|
|
168
|
+
const response = fetchGraphQL(params, variables, {
|
|
98
169
|
...cacheConfig,
|
|
99
170
|
}) as Promise<GraphQLResponse>;
|
|
100
171
|
|
|
172
|
+
// The pipeline starts the request at call time, before Relay's observable
|
|
173
|
+
// attaches its handlers — guard the raw promise so a rejection that beats
|
|
174
|
+
// (or never gains) a subscriber cannot crash the process. Consumers still
|
|
175
|
+
// observe the same rejection through the returned promise.
|
|
176
|
+
response.catch(() => {});
|
|
177
|
+
|
|
178
|
+
return response;
|
|
179
|
+
};
|
|
180
|
+
|
|
101
181
|
/**
|
|
102
182
|
* Creates a Relay `Environment` for the app.
|
|
103
183
|
*
|
|
@@ -112,8 +192,76 @@ export const createEnvironment = (
|
|
|
112
192
|
? createHttpNetwork(graphqlUrl)
|
|
113
193
|
: createLocalNetwork();
|
|
114
194
|
|
|
115
|
-
|
|
116
|
-
|
|
195
|
+
// toFetchFunction always yields a promise (see its cast); the narrower
|
|
196
|
+
// alias lets the capture wrapper await it without widening back into
|
|
197
|
+
// FetchFunction's observable-bearing return union.
|
|
198
|
+
const baseFetch = toFetchFunction(network.fetch) as (
|
|
199
|
+
...args: Parameters<FetchFunction>
|
|
200
|
+
) => Promise<GraphQLResponse>;
|
|
201
|
+
const capture = options.captureResponse;
|
|
202
|
+
const fetchFn: FetchFunction = capture
|
|
203
|
+
? async (params, variables, cacheConfig, uploadables) => {
|
|
204
|
+
const response = await baseFetch(
|
|
205
|
+
params,
|
|
206
|
+
variables,
|
|
207
|
+
cacheConfig,
|
|
208
|
+
uploadables,
|
|
209
|
+
);
|
|
210
|
+
// Batched responses are never produced by either executor; capture
|
|
211
|
+
// only well-formed single responses that carry data and a name.
|
|
212
|
+
if (
|
|
213
|
+
!Array.isArray(response) &&
|
|
214
|
+
"data" in response &&
|
|
215
|
+
response.data &&
|
|
216
|
+
params.name
|
|
217
|
+
) {
|
|
218
|
+
capture({
|
|
219
|
+
id: params.name,
|
|
220
|
+
variables,
|
|
221
|
+
data: response.data as unknown as Record<string, unknown>,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return response;
|
|
226
|
+
}
|
|
227
|
+
: baseFetch;
|
|
228
|
+
|
|
229
|
+
const environment = new Environment({
|
|
230
|
+
network: Network.create(fetchFn),
|
|
117
231
|
store: new Store(new RecordSource()),
|
|
118
232
|
});
|
|
233
|
+
|
|
234
|
+
for (const seed of options.payloads ?? []) {
|
|
235
|
+
const operation = createOperationDescriptor(
|
|
236
|
+
getRequest(seed.query),
|
|
237
|
+
seed.variables,
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
environment.commitPayload(operation, seed.data as PayloadData);
|
|
241
|
+
// Explicit retention — seeded data must survive until its first reader
|
|
242
|
+
// mounts and takes over the retain; no reliance on GC timing.
|
|
243
|
+
environment.retain(operation);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return environment;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
let browserEnvironment: Environment | null = null;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The browser-session Relay environment, created lazily on first use.
|
|
253
|
+
*
|
|
254
|
+
* Module scope so the client entry's provider and route-level `warm` hooks
|
|
255
|
+
* share one normalized store — a navigation-time cache warm lands in the same
|
|
256
|
+
* store `useLazyLoadQuery` reads from. The FIRST caller's options win (the
|
|
257
|
+
* client entry seeds SSR payloads before anything else runs); later calls
|
|
258
|
+
* reuse the existing environment. Server code must keep using
|
|
259
|
+
* `createEnvironment()` (fresh per request).
|
|
260
|
+
*/
|
|
261
|
+
export const getBrowserEnvironment = (
|
|
262
|
+
options?: CreateEnvironmentOptions,
|
|
263
|
+
): Environment => {
|
|
264
|
+
browserEnvironment ??= createEnvironment(options);
|
|
265
|
+
|
|
266
|
+
return browserEnvironment;
|
|
119
267
|
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import {
|
|
2
5
|
type AnyRoute,
|
|
3
6
|
group,
|
|
@@ -11,6 +14,8 @@ import {
|
|
|
11
14
|
import type { ReactElement, ReactNode } from "react";
|
|
12
15
|
import accountRoutes from "#domains/account/routes.js";
|
|
13
16
|
<% if (relay) { -%>
|
|
17
|
+
import { getRequest } from "relay-runtime";
|
|
18
|
+
import { PAGE_SIZE, productListQuery } from "#domains/catalog/ProductList.js";
|
|
14
19
|
import catalogRoutes from "#domains/catalog/routes.js";
|
|
15
20
|
<% } -%>
|
|
16
21
|
<% if (forms) { -%>
|
|
@@ -18,6 +23,9 @@ import contactRoutes from "#domains/contact/routes.js";
|
|
|
18
23
|
<% } -%>
|
|
19
24
|
import marketingRoutes from "#domains/marketing/routes.js";
|
|
20
25
|
import Navigation from "#lib/Navigation/index.js";
|
|
26
|
+
<% if (relay) { -%>
|
|
27
|
+
import type { RelaySeedPayload } from "#relay/environment.js";
|
|
28
|
+
<% } -%>
|
|
21
29
|
|
|
22
30
|
const protectedPaths = new Set(["/account"]);
|
|
23
31
|
|
|
@@ -27,50 +35,54 @@ function hasDemoAuth(search: unknown): boolean {
|
|
|
27
35
|
return authValue === "1";
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Auth decision for an already-matched route, from the router's own data —
|
|
40
|
+
* the matched pattern and the schema-validated search. Deriving it from the
|
|
41
|
+
* raw URL would normalize differently from the router (trailing slashes,
|
|
42
|
+
* duplicate search values) and let a protected page render unauthenticated.
|
|
43
|
+
*/
|
|
44
|
+
export function getAuthRedirectForMatch(match: {
|
|
45
|
+
readonly route: AnyRoute;
|
|
46
|
+
readonly search: unknown;
|
|
47
|
+
readonly pathname: string;
|
|
48
|
+
}): string | null {
|
|
49
|
+
if (!protectedPaths.has(match.route.url) || hasDemoAuth(match.search)) {
|
|
39
50
|
return null;
|
|
40
51
|
}
|
|
41
52
|
|
|
42
|
-
return `/login?from=${encodeURIComponent(
|
|
53
|
+
return `/login?from=${encodeURIComponent(match.pathname)}`;
|
|
43
54
|
}
|
|
44
55
|
|
|
45
56
|
export function withAuth(loginPath: string): RouteMiddleware {
|
|
46
|
-
return (
|
|
57
|
+
return <TRoute extends AnyRoute>(currentRoute: TRoute): TRoute => {
|
|
47
58
|
if (!protectedPaths.has(currentRoute.url)) {
|
|
48
59
|
return currentRoute;
|
|
49
60
|
}
|
|
50
61
|
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (currentPrefetch) {
|
|
69
|
-
return currentPrefetch(params, search, context);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
62
|
+
const currentWarm = currentRoute.warm;
|
|
63
|
+
const guardedWarm = (
|
|
64
|
+
params: unknown,
|
|
65
|
+
search: unknown,
|
|
66
|
+
context: NavigationContext,
|
|
67
|
+
) => {
|
|
68
|
+
if (!hasDemoAuth(search)) {
|
|
69
|
+
const from = currentRoute.render(
|
|
70
|
+
(params ?? {}) as RouteParamValues | Record<string, never>,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
redirect(`${loginPath}?from=${encodeURIComponent(from)}`, 302);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (currentWarm) {
|
|
77
|
+
return currentWarm(params, search, context);
|
|
78
|
+
}
|
|
72
79
|
};
|
|
73
|
-
|
|
80
|
+
|
|
81
|
+
// Overriding `warm` widens the property's type, so the object needs a
|
|
82
|
+
// local assertion back to TRoute; the middleware's signature itself is
|
|
83
|
+
// now the real generic contract.
|
|
84
|
+
return { ...currentRoute, warm: guardedWarm } as TRoute;
|
|
85
|
+
};
|
|
74
86
|
}
|
|
75
87
|
|
|
76
88
|
const publicLayout = wrapper<ReactElement>({
|
|
@@ -85,14 +97,31 @@ const publicLayout = wrapper<ReactElement>({
|
|
|
85
97
|
),
|
|
86
98
|
});
|
|
87
99
|
|
|
100
|
+
<% if (intl) { -%>
|
|
101
|
+
function NotFoundPage(): ReactElement {
|
|
102
|
+
const { t } = useTranslation();
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<section>
|
|
106
|
+
<h1>{t("notFound.heading")}</h1>
|
|
107
|
+
<p>{t("notFound.body")}</p>
|
|
108
|
+
</section>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
<% } -%>
|
|
88
113
|
const notFoundRoute = route({
|
|
89
114
|
url: "/not-found",
|
|
115
|
+
<% if (intl) { -%>
|
|
116
|
+
content: NotFoundPage,
|
|
117
|
+
<% } else { -%>
|
|
90
118
|
content: () => (
|
|
91
119
|
<section>
|
|
92
120
|
<h1>Page not found</h1>
|
|
93
121
|
<p>The page you are looking for does not exist.</p>
|
|
94
122
|
</section>
|
|
95
123
|
),
|
|
124
|
+
<% } -%>
|
|
96
125
|
});
|
|
97
126
|
|
|
98
127
|
const [guide, home] = group(publicLayout, [
|
|
@@ -113,9 +142,19 @@ const [contact] = group(publicLayout, [contactRoutes.contact] as const);
|
|
|
113
142
|
const [catalog] = group(publicLayout, [catalogRoutes.catalog] as const);
|
|
114
143
|
|
|
115
144
|
<% } -%>
|
|
145
|
+
// Static redirect route: matched entirely from the URL, no content — the
|
|
146
|
+
// router (and the SSR disposition helper) answers with a real 301. Static
|
|
147
|
+
// redirects accept 301 or 308 only; runtime redirect() covers the 302 family.
|
|
148
|
+
const legacyHome = route({
|
|
149
|
+
url: "/home",
|
|
150
|
+
redirect: "/",
|
|
151
|
+
status: 301,
|
|
152
|
+
});
|
|
153
|
+
|
|
116
154
|
const appRoutes = {
|
|
117
155
|
guide,
|
|
118
156
|
home,
|
|
157
|
+
legacyHome,
|
|
119
158
|
account,
|
|
120
159
|
login,
|
|
121
160
|
<% if (forms) { -%>
|
|
@@ -134,6 +173,71 @@ declare module "@canonical/router-react" {
|
|
|
134
173
|
}
|
|
135
174
|
}
|
|
136
175
|
|
|
176
|
+
/**
|
|
177
|
+
* A GraphQL operation captured on the server and replayed on the client:
|
|
178
|
+
* the operation name identifies the query, `variables` rebuilds the operation
|
|
179
|
+
* descriptor, and `data` is the raw response payload. Pure JSON — this is the
|
|
180
|
+
* wire shape that rides `__INITIAL_DATA__.relayPayloads`. (Declared for every
|
|
181
|
+
* flag combination so the server layer's signatures stay uniform; without
|
|
182
|
+
* --relay nothing produces or consumes one.)
|
|
183
|
+
*/
|
|
184
|
+
export interface SerializedRelayPayload {
|
|
185
|
+
readonly id: string;
|
|
186
|
+
readonly variables: Record<string, unknown>;
|
|
187
|
+
readonly data: Record<string, unknown>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
<% if (relay) { -%>
|
|
191
|
+
/**
|
|
192
|
+
* Per-route server queries: what the SSR layer fetches (and serializes) for a
|
|
193
|
+
* matched route before rendering it. Keyed by the route ids that
|
|
194
|
+
* `resolveRouteDisposition` computes; the declaration reuses the exact query
|
|
195
|
+
* the route's client-side `warm` hook fires, so navigation warming and SSR
|
|
196
|
+
* prefetch can never drift apart.
|
|
197
|
+
*/
|
|
198
|
+
export const serverQueries: Partial<
|
|
199
|
+
Record<
|
|
200
|
+
keyof AppRoutes,
|
|
201
|
+
{
|
|
202
|
+
readonly query: typeof productListQuery;
|
|
203
|
+
readonly variables: Record<string, unknown>;
|
|
204
|
+
}
|
|
205
|
+
>
|
|
206
|
+
> = {
|
|
207
|
+
catalog: { query: productListQuery, variables: { count: PAGE_SIZE } },
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Resolve serialized payloads back to replayable operations by matching each
|
|
212
|
+
* entry's operation name against the declared server queries. Unknown or
|
|
213
|
+
* malformed entries are dropped — a stale payload must never crash boot.
|
|
214
|
+
*/
|
|
215
|
+
export function resolveRelayPayloads(
|
|
216
|
+
serialized: readonly SerializedRelayPayload[] | undefined,
|
|
217
|
+
): readonly RelaySeedPayload[] {
|
|
218
|
+
// The payload rides window state, so a stale or foreign value can be any
|
|
219
|
+
// shape — only a real array is iterable here.
|
|
220
|
+
if (!Array.isArray(serialized) || serialized.length === 0) {
|
|
221
|
+
return [];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const byName = new Map(
|
|
225
|
+
Object.values(serverQueries).map((serverQuery) => [
|
|
226
|
+
getRequest(serverQuery.query).params.name,
|
|
227
|
+
serverQuery.query,
|
|
228
|
+
]),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
return serialized.flatMap((entry) => {
|
|
232
|
+
const query = byName.get(entry.id);
|
|
233
|
+
|
|
234
|
+
return query
|
|
235
|
+
? [{ query, variables: entry.variables, data: entry.data }]
|
|
236
|
+
: [];
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
<% } -%>
|
|
137
241
|
export const middleware = [withAuth("/login")] as const;
|
|
138
242
|
|
|
139
243
|
export { appRoutes, notFoundRoute };
|