@govtechsg/sgds-web-component 3.30.1-rc.3 → 3.30.1-rc.4
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/custom-elements.json +1 -1
- package/package.json +1 -1
- package/react/_ssr/index.d.ts +1 -1
- package/react/_ssr/index.js +2 -2
- package/react/_ssr/utils/ssr-nonce.js +4 -41
- package/react/_ssr/utils/ssr-nonce.js.map +1 -1
- package/react/_ssr-init.cjs.js +1 -0
- package/react/_ssr-init.js +1 -0
- package/react/utils/ssr-nonce-patch.cjs.js +1 -0
- package/utils/ssr-nonce-patch.d.ts +4 -0
- package/utils/ssr-nonce.d.ts +3 -22
package/custom-elements.json
CHANGED
|
@@ -32036,7 +32036,7 @@
|
|
|
32036
32036
|
"package": {
|
|
32037
32037
|
"name": "@govtechsg/sgds-web-component",
|
|
32038
32038
|
"description": "",
|
|
32039
|
-
"version": "3.30.1-rc.
|
|
32039
|
+
"version": "3.30.1-rc.4",
|
|
32040
32040
|
"author": "GovTechSG",
|
|
32041
32041
|
"license": "MIT"
|
|
32042
32042
|
}
|
package/package.json
CHANGED
package/react/_ssr/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { setCSPNonce, getCSPNonce
|
|
1
|
+
export { setCSPNonce, getCSPNonce } from '../../utils/ssr-nonce';
|
package/react/_ssr/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { getCSPNonce,
|
|
2
|
-
export { getCSPNonce,
|
|
1
|
+
import { getCSPNonce, setCSPNonce } from "./utils/ssr-nonce.js";
|
|
2
|
+
export { getCSPNonce, setCSPNonce };
|
|
@@ -3,50 +3,13 @@ function getCSPNonce() {
|
|
|
3
3
|
return globalThis["litNonce"] ?? null;
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
6
|
-
* Patch LitElementRenderer.renderShadow() to inject CSP nonces into
|
|
7
|
-
* DSD <style> tags. Call this ONCE at server startup from instrumentation.ts.
|
|
8
|
-
*
|
|
9
|
-
* This must run before any SSR request is handled. Next.js's
|
|
10
|
-
* instrumentation.ts `register()` hook guarantees this.
|
|
11
|
-
*/
|
|
12
|
-
async function registerSSRNonce() {
|
|
13
|
-
if (typeof window !== "undefined") return;
|
|
14
|
-
try {
|
|
15
|
-
const { LitElementRenderer } = await import("@lit-labs/ssr/lib/lit-element-renderer.js");
|
|
16
|
-
if (LitElementRenderer._sgdsNoncePatched) return;
|
|
17
|
-
LitElementRenderer._sgdsNoncePatched = true;
|
|
18
|
-
const originalRenderShadow = LitElementRenderer.prototype.renderShadow;
|
|
19
|
-
LitElementRenderer.prototype.renderShadow = function(renderInfo) {
|
|
20
|
-
const result = originalRenderShadow.call(this, renderInfo);
|
|
21
|
-
if (!result) return result;
|
|
22
|
-
const litNonce = globalThis["litNonce"];
|
|
23
|
-
if (!litNonce) return result;
|
|
24
|
-
return result.map((item) => {
|
|
25
|
-
if (typeof item === "string" && item === "<style>") return `<style nonce="${litNonce}">`;
|
|
26
|
-
return item;
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
} catch {}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
6
|
* CSP nonce for Declarative Shadow DOM (DSD) styles.
|
|
33
7
|
*
|
|
34
|
-
*
|
|
35
|
-
* -
|
|
36
|
-
*
|
|
37
|
-
* - `setCSPNonce()` — call per-request in layout.tsx.
|
|
38
|
-
* Sets globalThis.litNonce for the current request.
|
|
8
|
+
* Sets globalThis.litNonce — the standard Lit mechanism for CSP nonces.
|
|
9
|
+
* The ssr-nonce-patch.raw.js file reads this value at render time and
|
|
10
|
+
* injects it into DSD <style> tags.
|
|
39
11
|
*
|
|
40
12
|
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* // instrumentation.ts
|
|
43
|
-
* import { registerSSRNonce } from '@govtechsg/sgds-web-component/react/ssr';
|
|
44
|
-
*
|
|
45
|
-
* export async function register() {
|
|
46
|
-
* await registerSSRNonce();
|
|
47
|
-
* }
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
13
|
* ```tsx
|
|
51
14
|
* // app/layout.tsx (server component)
|
|
52
15
|
* import { setCSPNonce } from '@govtechsg/sgds-web-component/react/ssr';
|
|
@@ -68,6 +31,6 @@ function setCSPNonce(nonce) {
|
|
|
68
31
|
globalThis["litNonce"] = nonce ?? void 0;
|
|
69
32
|
}
|
|
70
33
|
//#endregion
|
|
71
|
-
export { getCSPNonce,
|
|
34
|
+
export { getCSPNonce, setCSPNonce };
|
|
72
35
|
|
|
73
36
|
//# sourceMappingURL=ssr-nonce.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-nonce.js","names":[],"sources":["../../../../src/utils/ssr-nonce.ts"],"sourcesContent":["export function getCSPNonce(): string | null {\n return ((globalThis as Record<string, unknown>)[\"litNonce\"] as string) ?? null;\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"ssr-nonce.js","names":[],"sources":["../../../../src/utils/ssr-nonce.ts"],"sourcesContent":["export function getCSPNonce(): string | null {\n return ((globalThis as Record<string, unknown>)[\"litNonce\"] as string) ?? null;\n}\n\n/**\n * CSP nonce for Declarative Shadow DOM (DSD) styles.\n *\n * Sets globalThis.litNonce — the standard Lit mechanism for CSP nonces.\n * The ssr-nonce-patch.raw.js file reads this value at render time and\n * injects it into DSD <style> tags.\n *\n * @example\n * ```tsx\n * // app/layout.tsx (server component)\n * import { setCSPNonce } from '@govtechsg/sgds-web-component/react/ssr';\n * import { headers } from 'next/headers';\n *\n * export default async function Layout({ children }) {\n * const nonce = (await headers()).get('x-nonce');\n * if (nonce) setCSPNonce(nonce);\n * return <html><body>{children}</body></html>;\n * }\n * ```\n */\n/**\n * Set the CSP nonce for the current request. Call this in your server\n * component (e.g. layout.tsx) before SGDS components render.\n * This is synchronous — no await needed.\n */\nexport function setCSPNonce(nonce: string | null): void {\n (globalThis as Record<string, unknown>)[\"litNonce\"] = nonce ?? undefined;\n}\n"],"mappings":";AAAA,SAAgB,cAA6B;CAC3C,OAAS,WAAuC,eAA0B;AAC5E;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,YAAY,OAA4B;CACtD,WAAwC,cAAc,SAAS,KAAA;AACjE"}
|
package/react/_ssr-init.cjs.js
CHANGED
package/react/_ssr-init.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";
|
|
@@ -2,4 +2,8 @@
|
|
|
2
2
|
* Placeholder — the actual ssr-nonce-patch is in ssr-nonce-patch.raw.js
|
|
3
3
|
* which is copied to lib/ by frankBuild.js, bypassing Rolldown.
|
|
4
4
|
* This preserves bare require() for webpack's module system.
|
|
5
|
+
*
|
|
6
|
+
* The export below prevents Rolldown from tree-shaking this module
|
|
7
|
+
* when imported from _ssr-init.ts.
|
|
5
8
|
*/
|
|
9
|
+
export declare const _ssrNoncePatchLoaded = true;
|
package/utils/ssr-nonce.d.ts
CHANGED
|
@@ -1,31 +1,12 @@
|
|
|
1
1
|
export declare function getCSPNonce(): string | null;
|
|
2
|
-
/**
|
|
3
|
-
* Patch LitElementRenderer.renderShadow() to inject CSP nonces into
|
|
4
|
-
* DSD <style> tags. Call this ONCE at server startup from instrumentation.ts.
|
|
5
|
-
*
|
|
6
|
-
* This must run before any SSR request is handled. Next.js's
|
|
7
|
-
* instrumentation.ts `register()` hook guarantees this.
|
|
8
|
-
*/
|
|
9
|
-
export declare function registerSSRNonce(): Promise<void>;
|
|
10
2
|
/**
|
|
11
3
|
* CSP nonce for Declarative Shadow DOM (DSD) styles.
|
|
12
4
|
*
|
|
13
|
-
*
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
16
|
-
* - `setCSPNonce()` — call per-request in layout.tsx.
|
|
17
|
-
* Sets globalThis.litNonce for the current request.
|
|
5
|
+
* Sets globalThis.litNonce — the standard Lit mechanism for CSP nonces.
|
|
6
|
+
* The ssr-nonce-patch.raw.js file reads this value at render time and
|
|
7
|
+
* injects it into DSD <style> tags.
|
|
18
8
|
*
|
|
19
9
|
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* // instrumentation.ts
|
|
22
|
-
* import { registerSSRNonce } from '@govtechsg/sgds-web-component/react/ssr';
|
|
23
|
-
*
|
|
24
|
-
* export async function register() {
|
|
25
|
-
* await registerSSRNonce();
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
10
|
* ```tsx
|
|
30
11
|
* // app/layout.tsx (server component)
|
|
31
12
|
* import { setCSPNonce } from '@govtechsg/sgds-web-component/react/ssr';
|