Skip to content

Commit

Permalink
fix: script inject
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 1, 2024
1 parent a5ade7c commit 50c200b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { TocAutoScroll } from '~/components/modules/toc/TocAutoScroll'
import { sansFont, serifFont } from '~/lib/fonts'
import { AggregationProvider } from '~/providers/root/aggregation-data-provider'
import { AppFeatureProvider } from '~/providers/root/app-feature-provider'
import { ScriptInjectProvider } from '~/providers/root/script-inject-provider'

import { WebAppProviders } from '../../providers/root'
import { Analyze } from './analyze'
Expand Down Expand Up @@ -156,6 +157,7 @@ export default async function RootLayout(props: PropsWithChildren) {
type="image/x-icon"
media="(prefers-color-scheme: light)"
/>
<ScriptInjectProvider />
</head>
<body
className={`${sansFont.variable} ${serifFont.variable} m-0 h-full p-0 font-sans`}
Expand All @@ -165,7 +167,6 @@ export default async function RootLayout(props: PropsWithChildren) {
aggregationData={data}
appConfig={themeConfig.config}
/>

<div data-theme>
<Root>{children}</Root>
</div>
Expand All @@ -174,10 +175,10 @@ export default async function RootLayout(props: PropsWithChildren) {
<SearchPanelWithHotKey />
<Analyze />
<SyncServerTime />
<ToastContainer />
<ScrollTop />
<div className="fixed inset-y-0 right-0 w-[var(--removed-body-scroll-bar-size)] bg-base-100" />
</WebAppProviders>
<ToastContainer />
<ScrollTop />
<div className="fixed inset-y-0 right-0 w-[var(--removed-body-scroll-bar-size)] bg-base-100" />
</body>
</html>
</AppFeatureProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ declare global {

export interface Custom {
css: string[]
styles: any[]
js: string[]
styles: string[]
scripts: ScriptProps[]
}

Expand Down
3 changes: 0 additions & 3 deletions src/providers/root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { DebugProvider } from './debug-provider'
import { EventProvider } from './event-provider'
import { JotaiStoreProvider } from './jotai-provider'
import { PageScrollInfoProvider } from './page-scroll-info-provider'
import { ScriptInjectProvider } from './script-inject-provider'
import { SocketContainer } from './socket-provider'

const loadFeatures = () =>
Expand Down Expand Up @@ -45,8 +44,6 @@ export function WebAppProviders({ children }: PropsWithChildren) {
{/* <SentryProvider key="SentryProvider" /> */}
<PageScrollInfoProvider key="PageScrollInfoProvider" />
<DebugProvider key="debugProvider" />

<ScriptInjectProvider />
</ProviderComposer>
)
}
Expand Down
32 changes: 25 additions & 7 deletions src/providers/root/script-inject-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
'use client'

import Script from 'next/script'

import { useAppConfigSelector } from './aggregation-data-provider'
import { fetchAggregationData } from '~/app/(app)/api'

export const ScriptInjectProvider = async () => {
const { theme } = await fetchAggregationData()
const { scripts, css, js, styles } = theme.config.custom || {}

export const ScriptInjectProvider = () => {
const scripts = useAppConfigSelector((config) => config.custom?.scripts)
if (!scripts) return null
return (
<>
{scripts.map((props) => {
{css && (
<style
id="shiro-custom-css"
dangerouslySetInnerHTML={{
__html: css.join('\n'),
}}
/>
)}
{js && (
<script
id="shiro-custom-js"
dangerouslySetInnerHTML={{
__html: js.join('\n'),
}}
/>
)}
{styles?.map((style) => (
<link key={style} rel="stylesheet" href={style} />
))}
{scripts?.map((props) => {
const nextProps = { ...props } as any
const dataKeys = Object.keys(props).filter((key) =>
/data[A-Z]/.test(key),
Expand Down

0 comments on commit 50c200b

Please sign in to comment.