@cslug/vue 0.9.0 → 0.11.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/docs/README.md CHANGED
@@ -11,7 +11,9 @@ browser builds see the same input (see spikes/a-vue-islands). At render time:
11
11
  2. An island renders its component with the **serialized** props (what the browser will get) inside
12
12
  `<cslug-island data-island data-hydrate data-props>`, and records its `lang` namespaces.
13
13
  3. `<Assets>` was a placeholder. It becomes the CSRF meta tag, the entry CSS, the component CSS and, only if
14
- the page has islands, the page data (`#cslug-data`), the island chunk preloads and the client script.
14
+ the page has islands, the page data (`#cslug-data`), the island chunk preloads and the client script. With a
15
+ CSP nonce on the request (`ctx.nonce`, set by `csp()` from `@cslug/security`), its `<script>` and `<style>` tags
16
+ and the `cslug dev` client carry it; `useRequest().nonce` gives it to your own inline tags.
15
17
 
16
18
  **In the browser**, `@cslug/vue` resolves to `src/client` (the `browser` export condition). `mountIslands()`
17
19
  imports each island's chunk when its `hydrate` condition is met and mounts it. `useT` formats ICU messages
package/docs/api.md CHANGED
@@ -9,7 +9,7 @@
9
9
  | `<Assets entry="client" />` | In the layout `<head>` |
10
10
  | `hydrate="load \| idle \| visible \| media:(…) \| none"` | On any component use; `idle` waits at most 2 s; `:ssr="false"` + `<template #fallback>` to skip server rendering |
11
11
  | `serialize(value)` / `Serialized<T>` | What an island receives (bigint → string, Date → ISO); type island props per prop: `defineProps<{ day: Serialized<Day> }>()` |
12
- | `useRequest()` | `locale`, `user`, `csrfToken`, `errors` (first message per field), `old`, `flash`, `url`, `alternates` |
12
+ | `useRequest()` | `locale`, `user`, `csrfToken`, `errors` (first message per field), `old`, `flash`, `url`, `alternates`, `nonce` (the CSP nonce, or `''`) |
13
13
  | `useT()` | `t`, `tr`, `locale`, `formatMoney`, `formatDate` in the current locale |
14
14
  | `route(name, params?, options?)` | |
15
15
  | `formatDuration(minutes)`, `formatMoney`, `formatDate` | |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cslug/vue",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Vue views rendered on the server, components hydrated as islands, composables and asset build.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,9 +33,9 @@
33
33
  "bun": ">=1.3.14"
34
34
  },
35
35
  "peerDependencies": {
36
- "@cslug/core": "0.9.0",
37
- "@cslug/http": "0.9.0",
38
- "@cslug/validation": "0.9.0",
36
+ "@cslug/core": "0.11.0",
37
+ "@cslug/http": "0.11.0",
38
+ "@cslug/validation": "0.11.0",
39
39
  "vue": "^3.5.0",
40
40
  "sass": "^1.80.0",
41
41
  "tailwindcss": "^4.0.0",
@@ -21,6 +21,8 @@ export type RequestInfo = {
21
21
  url: string
22
22
  /** This page in each supported locale (for `<link rel="alternate" hreflang>`), when its route has `:locale`. */
23
23
  alternates: { locale: string; url: string }[]
24
+ /** The CSP nonce for your own inline `<script>` / `<style>` (`csp()` from @cslug/security), or ''. */
25
+ nonce: string
24
26
  }
25
27
 
26
28
  /** The current request, read-only, on the server. Inside a hydrated island it throws: pass props instead. */
@@ -36,6 +38,7 @@ export function useRequest(): RequestInfo {
36
38
  flash: {},
37
39
  url: '',
38
40
  alternates: [],
41
+ nonce: '',
39
42
  }
40
43
  }
41
44
  const hasSession = ctx.session.available
@@ -52,6 +55,7 @@ export function useRequest(): RequestInfo {
52
55
  flash: hasSession ? ctx.session.flashed() : {},
53
56
  url: url.toString(),
54
57
  alternates: alternates(ctx, url),
58
+ nonce: ctx.nonce ?? '',
55
59
  }
56
60
  }
57
61
 
@@ -10,7 +10,7 @@
10
10
  * page if it rendered the component; else nothing (another page's component).
11
11
  * When the server comes back after a restart, the page reloads.
12
12
  */
13
- export type LiveConfig = { url: string; hmr: boolean; modules: string[] }
13
+ export type LiveConfig = { url: string; hmr: boolean; modules: string[]; nonce?: string }
14
14
 
15
15
  type Def = Record<string, unknown>
16
16
  type HmrRuntime = { rerender(id: string, render: unknown): void; reload(id: string, def: Def): void }
@@ -26,6 +26,7 @@ export function liveClient(config: LiveConfig): void {
26
26
  if (!el) {
27
27
  el = document.createElement('style')
28
28
  el.dataset.cslug = id
29
+ if (config.nonce) el.nonce = config.nonce
29
30
  document.head.append(el)
30
31
  }
31
32
  el.textContent = css
@@ -84,4 +85,4 @@ export function liveClient(config: LiveConfig): void {
84
85
 
85
86
  /** The inline `<script type="module">` that runs the client with `config`. */
86
87
  export const liveScript = (config: LiveConfig): string =>
87
- `<script type="module">(${liveClient.toString()})(${JSON.stringify(config).replace(/</g, '\\u003c')})</script>`
88
+ `<script type="module"${config.nonce ? ` nonce="${config.nonce.replace(/[^\w+/=-]/g, '')}"` : ''}>(${liveClient.toString()})(${JSON.stringify(config).replace(/</g, '\\u003c')})</script>`
package/src/render.ts CHANGED
@@ -104,10 +104,11 @@ function assetsHtml(entry: string, state: RenderState, root: string): string {
104
104
  const token = state.http && !isMissing(state.http) ? state.http.session.token() : undefined
105
105
  if (token) out.push(`<meta name="csrf-token" content="${escapeAttr(token)}">`)
106
106
  const assets = manifest?.entries[entry]
107
+ const nonce = nonceAttr(state)
107
108
  for (const css of assets?.css ?? []) out.push(`<link rel="stylesheet" href="${escapeAttr(css)}">`)
108
109
  out.push(componentCss(state))
109
110
  if (state.islands.size) {
110
- out.push('<style>cslug-island{display:contents}</style>')
111
+ out.push(`<style${nonce}>cslug-island{display:contents}</style>`)
111
112
  if (assets?.js) {
112
113
  const data = pageData(state, manifest as AssetManifest)
113
114
  out.push(
@@ -117,7 +118,7 @@ function assetsHtml(entry: string, state: RenderState, root: string): string {
117
118
  const chunk = manifest?.islands[island]
118
119
  if (chunk) out.push(`<link rel="modulepreload" href="${escapeAttr(chunk)}">`)
119
120
  }
120
- out.push(`<script type="module" src="${escapeAttr(assets.js)}"></script>`)
121
+ out.push(`<script type="module" src="${escapeAttr(assets.js)}"${nonce}></script>`)
121
122
  } else if (state.dev) {
122
123
  out.push(
123
124
  `<!-- cslug: this page has islands but public/build/manifest.json has no "${entry}" entry: run cslug dev or cslug build -->`,
@@ -135,14 +136,18 @@ function assetsHtml(entry: string, state: RenderState, root: string): string {
135
136
  function componentCss(state: RenderState): string {
136
137
  const ids = [...state.modules].filter((m) => componentStyles.get(m))
137
138
  if (!ids.length) return ''
138
- if (!liveConfig()) return `<style data-cslug>${ids.map((m) => componentStyles.get(m)).join('\n')}</style>`
139
- return ids.map((m) => `<style data-cslug="${escapeAttr(m)}">${componentStyles.get(m)}</style>`).join('')
139
+ const nonce = nonceAttr(state)
140
+ if (!liveConfig()) return `<style data-cslug${nonce}>${ids.map((m) => componentStyles.get(m)).join('\n')}</style>`
141
+ return ids.map((m) => `<style data-cslug="${escapeAttr(m)}"${nonce}>${componentStyles.get(m)}</style>`).join('')
140
142
  }
141
143
 
144
+ /** ` nonce="…"` when a CSP middleware gave this request a nonce (`csp()` from @cslug/security). */
145
+ const nonceAttr = (state: RenderState) => (state.http?.nonce ? ` nonce="${escapeAttr(state.http.nonce)}"` : '')
146
+
142
147
  /** Under `cslug dev`: the live-reload and HMR client (live-client.ts), told which components the page rendered. */
143
148
  function devClient(state: RenderState): string {
144
149
  const live = liveConfig()
145
- return live ? liveScript({ ...live, modules: [...state.modules] }) : ''
150
+ return live ? liveScript({ ...live, modules: [...state.modules], nonce: state.http?.nonce }) : ''
146
151
  }
147
152
 
148
153
  /** What islands need in the browser: locale, their translation namespaces, named routes, chunk URLs. */