mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-11-29 09:16:42 +00:00
feat: add dynamic headers via NEXT_PUBLIC_HEAD_SCRIPTS env at buildtime
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Metadata } from 'next'
|
||||
import { ScriptHeaders } from '@/lib/script-headers'
|
||||
import './globals.css'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
@@ -16,6 +17,7 @@ export default function RootLayout({
|
||||
<head>
|
||||
<link rel="icon" href="/mantisbase.svg" type="image/svg+xml" />
|
||||
<link rel="alternate icon" href="/favicon.ico" />
|
||||
<ScriptHeaders />
|
||||
</head>
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
|
||||
42
lib/script-headers.tsx
Normal file
42
lib/script-headers.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
const HEAD_SCRIPTS =
|
||||
process.env.HEAD_SCRIPTS?.trim() ??
|
||||
process.env.NEXT_PUBLIC_HEAD_SCRIPTS?.trim() ??
|
||||
''
|
||||
|
||||
function parseScriptTags(html: string) {
|
||||
return html.match(/<script[\s\S]*?<\/script>/gi) ?? []
|
||||
}
|
||||
|
||||
function renderScriptTag(tag: string, index: number) {
|
||||
const src = tag.match(/\bsrc="([^"]+)"/)?.[1]
|
||||
const defer = /\bdefer\b/.test(tag)
|
||||
const dataDomain = tag.match(/\bdata-domain="([^"]+)"/)?.[1]
|
||||
const inline = tag.match(/<script[^>]*>([\s\S]*?)<\/script>/i)?.[1]?.trim()
|
||||
|
||||
if (src) {
|
||||
return (
|
||||
<script
|
||||
key={index}
|
||||
defer={defer || undefined}
|
||||
data-domain={dataDomain}
|
||||
src={src}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
if (inline) {
|
||||
return (
|
||||
<script key={index} dangerouslySetInnerHTML={{ __html: inline }} />
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function ScriptHeaders() {
|
||||
if (!HEAD_SCRIPTS) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <>{parseScriptTags(HEAD_SCRIPTS).map(renderScriptTag)}</>
|
||||
}
|
||||
Reference in New Issue
Block a user