-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2292 from NDLANO/refactor/render-in-react
refactor: render the entire document with react.
- Loading branch information
Showing
24 changed files
with
401 additions
and
340 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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Copyright (c) 2025-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { ReactNode } from "react"; | ||
import type { ManifestChunk } from "vite"; | ||
import config from "./config"; | ||
|
||
interface Props { | ||
language: string; | ||
children?: ReactNode; | ||
chunks?: ManifestChunk[]; | ||
devEntrypoint: string; | ||
} | ||
|
||
const getUniqueCss = (chunks: ManifestChunk[]) => { | ||
const uniq = chunks.reduce((acc, curr) => { | ||
curr.css?.forEach((css) => acc.add(css)); | ||
return acc; | ||
}, new Set<string>()); | ||
return Array.from(uniq); | ||
}; | ||
|
||
export const Document = ({ language, children, chunks = [], devEntrypoint }: Props) => { | ||
const faviconEnvironment = config.ndlaEnvironment === "dev" ? "test" : config.ndlaEnvironment; | ||
const locale = language === "nb" || language === "nn" ? "no" : language; | ||
|
||
const [entryPoint, ...importedChunks] = chunks; | ||
const css = getUniqueCss(chunks); | ||
|
||
return ( | ||
<html lang={locale}> | ||
<head> | ||
<link rel="icon" type="image/png" sizes="32x32" href={`/static/favicon-${faviconEnvironment}-32x32.png`} /> | ||
<link rel="icon" type="image/png" sizes="16x16" href={`/static/favicon-${faviconEnvironment}-16x16.png`} /> | ||
<link | ||
rel="apple-touch-icon" | ||
type="image/png" | ||
sizes="180x180" | ||
href={`/static/apple-touch-icon-${faviconEnvironment}.png`} | ||
/> | ||
<meta httpEquiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1 viewport-fit=cover" /> | ||
<link href="https://api.fontshare.com/v2/css?f[]=satoshi@1&display=swap" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<script | ||
type="text/javascript" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
window.dataLayer = window.dataLayer || []; | ||
window._mtm = window._mtm || []; | ||
window.originalLocation = { | ||
originalLocation: | ||
document.location.protocol + | ||
"//" + | ||
document.location.hostname + | ||
document.location.pathname + | ||
document.location.search, | ||
}; | ||
window.dataLayer.push(window.originalLocation); | ||
`, | ||
}} | ||
></script> | ||
{config.runtimeType === "development" && ( | ||
<> | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
import RefreshRuntime from "/@react-refresh"; | ||
RefreshRuntime.injectIntoGlobalHook(window); | ||
window.$RefreshReg$ = () => {}; | ||
window.$RefreshSig$ = () => (type) => type; | ||
window.__vite_plugin_react_preamble_installed__ = true; | ||
`, | ||
}} | ||
type="module" | ||
/> | ||
<script src="/@vite/client" type="module" /> | ||
<script type="module" src={`/${devEntrypoint}`}></script> | ||
</> | ||
)} | ||
<script | ||
// We're hydrating the entire document. Our config differentiates between server and client, so it's necessary to suppress any hydration warnings here. TODO: Find a better workaround for this | ||
suppressHydrationWarning | ||
dangerouslySetInnerHTML={{ | ||
__html: config.isClient ? "" : `window.DATA = "$WINDOW_DATA"`, | ||
}} | ||
></script> | ||
{!!entryPoint && <script type="module" src={`/${entryPoint.file}`}></script>} | ||
{css.map((file) => ( | ||
<link rel="stylesheet" href={`/${file}`} key={file} /> | ||
))} | ||
{importedChunks.map((chunk) => ( | ||
<link rel="modulepreload" href={`/${chunk.file}`} key={chunk.file}></link> | ||
))} | ||
<div id="root">{children}</div> | ||
</body> | ||
</html> | ||
); | ||
}; |
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,17 @@ | ||
/** | ||
* Copyright (c) 2025-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
export type EntryPointType = "default" | "lti" | "iframeArticle" | "iframeEmbed" | "error"; | ||
|
||
export const entryPoints: Record<EntryPointType, string> = { | ||
default: "src/client.tsx", | ||
lti: "src/lti/index.tsx", | ||
iframeArticle: "src/iframe/index.tsx", | ||
iframeEmbed: "src/iframe/embedIframeIndex.tsx", | ||
error: "src/containers/ErrorPage/ErrorEntry.tsx", | ||
} as const; |
Oops, something went wrong.