forked from mdn/fred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.ssr.js
More file actions
132 lines (128 loc) · 5.33 KB
/
entry.ssr.js
File metadata and controls
132 lines (128 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable unicorn/no-useless-switch-case */
import { render as r } from "@lit-labs/ssr";
import { collectResult } from "@lit-labs/ssr/lib/render-result.js";
import { Advertising } from "./components/advertising/server.js";
import { BlogIndex } from "./components/blog-index/server.js";
import { BlogPost } from "./components/blog-post/server.js";
import { ContributorSpotlight } from "./components/contributor-spotlight/server.js";
import { CurriculumAbout } from "./components/curriculum-about/server.js";
import { CurriculumDefault } from "./components/curriculum-default/server.js";
import { CurriculumLanding } from "./components/curriculum-landing/server.js";
import { CurriculumModule } from "./components/curriculum-module/server.js";
import { CurriculumOverview } from "./components/curriculum-overview/server.js";
import { Doc } from "./components/doc/server.js";
import { GenericAbout } from "./components/generic-about/server.js";
import { GenericCommunity } from "./components/generic-community/server.js";
import { GenericDoc } from "./components/generic-doc/server.js";
import { Homepage } from "./components/homepage/server.js";
import { NotFound } from "./components/not-found/server.js";
import { ObservatoryLanding } from "./components/observatory-landing/server.js";
import { ObservatoryResults } from "./components/observatory-results/server.js";
import { OuterLayout } from "./components/outer-layout/server.js";
import { PageLayout } from "./components/page-layout/server.js";
import { Playground } from "./components/playground/server.js";
import { Plus } from "./components/plus/server.js";
import { Sandbox } from "./components/sandbox/server.js";
import { asyncLocalStorage } from "./components/server/async-local-storage.js";
import { SiteSearch } from "./components/site-search/server.js";
import { addFluent } from "./l10n/context.js";
import { runWithContext } from "./symmetric-context/server.js";
// remove custom elements with `static ssr = false`
// @ts-expect-error
for (const [name, def] of customElements.__definitions) {
if (def?.ctor?.ssr === false) {
// @ts-expect-error
customElements.__definitions.delete(name);
}
}
/**
* @param {string} path
* @param {import("@fred").PartialContext} partialContext
* @param {import("@fred").CompilationStats} compilationStats
*/
export async function render(path, partialContext, compilationStats) {
const locale = path.split("/")[1] || "en-US";
if (locale === "qa") {
path = path.replace("/qa/", "/en-US/");
}
const context = {
path,
...(await addFluent(locale)),
...partialContext,
};
/** @type {import("./components/server/types.js").AsyncLocalStorageContents} */
const storageContents = {
componentsUsed: new Set(),
componentsWithStylesInHead: new Set(),
compilationStats,
};
return asyncLocalStorage.run(storageContents, () =>
runWithContext({ locale }, async () => {
const component = await (async () => {
switch (context.renderer) {
case "BlogIndex":
return BlogIndex.render(context);
case "BlogPost":
return BlogPost.render(context);
case "ContributorSpotlight":
return ContributorSpotlight.render(context);
case "CurriculumAbout":
return CurriculumAbout.render(context);
case "CurriculumDefault":
return CurriculumDefault.render(context);
case "CurriculumLanding":
return CurriculumLanding.render(context);
case "CurriculumModule":
return CurriculumModule.render(context);
case "CurriculumOverview":
return CurriculumOverview.render(context);
case "Doc":
return Doc.render(context);
case "GenericAbout":
return GenericAbout.render(context);
case "GenericCommunity":
return GenericCommunity.render(context);
case "GenericDoc":
return GenericDoc.render(context);
case "Homepage":
return Homepage.render(context);
case "SpaAdvertise":
return Advertising.render(context);
case "SpaObservatoryAnalyze":
return ObservatoryResults.render(context);
case "SpaObservatoryLanding":
return ObservatoryLanding.render(context);
case "SpaPlay":
return Playground.render(context);
case "SpaPlusAiHelp":
return Plus.render(context);
case "SpaPlusCollections":
return Plus.render(context);
case "SpaPlusCollectionsFrequentlyViewed":
return Plus.render(context);
case "SpaPlusLanding":
return Plus.render(context);
case "SpaPlusSettings":
return Plus.render(context);
case "SpaPlusUpdates":
return Plus.render(context);
case "SpaSearch":
return SiteSearch.render(context);
case "SpaUnknown": {
return PageLayout.render(
context,
`Unknown Spa Page title=${context.pageTitle}, slug=${context.slug}`,
);
}
// @ts-expect-error
case "Sandbox":
return Sandbox.render();
case "SpaNotFound":
default:
return NotFound.render(context);
}
})();
return await collectResult(r(OuterLayout.render(context, component)));
}),
);
}