Skip to content

Commit

Permalink
chore: refactor to decorate index html
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaellis authored and juice49 committed Feb 6, 2025
1 parent d55479a commit 2d8c1ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
12 changes: 12 additions & 0 deletions packages/sanity/src/_internal/cli/server/renderDocument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ export function decorateIndexWithAutoGeneratedWarning(template: string): string
return template.replace(/<head/, `\n<!--\n${autoGeneratedWarning}\n-->\n<head`)
}

/**
* Decorates the given HTML template with a script
* tag that loads the bridge component to communicate
* with core-ui.
*/
export function decorateIndexWithBridgeScript(template: string): string {
return template.replace(
'</head>',
'<script src="https://core.sanity-cdn.com/bridge.js" async type="module"></script>\n</head>',
)
}

export function getPossibleDocumentComponentLocations(studioRootPath: string): string[] {
return [path.join(studioRootPath, '_document.js'), path.join(studioRootPath, '_document.tsx')]
}
Expand Down
23 changes: 13 additions & 10 deletions packages/sanity/src/_internal/cli/server/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {debug as serverDebug} from './debug'
import {getEntryModule} from './getEntryModule'
import {
decorateIndexWithAutoGeneratedWarning,
decorateIndexWithBridgeScript,
getPossibleDocumentComponentLocations,
renderDocument,
} from './renderDocument'
Expand Down Expand Up @@ -48,16 +49,18 @@ export async function writeSanityRuntime({

async function renderAndWriteDocument() {
debug('Rendering document template')
const indexHtml = decorateIndexWithAutoGeneratedWarning(
await renderDocument({
studioRootPath: cwd,
monorepo,
props: {
entryPath: `/${path.relative(cwd, path.join(runtimeDir, 'app.js'))}`,
basePath: basePath || '/',
},
isCoreApp,
}),
const indexHtml = decorateIndexWithBridgeScript(
decorateIndexWithAutoGeneratedWarning(
await renderDocument({
studioRootPath: cwd,
monorepo,
props: {
entryPath: `/${path.relative(cwd, path.join(runtimeDir, 'app.js'))}`,
basePath: basePath || '/',
},
isCoreApp,
}),
),
)

debug('Writing index.html to runtime directory')
Expand Down
17 changes: 0 additions & 17 deletions packages/sanity/src/core/studio/Bridge.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions packages/sanity/src/core/studio/Studio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {GlobalStyle} from './GlobalStyle'
import {type RouterHistory} from './router'
import {StudioLayout} from './StudioLayout'
import {StudioProvider} from './StudioProvider'
import {Bridge} from './Bridge'

/**
* @hidden
Expand Down Expand Up @@ -113,7 +112,6 @@ export function Studio(props: StudioProps): React.JSX.Element {
unstable_history={unstable_history}
unstable_noAuthBoundary={unstable_noAuthBoundary}
>
<Bridge />
{globalStyles && <GlobalStyle />}
<StudioLayout />
</StudioProvider>
Expand Down

0 comments on commit 2d8c1ba

Please sign in to comment.