@govtechsg/sgds-web-component 3.30.1-rc.2 → 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/utils/ssr-nonce-patch.cjs.js +0 -31
- package/react/utils/ssr-nonce-patch.js +37 -30
- package/utils/ssr-nonce-patch.d.ts +6 -6
- package/utils/ssr-nonce.d.ts +3 -22
- package/react/utils/ssr-nonce-patch.cjs.js.map +0 -1
- package/react/utils/ssr-nonce-patch.js.map +0 -1
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"}
|
|
@@ -1,32 +1 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
//#region src/utils/ssr-nonce-patch.ts
|
|
3
|
-
/**
|
|
4
|
-
* Patches @lit-labs/ssr's LitElementRenderer.renderShadow() to respect
|
|
5
|
-
* globalThis.litNonce for CSP nonce injection.
|
|
6
|
-
*
|
|
7
|
-
* Uses new Function("return require")() to get a synchronous require
|
|
8
|
-
* that is invisible to both Rolldown and webpack. Bundlers cannot
|
|
9
|
-
* statically analyze new Function(), so they leave it alone.
|
|
10
|
-
* At runtime in Node.js, it returns the real require function.
|
|
11
|
-
*/
|
|
12
|
-
if (typeof window === "undefined") try {
|
|
13
|
-
const mod = new Function("return require")()("@lit-labs/ssr/lib/lit-element-renderer.js");
|
|
14
|
-
const LitElementRenderer = mod.LitElementRenderer || mod.default?.LitElementRenderer;
|
|
15
|
-
if (LitElementRenderer && !LitElementRenderer._sgdsNoncePatched) {
|
|
16
|
-
LitElementRenderer._sgdsNoncePatched = true;
|
|
17
|
-
const originalRenderShadow = LitElementRenderer.prototype.renderShadow;
|
|
18
|
-
LitElementRenderer.prototype.renderShadow = function(renderInfo) {
|
|
19
|
-
const result = originalRenderShadow.call(this, renderInfo);
|
|
20
|
-
if (!result) return result;
|
|
21
|
-
const nonce = globalThis.litNonce;
|
|
22
|
-
if (!nonce) return result;
|
|
23
|
-
return result.map((item) => {
|
|
24
|
-
if (typeof item === "string" && item === "<style>") return `<style nonce="${nonce}">`;
|
|
25
|
-
return item;
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
} catch {}
|
|
30
|
-
//#endregion
|
|
31
|
-
|
|
32
|
-
//# sourceMappingURL=ssr-nonce-patch.cjs.js.map
|
|
@@ -1,32 +1,39 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
//#region src/utils/ssr-nonce-patch.ts
|
|
3
1
|
/**
|
|
4
|
-
* Patches @lit-labs/ssr's LitElementRenderer.renderShadow() to respect
|
|
5
|
-
* globalThis.litNonce for CSP nonce injection.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*/
|
|
12
|
-
if (typeof window === "undefined")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const originalRenderShadow = LitElementRenderer.prototype.renderShadow;
|
|
18
|
-
LitElementRenderer.prototype.renderShadow = function(renderInfo) {
|
|
19
|
-
const result = originalRenderShadow.call(this, renderInfo);
|
|
20
|
-
if (!result) return result;
|
|
21
|
-
const nonce = globalThis.litNonce;
|
|
22
|
-
if (!nonce) return result;
|
|
23
|
-
return result.map((item) => {
|
|
24
|
-
if (typeof item === "string" && item === "<style>") return `<style nonce="${nonce}">`;
|
|
25
|
-
return item;
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
} catch {}
|
|
30
|
-
//#endregion
|
|
2
|
+
* Patches @lit-labs/ssr's LitElementRenderer.renderShadow() to respect
|
|
3
|
+
* globalThis.litNonce for CSP nonce injection.
|
|
4
|
+
*
|
|
5
|
+
* This file is NOT processed by Rolldown — it's copied as-is to lib/.
|
|
6
|
+
* The bare require() is preserved so webpack resolves it within its own
|
|
7
|
+
* module system, ensuring we patch the SAME LitElementRenderer instance
|
|
8
|
+
* that @lit-labs/ssr-react uses during rendering.
|
|
9
|
+
*/
|
|
10
|
+
if (typeof window === "undefined") {
|
|
11
|
+
try {
|
|
12
|
+
var mod = require("@lit-labs/ssr/lib/lit-element-renderer.js");
|
|
13
|
+
var LitElementRenderer =
|
|
14
|
+
mod.LitElementRenderer || (mod.default && mod.default.LitElementRenderer);
|
|
31
15
|
|
|
32
|
-
|
|
16
|
+
if (LitElementRenderer && !LitElementRenderer._sgdsNoncePatched) {
|
|
17
|
+
LitElementRenderer._sgdsNoncePatched = true;
|
|
18
|
+
|
|
19
|
+
var originalRenderShadow = LitElementRenderer.prototype.renderShadow;
|
|
20
|
+
|
|
21
|
+
LitElementRenderer.prototype.renderShadow = function (renderInfo) {
|
|
22
|
+
var result = originalRenderShadow.call(this, renderInfo);
|
|
23
|
+
if (!result) return result;
|
|
24
|
+
|
|
25
|
+
var nonce = globalThis.litNonce;
|
|
26
|
+
if (!nonce) return result;
|
|
27
|
+
|
|
28
|
+
return result.map(function (item) {
|
|
29
|
+
if (typeof item === "string" && item === "<style>") {
|
|
30
|
+
return '<style nonce="' + nonce + '">';
|
|
31
|
+
}
|
|
32
|
+
return item;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
// @lit-labs/ssr not available — nonce injection disabled
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Placeholder — the actual ssr-nonce-patch is in ssr-nonce-patch.raw.js
|
|
3
|
+
* which is copied to lib/ by frankBuild.js, bypassing Rolldown.
|
|
4
|
+
* This preserves bare require() for webpack's module system.
|
|
4
5
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* statically analyze new Function(), so they leave it alone.
|
|
8
|
-
* At runtime in Node.js, it returns the real require function.
|
|
6
|
+
* The export below prevents Rolldown from tree-shaking this module
|
|
7
|
+
* when imported from _ssr-init.ts.
|
|
9
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';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-nonce-patch.cjs.js","names":[],"sources":["../../../src/utils/ssr-nonce-patch.ts"],"sourcesContent":["/**\n * Patches @lit-labs/ssr's LitElementRenderer.renderShadow() to respect\n * globalThis.litNonce for CSP nonce injection.\n *\n * Uses new Function(\"return require\")() to get a synchronous require\n * that is invisible to both Rolldown and webpack. Bundlers cannot\n * statically analyze new Function(), so they leave it alone.\n * At runtime in Node.js, it returns the real require function.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\nif (typeof window === \"undefined\") {\n try {\n // new Function hides require from all bundlers — synchronous at runtime\n const _require = new Function(\"return require\")() as (\n id: string\n ) => any;\n const mod = _require(\"@lit-labs/ssr/lib/lit-element-renderer.js\");\n const LitElementRenderer =\n mod.LitElementRenderer || mod.default?.LitElementRenderer;\n\n if (LitElementRenderer && !LitElementRenderer._sgdsNoncePatched) {\n LitElementRenderer._sgdsNoncePatched = true;\n\n const originalRenderShadow =\n LitElementRenderer.prototype.renderShadow;\n\n LitElementRenderer.prototype.renderShadow = function (\n renderInfo: unknown\n ) {\n const result = originalRenderShadow.call(this, renderInfo);\n if (!result) return result;\n\n const nonce = (globalThis as any).litNonce;\n if (!nonce) return result;\n\n return result.map((item: unknown) => {\n if (typeof item === \"string\" && item === \"<style>\") {\n return `<style nonce=\"${nonce}\">`;\n }\n return item;\n });\n };\n }\n } catch {\n // @lit-labs/ssr not available — nonce injection disabled\n }\n}\n"],"mappings":";;;;;;;;;;;AAYA,IAAI,OAAO,WAAW,aACpB,IAAI;CAKF,MAAM,MAHW,IAAI,SAAS,gBAAgB,CAAC,CAG5B,CAAC,CAAC,2CAA2C;CAChE,MAAM,qBACJ,IAAI,sBAAsB,IAAI,SAAS;CAEzC,IAAI,sBAAsB,CAAC,mBAAmB,mBAAmB;EAC/D,mBAAmB,oBAAoB;EAEvC,MAAM,uBACJ,mBAAmB,UAAU;EAE/B,mBAAmB,UAAU,eAAe,SAC1C,YACA;GACA,MAAM,SAAS,qBAAqB,KAAK,MAAM,UAAU;GACzD,IAAI,CAAC,QAAQ,OAAO;GAEpB,MAAM,QAAS,WAAmB;GAClC,IAAI,CAAC,OAAO,OAAO;GAEnB,OAAO,OAAO,KAAK,SAAkB;IACnC,IAAI,OAAO,SAAS,YAAY,SAAS,WACvC,OAAO,iBAAiB,MAAM;IAEhC,OAAO;GACT,CAAC;EACH;CACF;AACF,QAAQ,CAER"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-nonce-patch.js","names":[],"sources":["../../../src/utils/ssr-nonce-patch.ts"],"sourcesContent":["/**\n * Patches @lit-labs/ssr's LitElementRenderer.renderShadow() to respect\n * globalThis.litNonce for CSP nonce injection.\n *\n * Uses new Function(\"return require\")() to get a synchronous require\n * that is invisible to both Rolldown and webpack. Bundlers cannot\n * statically analyze new Function(), so they leave it alone.\n * At runtime in Node.js, it returns the real require function.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\nif (typeof window === \"undefined\") {\n try {\n // new Function hides require from all bundlers — synchronous at runtime\n const _require = new Function(\"return require\")() as (\n id: string\n ) => any;\n const mod = _require(\"@lit-labs/ssr/lib/lit-element-renderer.js\");\n const LitElementRenderer =\n mod.LitElementRenderer || mod.default?.LitElementRenderer;\n\n if (LitElementRenderer && !LitElementRenderer._sgdsNoncePatched) {\n LitElementRenderer._sgdsNoncePatched = true;\n\n const originalRenderShadow =\n LitElementRenderer.prototype.renderShadow;\n\n LitElementRenderer.prototype.renderShadow = function (\n renderInfo: unknown\n ) {\n const result = originalRenderShadow.call(this, renderInfo);\n if (!result) return result;\n\n const nonce = (globalThis as any).litNonce;\n if (!nonce) return result;\n\n return result.map((item: unknown) => {\n if (typeof item === \"string\" && item === \"<style>\") {\n return `<style nonce=\"${nonce}\">`;\n }\n return item;\n });\n };\n }\n } catch {\n // @lit-labs/ssr not available — nonce injection disabled\n }\n}\n"],"mappings":";;;;;;;;;;;AAYA,IAAI,OAAO,WAAW,aACpB,IAAI;CAKF,MAAM,MAHW,IAAI,SAAS,gBAAgB,CAAC,CAG5B,CAAC,CAAC,2CAA2C;CAChE,MAAM,qBACJ,IAAI,sBAAsB,IAAI,SAAS;CAEzC,IAAI,sBAAsB,CAAC,mBAAmB,mBAAmB;EAC/D,mBAAmB,oBAAoB;EAEvC,MAAM,uBACJ,mBAAmB,UAAU;EAE/B,mBAAmB,UAAU,eAAe,SAC1C,YACA;GACA,MAAM,SAAS,qBAAqB,KAAK,MAAM,UAAU;GACzD,IAAI,CAAC,QAAQ,OAAO;GAEpB,MAAM,QAAS,WAAmB;GAClC,IAAI,CAAC,OAAO,OAAO;GAEnB,OAAO,OAAO,KAAK,SAAkB;IACnC,IAAI,OAAO,SAAS,YAAY,SAAS,WACvC,OAAO,iBAAiB,MAAM;IAEhC,OAAO;GACT,CAAC;EACH;CACF;AACF,QAAQ,CAER"}
|