@inglorious/ssx 2.1.20 → 2.3.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 CHANGED
@@ -3,9 +3,9 @@
3
3
  [![NPM version](https://img.shields.io/npm/v/@inglorious/ssx.svg)](https://www.npmjs.com/package/@inglorious/ssx)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
5
 
6
- **Static Site Xecution** - Build blazing-fast static sites with [@inglorious/web](https://www.npmjs.com/package/@inglorious/web), complete with static-site generation, optional server-side rendering, client-side hydration, and zero-config routing.
6
+ **Static Site Xecution** - Pre-rendered sites with [@inglorious/web](https://www.npmjs.com/package/@inglorious/web): static-site generation, server-side rendering on demand, client-side hydration, API routes and zero-config routing.
7
7
 
8
- SSX takes your entity-based web apps and generates optimized static HTML with full hydration support. By default, pages are generated during the build and written to `dist/`, but you can opt specific pages into request-time rendering when you need fresh data or per-request behavior. Think Next.js SSG or Astro, but with the simplicity and predictability of Inglorious Web's entity architecture.
8
+ SSX renders your entity-based web apps to HTML on the server, then hydrates them in the browser. Pages are generated at build time as static files, or rendered on each request when they need fresh data or per-request behavior: one page can be static and the next server-rendered. Think Next.js or Astro, but with the simplicity and predictability of Inglorious Web's entity architecture.
9
9
 
10
10
  ---
11
11
 
@@ -13,14 +13,14 @@ SSX takes your entity-based web apps and generates optimized static HTML with fu
13
13
 
14
14
  ### ⚡️ Fast by Default
15
15
 
16
- - **Pre-rendered HTML** - Every page is built at compile time
17
- - **Instant load times** - No waiting for server responses
18
- - **CDN-ready** - Deploy anywhere static files are served
16
+ - **Pre-rendered HTML** - Every page arrives as HTML, built ahead or rendered on request
17
+ - **Instant load times** - Static pages need no server work at all
18
+ - **CDN-ready** - A static site deploys anywhere static files are served
19
19
  - **Perfect Lighthouse scores** - SEO and performance out of the box
20
20
 
21
21
  ### 🎯 Simple Architecture
22
22
 
23
- - **No server required** - Pure static files
23
+ - **Static or server-rendered** - Pure static files, or a server for the pages that need one
24
24
  - **No complex build configs** - Convention over configuration
25
25
  - **File-based routing** - Pages are just files in `src/pages/`
26
26
  - **Entity-based state** - Same familiar patterns from @inglorious/web
@@ -154,11 +154,12 @@ Deploy `dist/` to:
154
154
 
155
155
  ## Features
156
156
 
157
- ### ⚙️ Rendering Modes: SSG by Default, SSR by Opt-In
157
+ ### ⚙️ Rendering Modes: SSG and SSR
158
158
 
159
- SSX is primarily a static-site generator. During `ssx build`, most pages are rendered once and emitted as static HTML files in `dist/`.
159
+ Every page is rendered to HTML on the server and hydrated in the browser. What changes is _when_ it is rendered:
160
160
 
161
- If a page needs request-time rendering, export `ssr` from the page module:
161
+ - **Static (the default).** `ssx build` renders the page once and writes it to `dist/`. Its `load` runs at build time.
162
+ - **Server-rendered.** A page that exports `ssr` is rendered on each request, with its `load` run for that request, so it can show fresh data or vary per request:
162
163
 
163
164
  ```javascript
164
165
  // src/pages/checkout.js
@@ -178,7 +179,29 @@ export const Checkout = {
178
179
  }
179
180
  ```
180
181
 
181
- SSR pages are skipped during the static build, then rendered on demand by the dev server or by a dedicated runtime server started with `ssx ssr`. That keeps the build output focused on pages that should be statically generated.
182
+ Each request gets a store of its own, so one visitor's data never reaches another's page. `load` gets the page with the `request`, a Web Fetch API `Request` like the one API routes get, to read headers and cookies from:
183
+
184
+ ```javascript
185
+ export const ssr = true
186
+
187
+ export async function load(entity, page) {
188
+ const session = page.request.headers.get("cookie")
189
+ entity.orders = await getOrders(session)
190
+ }
191
+ ```
192
+
193
+ A dynamic route that exports `ssr` needs no `staticPaths`: any path its pattern matches is a page, and `page.params` come from the URL. `src/pages/orders/_id.js` answers `/orders/42`, with `page.params.id === "42"`, and every other order alike.
194
+
195
+ #### Deploying
196
+
197
+ | The site has… | Build | Run in production | Serves |
198
+ | ----------------------- | ----------- | ---------------------------------------------- | ----------------------------------------------------------- |
199
+ | Only static pages | `ssx build` | Any static host, or `ssx serve` for API routes | `dist/`, and `src/api/` with `ssx serve` |
200
+ | Pages that export `ssr` | — | `ssx ssr` | Every page, rendered on request, the API routes, and assets |
201
+
202
+ `ssx build` skips the pages that export `ssr`, and `ssx serve` only serves what the build wrote, so a site with server-rendered pages runs on `ssx ssr`. It renders every page on request, static ones included, from your sources through Vite, with no build step. In development, `ssx dev` renders every page on request as well.
203
+
204
+ A static dynamic route lists its paths with [`staticPaths`](#-dynamic-routes-with-staticpaths), as the build has to know them; a server-rendered one does not.
182
205
 
183
206
  ### 🗺️ Sitemap & RSS Generation
184
207
 
@@ -289,7 +312,7 @@ Notes:
289
312
 
290
313
  - The default locale is not prefixed (`/about`, not `/en/about`).
291
314
  - Locale-prefixed routes are handled in both build output and client-side navigation.
292
- - SSX picks the locale; to translate strings (catalogues, plurals, dates, markup in translations), use [`@inglorious/i18n`](https://www.npmjs.com/package/@inglorious/i18n) and switch it to the page's locale with `api.notify("#i18n:localeChange", entity.locale)`.
315
+ - SSX picks the locale; to translate strings (catalogues, plurals, dates, markup in translations), use [`@inglorious/i18n`](https://www.npmjs.com/package/@inglorious/i18n) with its entity named `i18n`. SSX switches it to the locale of each page and loads that catalogue before `load` runs and the page renders, on the server and before the client hydrates, so translations always match the URL rather than the browser's language. Translate inside `render`, `load` or a `metadata` function rather than when a module is imported, so each page gets its own language.
293
316
 
294
317
  ### ⚛️ Entity-Based State and Behavior
295
318
 
@@ -325,7 +348,7 @@ export const entities = {
325
348
 
326
349
  ### 🔄 Data Loading
327
350
 
328
- Load data at build time with the `load` export:
351
+ Load a page's data with the `load` export: at build time for a static page, on each request for one that exports `ssr`:
329
352
 
330
353
  ```javascript
331
354
  // src/pages/blog.js
@@ -348,7 +371,7 @@ export const Blog = {
348
371
  },
349
372
  }
350
373
 
351
- // SSR: Load data during build
374
+ // Runs on the server, before the page renders
352
375
  export async function load(entity) {
353
376
  const response = await fetch("https://api.example.com/posts")
354
377
  entity.posts = await response.json()
@@ -359,7 +382,7 @@ export const metadata = {
359
382
  }
360
383
  ```
361
384
 
362
- The `load` function runs on the server during build. Data is serialized into the HTML and available immediately on the client.
385
+ The `load` function runs on the server: during the build for a static page, on every request for a server-rendered one. Its data is serialized into the HTML and available immediately on the client.
363
386
 
364
387
  ### 🎨 Dynamic Routes with `staticPaths`
365
388
 
@@ -520,6 +543,35 @@ title: My Post
520
543
  This is a markdown page.
521
544
  ```
522
545
 
546
+ ### 🧩 JSX and Vue Pages
547
+
548
+ Pages can be written in JSX or Vue-like templates instead of lit-html. Add the plugin to the `vite` config in `site.config.js`, and SSX uses it both to render pages at build time and to hydrate them in the browser:
549
+
550
+ ```javascript
551
+ import { jsx } from "@inglorious/vite-plugin-jsx"
552
+
553
+ export default {
554
+ vite: {
555
+ plugins: [jsx()],
556
+ },
557
+ }
558
+ ```
559
+
560
+ With [`@inglorious/vite-plugin-jsx`](https://www.npmjs.com/package/@inglorious/vite-plugin-jsx), pages are `.jsx` or `.tsx` files, exporting their type and `metadata` as usual.
561
+
562
+ With [`@inglorious/vite-plugin-vue`](https://www.npmjs.com/package/@inglorious/vite-plugin-vue), a `.vue` file only exports its type, so SSX does not route it. Each page is a `.js` or `.ts` module that re-exports the type from its `.vue` file, next to its `metadata`:
563
+
564
+ ```javascript
565
+ // src/pages/about.js
566
+ export { About } from "./about.vue"
567
+
568
+ export const metadata = {
569
+ title: "About",
570
+ }
571
+ ```
572
+
573
+ `create-app` sets either up with `--template=ssx-js --syntax=jsx` or `--syntax=vue`.
574
+
523
575
  ### ⚡ Serverless Functions (API Routes)
524
576
 
525
577
  SSX supports serverless functions for dynamic API endpoints. Create files in `src/api/` that export HTTP method handlers:
@@ -671,7 +723,7 @@ Options:
671
723
 
672
724
  ### `ssx serve`
673
725
 
674
- Serves the production build with static files and API routes:
726
+ Serves the production build with static files and API routes. Pages that export `ssr` are not in the build: serve a site with them with `ssx ssr`.
675
727
 
676
728
  ```bash
677
729
  pnpm ssx serve [options]
@@ -685,7 +737,7 @@ Options:
685
737
 
686
738
  ### `ssx ssr`
687
739
 
688
- Starts a runtime server that renders SSR-marked pages on demand:
740
+ Starts a server that renders every page on request, those that export `ssr` and static ones alike, and serves the API routes and assets. It renders from your sources through Vite, so it needs no `ssx build`:
689
741
 
690
742
  ```bash
691
743
  pnpm ssx ssr [options]
@@ -723,19 +775,20 @@ my-site/
723
775
 
724
776
  ## Comparison to Other Tools
725
777
 
726
- | Feature | SSX | Next.js (SSG) | Astro | Eleventy |
727
- | ------------------ | ----------- | ------------- | ------ | -------- |
728
- | Pre-rendered HTML | ✅ | ✅ | ✅ | ✅ |
729
- | Client hydration | ✅ lit-html | ✅ React | ✅ Any | ❌ |
730
- | Client routing | ✅ | ✅ | ✅ | ❌ |
731
- | Lazy loading | ✅ | ✅ | ✅ | ❌ |
732
- | Entity-based state | ✅ | ❌ | ❌ | ❌ |
733
- | Zero config | ✅ | ❌ | ❌ | ❌ |
734
- | Framework agnostic | ❌ | ❌ | ✅ | ✅ |
778
+ | Feature | SSX | Next.js | Astro | Eleventy |
779
+ | ------------------ | ----------- | -------- | ------ | -------- |
780
+ | Pre-rendered HTML | ✅ | ✅ | ✅ | ✅ |
781
+ | SSR on request | ✅ | ✅ | ✅ | ❌ |
782
+ | Client hydration | ✅ lit-html | ✅ React | ✅ Any | ❌ |
783
+ | Client routing | ✅ | ✅ | ✅ | ❌ |
784
+ | Lazy loading | ✅ | ✅ | ✅ | ❌ |
785
+ | Entity-based state | ✅ | ❌ | ❌ | ❌ |
786
+ | Zero config | ✅ | ❌ | ❌ | ❌ |
787
+ | Framework agnostic | ❌ | ❌ | ✅ | ✅ |
735
788
 
736
789
  SSX is perfect if you:
737
790
 
738
- - Want static site performance
791
+ - Want static site performance, with server rendering where it is needed
739
792
  - Love entity-based architecture
740
793
  - Prefer convention over configuration
741
794
  - Need full client-side interactivity
@@ -909,6 +962,22 @@ await dev({
909
962
  })
910
963
  ```
911
964
 
965
+ ### SSR Server API
966
+
967
+ Renders every page on request, and serves the API routes:
968
+
969
+ ```javascript
970
+ import { ssr } from "@inglorious/ssx/ssr"
971
+
972
+ const server = await ssr({
973
+ rootDir: ".",
974
+ port: 3000,
975
+ configFile: "site.config.js",
976
+ })
977
+
978
+ server.close()
979
+ ```
980
+
912
981
  ### Production Server API
913
982
 
914
983
  ```javascript
@@ -942,6 +1011,7 @@ Check out these example projects:
942
1011
  - [x] Markdown support
943
1012
  - [x] i18n routing and locale-aware client navigation
944
1013
  - [x] API routes (serverless functions)
1014
+ - [x] Server-side rendering on request
945
1015
 
946
1016
  ---
947
1017
 
@@ -954,7 +1024,7 @@ SSX embraces the philosophy of [@inglorious/web](https://www.npmjs.com/package/@
954
1024
  - **Predictability over magic** - Explicit is better than implicit
955
1025
  - **Standards over abstractions** - Use the platform
956
1026
 
957
- Static site generation should be simple. SSX makes it simple.
1027
+ Rendering on the server should be simple, at build time or on request. SSX makes it simple.
958
1028
 
959
1029
  ---
960
1030
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/ssx",
3
- "version": "2.1.20",
3
+ "version": "2.3.0",
4
4
  "description": "Server-Side-X. Xecution? Xperience? Who knows.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -30,10 +30,11 @@
30
30
  "./dev": "./src/dev/index.js",
31
31
  "./build": "./src/build/index.js",
32
32
  "./serve": "./src/serve/index.js",
33
+ "./ssr": "./src/ssr/index.js",
33
34
  "./markdown": "./src/utils/markdown.js",
34
- "./i18n": {
35
- "types": "./types/i18n.d.ts",
36
- "import": "./src/utils/i18n.js"
35
+ "./client": {
36
+ "types": "./types/client.d.ts",
37
+ "import": "./src/client.js"
37
38
  }
38
39
  },
39
40
  "files": [
@@ -48,9 +49,9 @@
48
49
  },
49
50
  "sideEffects": false,
50
51
  "dependencies": {
51
- "@inglorious/store": "12.0.0",
52
+ "@inglorious/store": "12.1.0",
52
53
  "@inglorious/utils": "5.1.0",
53
- "@inglorious/web": "6.9.0",
54
+ "@inglorious/web": "6.10.0",
54
55
  "@lit-labs/ssr": "^4.1.0",
55
56
  "commander": "^15.0.0",
56
57
  "connect": "^3.7.0",
@@ -69,7 +70,7 @@
69
70
  },
70
71
  "devDependencies": {
71
72
  "@inglorious/eslint-config": "1.2.0",
72
- "@inglorious/logo": "3.0.23",
73
+ "@inglorious/logo": "3.0.25",
73
74
  "prettier": "^3.9.6",
74
75
  "vitest": "^5.0.0"
75
76
  },
@@ -2,6 +2,7 @@ import path from "node:path"
2
2
  import { pathToFileURL } from "node:url"
3
3
 
4
4
  import { renderPage } from "../render/index.js"
5
+ import { waitForLocale } from "../utils/i18n.js"
5
6
  import { extractPageMetadata } from "./metadata.js"
6
7
 
7
8
  /**
@@ -42,6 +43,8 @@ export async function generatePages(store, pages, options = {}, loader) {
42
43
  if (page.locale) {
43
44
  entity.locale = page.locale
44
45
  }
46
+ // Translations in the page's locale, for `load` and the render.
47
+ await waitForLocale(store, page.locale)
45
48
 
46
49
  if (module.load) {
47
50
  await module.load(entity, page)
@@ -42,7 +42,11 @@ describe("generatePages", () => {
42
42
 
43
43
  it("should generate HTML and metadata by default", async () => {
44
44
  const store = {
45
- _api: { getEntities: vi.fn(() => []), getEntity: vi.fn(() => ({})) },
45
+ _api: {
46
+ getEntities: vi.fn(() => []),
47
+ getEntity: vi.fn(() => ({})),
48
+ getType: vi.fn(),
49
+ },
46
50
  }
47
51
  const pages = [{ path: "/p1", filePath: pageFile, moduleName: "p1" }]
48
52
 
@@ -60,7 +64,11 @@ describe("generatePages", () => {
60
64
 
61
65
  it("should skip HTML generation when disabled", async () => {
62
66
  const store = {
63
- _api: { getEntities: vi.fn(() => []), getEntity: vi.fn(() => ({})) },
67
+ _api: {
68
+ getEntities: vi.fn(() => []),
69
+ getEntity: vi.fn(() => ({})),
70
+ getType: vi.fn(),
71
+ },
64
72
  }
65
73
  const pages = [{ path: "/p2", filePath: pageFile, moduleName: "p2" }]
66
74
 
@@ -76,7 +84,11 @@ describe("generatePages", () => {
76
84
 
77
85
  it("should skip metadata generation when disabled", async () => {
78
86
  const store = {
79
- _api: { getEntities: vi.fn(() => []), getEntity: vi.fn(() => ({})) },
87
+ _api: {
88
+ getEntities: vi.fn(() => []),
89
+ getEntity: vi.fn(() => ({})),
90
+ getType: vi.fn(),
91
+ },
80
92
  }
81
93
  const pages = [{ path: "/p3", filePath: pageFile, moduleName: "p3" }]
82
94
 
@@ -97,6 +109,7 @@ describe("generatePages", () => {
97
109
  _api: {
98
110
  getEntities: vi.fn(() => [entity]),
99
111
  getEntity: vi.fn(() => entity),
112
+ getType: vi.fn(),
100
113
  },
101
114
  }
102
115
  const pages = [
@@ -124,6 +137,7 @@ describe("generatePages", () => {
124
137
  _api: {
125
138
  getEntities: vi.fn(() => [entity]),
126
139
  getEntity: vi.fn(() => entity),
140
+ getType: vi.fn(),
127
141
  },
128
142
  }
129
143
  const pages = [
@@ -156,6 +170,7 @@ describe("generatePages", () => {
156
170
  _api: {
157
171
  getEntities: vi.fn(() => [{}]),
158
172
  getEntity: vi.fn(() => ({})),
173
+ getType: vi.fn(),
159
174
  },
160
175
  }
161
176
  const pages = [{ path: "/ssr", filePath: pageFile, moduleName: "ssr" }]
package/src/client.js ADDED
@@ -0,0 +1,4 @@
1
+ // The runtime of the client entry SSX generates, which imports it by package name
2
+ // from inside the app. Not meant for apps themselves.
3
+ export { getLocaleFromPath, waitForLocale } from "./utils/i18n.js"
4
+ export { syncPageHead } from "./utils/page-head.js"
package/src/dev/api.js CHANGED
@@ -53,7 +53,13 @@ export const createApiMiddleware = (rootDir, loader, onError) => {
53
53
  }
54
54
  }
55
55
 
56
- function createRequest(req) {
56
+ /**
57
+ * The incoming request as a Web Fetch API `Request`, as API routes and
58
+ * server-rendered pages receive it.
59
+ * @param {import("node:http").IncomingMessage} req
60
+ * @returns {Request}
61
+ */
62
+ export function createRequest(req) {
57
63
  const protocol = req.socket.encrypted ? "https" : "http"
58
64
  const host = req.headers.host || "localhost"
59
65
  const url = `${protocol}://${host}${req.url}`
package/src/dev/index.js CHANGED
@@ -3,7 +3,7 @@ import { createServer } from "vite"
3
3
 
4
4
  import { renderRequest } from "../ssr/index.js"
5
5
  import { loadConfig } from "../utils/config.js"
6
- import { createApiMiddleware } from "./api.js"
6
+ import { createApiMiddleware, createRequest } from "./api.js"
7
7
  import { createViteConfig } from "./vite-config.js"
8
8
 
9
9
  /**
@@ -55,6 +55,7 @@ export async function dev(options = {}) {
55
55
  options: { ...mergedOptions, isDev: true },
56
56
  loader,
57
57
  viteServer,
58
+ request: createRequest(req),
58
59
  })
59
60
 
60
61
  if (!result) {
@@ -1,5 +1,6 @@
1
1
  import { createStore } from "@inglorious/store"
2
2
  import { html } from "@inglorious/web"
3
+ import { when } from "@inglorious/web/directives/when"
3
4
  import { describe, expect, it } from "vitest"
4
5
 
5
6
  import { toHTML } from "./html.js"
@@ -99,9 +100,11 @@ describe("await toHTML", () => {
99
100
  types: {
100
101
  content: {
101
102
  render: (entity) =>
102
- entity.isVisible
103
- ? html`<p>Visible content</p>`
104
- : html`<p>Hidden</p>`,
103
+ when(
104
+ entity.isVisible,
105
+ () => html`<p>Visible content</p>`,
106
+ () => html`<p>Hidden</p>`,
107
+ ),
105
108
  },
106
109
  },
107
110
  entities: {
@@ -1,11 +1,14 @@
1
1
  import { toCamelCase } from "@inglorious/utils/data-structures/string.js"
2
2
 
3
+ import { getTextDirection, selectLocale } from "../utils/i18n.js"
3
4
  import { createGetPageOption } from "../utils/page-options.js"
4
5
  import { toHTML } from "./html.js"
5
6
 
7
+ // Empty, so the site options apply; the layout supplies the final defaults.
6
8
  const DEFAULT_OPTIONS = {
7
- lang: "en",
8
- charset: "UTF-8",
9
+ lang: "",
10
+ dir: "",
11
+ charset: "",
9
12
  title: "",
10
13
  favicon: "",
11
14
  meta: {},
@@ -28,9 +31,15 @@ export async function renderPage(store, page, entity, options = {}) {
28
31
  const { moduleName, module } = page
29
32
  const entityId = entity?.id ?? toCamelCase(moduleName)
30
33
 
34
+ // Before anything translates, and with nothing awaited until the templates are built,
35
+ // so that other renders sharing the translations cannot switch them in between.
36
+ selectLocale(store, entity?.locale)
37
+
31
38
  const getPageOption = createGetPageOption(store, module, entity)
32
39
 
33
- const lang = getPageOption("lang", DEFAULT_OPTIONS) || options.lang
40
+ const lang =
41
+ getPageOption("lang", DEFAULT_OPTIONS) || entity?.locale || options.lang
42
+ const dir = getPageOption("dir", DEFAULT_OPTIONS) || getTextDirection(lang)
34
43
  const charset = getPageOption("charset", DEFAULT_OPTIONS) || options.charset
35
44
  const title = getPageOption("title", DEFAULT_OPTIONS) || options.title
36
45
  const favicon = getPageOption("favicon", DEFAULT_OPTIONS) || options.favicon
@@ -48,6 +57,7 @@ export async function renderPage(store, page, entity, options = {}) {
48
57
  return toHTML(store, (api) => api.render(entityId), {
49
58
  ...options,
50
59
  lang,
60
+ dir,
51
61
  charset,
52
62
  title,
53
63
  favicon,
@@ -4,6 +4,7 @@
4
4
  * @param {string} body - The body content HTML.
5
5
  * @param {Object} options - Layout options.
6
6
  * @param {string} [options.lang="en"] - Language attribute.
7
+ * @param {"ltr" | "rtl"} [options.dir="ltr"] - Text direction attribute.
7
8
  * @param {string} [options.charset="UTF-8"] - Character set.
8
9
  * @param {string} [options.title=""] - Page title.
9
10
  * @param {string} [options.favicon=""] - Favicon URL; without one, no icon link is rendered.
@@ -17,6 +18,7 @@
17
18
  export function layout(body, options) {
18
19
  const {
19
20
  lang = "en",
21
+ dir = "ltr",
20
22
  head = "",
21
23
  prescripts = [],
22
24
  charset = "UTF-8",
@@ -29,7 +31,7 @@ export function layout(body, options) {
29
31
  } = options
30
32
 
31
33
  return `<!DOCTYPE html>
32
- <html lang="${lang}">
34
+ <html lang="${lang}" dir="${dir}">
33
35
  <head>
34
36
  ${prescripts
35
37
  .map((src) => `<script type="text/javascript" src="${src}"></script>`)
@@ -6,7 +6,7 @@ describe("layout", () => {
6
6
  it("should render default layout structure", () => {
7
7
  const html = layout("<h1>Hello</h1>", {})
8
8
  expect(html).toContain("<!DOCTYPE html>")
9
- expect(html).toContain('<html lang="en">')
9
+ expect(html).toContain('<html lang="en" dir="ltr">')
10
10
  expect(html).toContain('<meta charset="UTF-8" />')
11
11
  expect(html).toContain('<div id="root"><h1>Hello</h1></div>')
12
12
  expect(html).toContain('<script type="module" src="/main.js"></script>')
@@ -15,7 +15,7 @@ describe("layout", () => {
15
15
  it("should render with custom title and language", () => {
16
16
  const html = layout("", { title: "My Page", lang: "fr" })
17
17
  expect(html).toContain("<title>My Page</title>")
18
- expect(html).toContain('<html lang="fr">')
18
+ expect(html).toContain('<html lang="fr" dir="ltr">')
19
19
  })
20
20
 
21
21
  it("should render meta tags", () => {
@@ -79,4 +79,9 @@ describe("layout", () => {
79
79
  const html = layout("", { isDev: false })
80
80
  expect(html).not.toContain("/@vite/client")
81
81
  })
82
+
83
+ it("should set the text direction", () => {
84
+ const html = layout("", { lang: "he", dir: "rtl" })
85
+ expect(html).toContain('<html lang="he" dir="rtl">')
86
+ })
82
87
  })
@@ -2,6 +2,7 @@ import path from "node:path"
2
2
  import { pathToFileURL } from "node:url"
3
3
 
4
4
  import { createStore } from "@inglorious/store"
5
+ import { html } from "@inglorious/web"
5
6
  import { describe, expect, it } from "vitest"
6
7
 
7
8
  import { renderPage } from "."
@@ -112,4 +113,112 @@ describe("renderPage", () => {
112
113
 
113
114
  expect(html).toMatchSnapshot()
114
115
  })
116
+
117
+ it("should set the document language from the page locale", async () => {
118
+ const module = await import(pathToFileURL(path.join(PAGES_DIR, "about.js")))
119
+ const page = { path: "/it/about", moduleName: "about", module }
120
+ const entity = { type: "About", name: "Noi", locale: "it" }
121
+
122
+ const store = createStore({
123
+ types: { About: module.About },
124
+ entities: { about: entity },
125
+ updateMode: "manual",
126
+ })
127
+ store.update()
128
+
129
+ const html = await renderPage(store, page, entity, {
130
+ ...DEFAULT_OPTIONS,
131
+ wrap: true,
132
+ lang: "en",
133
+ })
134
+
135
+ expect(html).toContain(`<html lang="it" dir="ltr">`)
136
+ })
137
+
138
+ it("should prefer the language set in the page metadata", async () => {
139
+ const About = { render: () => "" }
140
+ const module = { About, metadata: { lang: "pt" } }
141
+ const page = { path: "/it/about", moduleName: "about", module }
142
+ const entity = { type: "About", locale: "it" }
143
+
144
+ const store = createStore({
145
+ types: { About },
146
+ entities: { about: entity },
147
+ updateMode: "manual",
148
+ })
149
+ store.update()
150
+
151
+ const html = await renderPage(store, page, entity, {
152
+ ...DEFAULT_OPTIONS,
153
+ wrap: true,
154
+ lang: "en",
155
+ })
156
+
157
+ expect(html).toContain(`<html lang="pt" dir="ltr">`)
158
+ })
159
+
160
+ it("should fall back to the site language and charset", async () => {
161
+ const About = { render: () => "" }
162
+ const page = { path: "/about", moduleName: "about", module: { About } }
163
+ const entity = { type: "About" }
164
+
165
+ const store = createStore({
166
+ types: { About },
167
+ entities: { about: entity },
168
+ updateMode: "manual",
169
+ })
170
+ store.update()
171
+
172
+ const html = await renderPage(store, page, entity, {
173
+ ...DEFAULT_OPTIONS,
174
+ wrap: true,
175
+ lang: "it",
176
+ charset: "ISO-8859-1",
177
+ })
178
+
179
+ expect(html).toContain(`<html lang="it" dir="ltr">`)
180
+ expect(html).toContain(`<meta charset="ISO-8859-1" />`)
181
+ })
182
+
183
+ it("should write right-to-left languages right to left", async () => {
184
+ const About = { render: () => "" }
185
+ const page = { path: "/ar/about", moduleName: "about", module: { About } }
186
+ const entity = { type: "About", locale: "ar" }
187
+
188
+ const store = createStore({
189
+ types: { About },
190
+ entities: { about: entity },
191
+ updateMode: "manual",
192
+ })
193
+ store.update()
194
+
195
+ const html = await renderPage(store, page, entity, {
196
+ ...DEFAULT_OPTIONS,
197
+ wrap: true,
198
+ })
199
+
200
+ expect(html).toContain(`<html lang="ar" dir="rtl">`)
201
+ })
202
+
203
+ it("should render in the page's locale", async () => {
204
+ let language = "en"
205
+ const messages = { en: "Hello", it: "Ciao" }
206
+ const I18n = {
207
+ localeChange(entity, locale) {
208
+ entity.locale = language = locale
209
+ },
210
+ }
211
+ const Hello = { render: () => html`<p>${messages[language]}</p>` }
212
+ const page = { path: "/it/hello", moduleName: "hello", module: { Hello } }
213
+ const entity = { type: "Hello", locale: "it" }
214
+
215
+ const store = createStore({
216
+ types: { I18n, Hello },
217
+ entities: { i18n: { type: "I18n", locale: "en" }, hello: entity },
218
+ })
219
+
220
+ const output = await renderPage(store, page, entity, DEFAULT_OPTIONS)
221
+
222
+ expect(output).toContain("Ciao")
223
+ })
115
224
  })