(html: string, user?: AppUser | null)
| 105 | * Also injects PostHog script if configured. |
| 106 | */ |
| 107 | export function injectConfigIntoHtml(html: string, user?: AppUser | null): string { |
| 108 | const configScript = getAppConfigScript(user); |
| 109 | |
| 110 | // Add early error buffer (sync) and PostHog script (deferred) if API key is configured |
| 111 | const posthogScripts = POSTHOG_API_KEY |
| 112 | ? `${EARLY_ERROR_BUFFER_SCRIPT}\n<script src="/posthog-init.js" defer></script>` |
| 113 | : ''; |
| 114 | |
| 115 | // csrf.js patches fetch() to include X-CSRF-Token on state-changing requests |
| 116 | const csrfScript = `<script src="/csrf.js"></script>`; |
| 117 | |
| 118 | const injectedScripts = `${configScript}\n${csrfScript}\n${posthogScripts}`; |
| 119 | |
| 120 | if (html.includes("</head>")) { |
| 121 | return html.replace("</head>", `${injectedScripts}\n</head>`); |
| 122 | } |
| 123 | return html.replace("<body", `${injectedScripts}\n<body`); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Resolve path to a public HTML file. |
no test coverage detected