-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove `next-runtime-env` as it prevents static rendering (use context in `layout.tsx` instead) - Get rid of `config.ts` - Simplify block preview and provide site config context in there as well - Use separate `layout.tsx` files for block preview and site Supersedes #350 and #349 To discuss: - Move `SiteConfigsProvider.tsx` and `SiteConfigProvider.tsx` to @comet/cms-site? - Shall we document the following explanation and if yes - where? ---- **Ways to access the current SiteConfig** - _Page_ Use `await getSiteConfigForDomain(params.domain)` - _Component_ (Server Rendering) Not possible, instead pass the necessary data from the page - _Component or Block-Preview_ (Client Rendering) Use the `useSiteConfig()`-hook
- Loading branch information
Showing
11 changed files
with
144 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
PRIVATE_SITE_CONFIGS='{{ site://configs/private/local }}' | ||
PUBLIC_SITE_CONFIGS='{{ site://configs/public/local }}' | ||
NEXT_PUBLIC_SITE_CONFIGS=$PUBLIC_SITE_CONFIGS |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use client"; | ||
import { BlockPreviewProvider, IFrameBridgeProvider, useBlockPreviewFetch, useIFrameBridge } from "@comet/cms-site"; | ||
import { PageContentBlockData } from "@src/blocks.generated"; | ||
import { PageContentBlock } from "@src/documents/pages/blocks/PageContentBlock"; | ||
import { StageBlock } from "@src/documents/pages/blocks/StageBlock"; | ||
import type { ContentScope, PublicSiteConfig } from "@src/site-configs"; | ||
import { graphQLApiUrl } from "@src/util/graphQLClient"; | ||
import { recursivelyLoadBlockData } from "@src/util/recursivelyLoadBlockData"; | ||
import { SiteConfigProvider } from "@src/util/SiteConfigProvider"; | ||
import { createContext, PropsWithChildren, useContext, useEffect, useState } from "react"; | ||
|
||
const PagePreview: React.FunctionComponent = () => { | ||
const iFrameBridge = useIFrameBridge(); | ||
|
||
const { fetch, graphQLFetch } = useBlockPreviewFetch(graphQLApiUrl); | ||
|
||
const [blockData, setBlockData] = useState<PageContentBlockData>(); | ||
useEffect(() => { | ||
async function load() { | ||
if (!iFrameBridge.block) { | ||
setBlockData(undefined); | ||
return; | ||
} | ||
const newData = await recursivelyLoadBlockData({ | ||
blockType: "PageContent", | ||
blockData: iFrameBridge.block, | ||
graphQLFetch, | ||
fetch, | ||
}); | ||
setBlockData(newData); | ||
} | ||
load(); | ||
}, [iFrameBridge.block, fetch, graphQLFetch]); | ||
|
||
return <div>{blockData && <PageContentBlock data={blockData} />}</div>; | ||
}; | ||
|
||
const StagePreview = () => { | ||
const { block: stage } = useIFrameBridge(); | ||
return stage ? <StageBlock data={stage} /> : null; | ||
}; | ||
|
||
const previewComponents = { | ||
page: PagePreview, | ||
stage: StagePreview, | ||
}; | ||
|
||
const PreviewWrapper = ({ type }: { type: string }) => { | ||
const iFrameBridge = useIFrameBridge(); | ||
const siteConfigs = useContext(SiteConfigsContext); | ||
if (!iFrameBridge.contentScope) return; | ||
const contentScope = iFrameBridge.contentScope as ContentScope; | ||
const siteConfig = siteConfigs.find((siteConfig) => siteConfig.scope.domain === contentScope.domain); | ||
|
||
const PreviewComponent = previewComponents[type]; | ||
return ( | ||
<SiteConfigProvider siteConfig={siteConfig}> | ||
<PreviewComponent /> | ||
</SiteConfigProvider> | ||
); | ||
}; | ||
|
||
const SiteConfigsContext = createContext<PublicSiteConfig[]>([]); | ||
|
||
export function SiteConfigsProvider({ children, siteConfigs }: PropsWithChildren<{ siteConfigs: PublicSiteConfig[] }>) { | ||
return <SiteConfigsContext.Provider value={siteConfigs}>{children}</SiteConfigsContext.Provider>; | ||
} | ||
|
||
const PreviewPage = ({ params }: { params: { type: string } }) => { | ||
return ( | ||
<IFrameBridgeProvider> | ||
<BlockPreviewProvider> | ||
<PreviewWrapper type={params.type} /> | ||
</BlockPreviewProvider> | ||
</IFrameBridgeProvider> | ||
); | ||
}; | ||
|
||
export default PreviewPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { GlobalStyle } from "@src/layout/GlobalStyle"; | ||
import { getSiteConfigs } from "@src/middleware"; | ||
import { ResponsiveSpacingStyle } from "@src/util/ResponsiveSpacingStyle"; | ||
import StyledComponentsRegistry from "@src/util/StyledComponentsRegistry"; | ||
|
||
import { SiteConfigsProvider } from "./[type]/page"; | ||
|
||
export default async function BlockPreviewLayout({ children }: { children: React.ReactNode }) { | ||
const siteConfigs = getSiteConfigs(); | ||
return ( | ||
<html> | ||
<body> | ||
<StyledComponentsRegistry> | ||
<GlobalStyle /> | ||
<ResponsiveSpacingStyle /> | ||
<SiteConfigsProvider siteConfigs={siteConfigs}>{children}</SiteConfigsProvider> | ||
</StyledComponentsRegistry> | ||
</body> | ||
</html> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use client"; | ||
|
||
import type { PublicSiteConfig } from "@src/site-configs.d"; | ||
import { createContext, PropsWithChildren, useContext } from "react"; | ||
|
||
export const SiteConfigContext = createContext<PublicSiteConfig | undefined>(undefined); | ||
export const useSiteConfig = () => { | ||
const siteConfig = useContext(SiteConfigContext); | ||
if (!siteConfig) throw new Error("SiteConfig not set in SiteConfigProvider"); | ||
return siteConfig; | ||
}; | ||
|
||
export function SiteConfigProvider({ children, siteConfig }: PropsWithChildren<{ siteConfig?: PublicSiteConfig }>) { | ||
return <SiteConfigContext.Provider value={siteConfig}>{children}</SiteConfigContext.Provider>; | ||
} |