NewsCrawler.org

Matching a CMS Markdown preview to the public site with iframe and createPortal

管理画面の Markdown プレビューを公開サイトと同じ見た目にする(iframe + createPortal)
公開サイトと管理画面のプレビューの比較
ビルド時に公開サイトの CSS を生成し、実行時に iframe から読み込む流れ
Image: zenn.dev

The author's company, MyVision, runs several job-support sites, each with its own design, and manages their articles in an internal admin CMS that has a Markdown preview. The preview did not always look like the public sites, because the admin app's own Tailwind CSS was applied instead of each site's CSS. The article's fix renders the preview inside an iframe, which loads each public site's CSS, while createPortal draws the React tree into the iframe's body, so props and context still reach the renderer.

The public sites' CSS is generated at build time from each app's entry CSS using @tailwindcss/postcss and is loaded with a link tag whose query string holds the build time. The author notes that the base option must point to each app's own directory, otherwise the script scans the admin app's source and produces the wrong CSS without an error. Fonts set with next/font are handled by generating a separate CSS file with the Google Fonts import.

Because Turborepo caches tasks, the source CSS files must be listed as inputs and the generated CSS as an output, or stale CSS is served. The author says the method does not depend on the admin app's own styling.

Read the original zenn.dev

Sources

  • Zenn JA

    管理画面の Markdown プレビューを公開サイトと同じ見た目にする(iframe + createPortal)

    21 likes0 bookmarks0 comments

Terms

  1. monorepo — A single repository that holds several related projects or apps.
  2. createPortal — A React function that renders child components into a different DOM node than their parent element.
  3. Turborepo — A build tool for monorepos that runs tasks and caches their results so unchanged work is skipped.

Published: First seen: Читать по-русски

Open as a page