-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jamie B <[email protected]> Co-authored-by: Marjan Kalanaki <[email protected]> Co-authored-by: Frederick O'Brien <[email protected]> Co-authored-by: Simon Adcock <[email protected]> Co-authored-by: Simon Adcock <[email protected]>
- Loading branch information
1 parent
2a1bd37
commit 97c4202
Showing
17 changed files
with
307 additions
and
64 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 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,24 @@ | ||
/* eslint-disable ssr-friendly/no-dom-globals-in-module-scope */ | ||
// @ts-expect-error: Cannot find module | ||
import css from '@guardian/react-crossword/lib/index.css'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { Crosswords } from '../components/Crosswords.editions'; | ||
import type { FEEditionsCrossword } from '../types/editionsCrossword'; | ||
|
||
const style = document.createElement('style'); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- We know this will be a string | ||
style.innerHTML = css; | ||
document.body.appendChild(style); | ||
|
||
const element = document.getElementById('editions-crossword-player'); | ||
if (!element) { | ||
throw new Error('No element found with id "editions-crossword-player"'); | ||
} | ||
const crosswordsData = element.dataset.crosswords; | ||
if (!crosswordsData) { | ||
throw new Error('No data found for "editions-crossword-player"'); | ||
} | ||
|
||
const crosswords = JSON.parse(crosswordsData) as FEEditionsCrossword[]; | ||
const root = createRoot(element); | ||
root.render(<Crosswords crosswords={crosswords} timeZone={undefined} />); |
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
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 @@ | ||
import { StrictMode } from 'react'; | ||
import type { FEEditionsCrosswords } from '../types/editionsCrossword'; | ||
|
||
interface Props { | ||
editionsCrosswords: FEEditionsCrosswords; | ||
} | ||
|
||
export const EditionsCrosswordPage = ({ editionsCrosswords }: Props) => { | ||
return ( | ||
<StrictMode> | ||
<main | ||
id="editions-crossword-player" | ||
data-crosswords={JSON.stringify(editionsCrosswords.crosswords)} | ||
></main> | ||
</StrictMode> | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -114,8 +114,7 @@ | |
"length", | ||
"number", | ||
"position", | ||
"separatorLocations", | ||
"solution" | ||
"separatorLocations" | ||
] | ||
} | ||
}, | ||
|
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,8 +1,13 @@ | ||
import type { RequestHandler } from 'express'; | ||
import { validateAsEditionsCrosswordType } from '../model/validate'; | ||
import { makePrefetchHeader } from './lib/header'; | ||
import { renderCrosswordHtml } from './render.editionsCrossword'; | ||
|
||
export const handleEditionsCrossword: RequestHandler = ({ body }, res) => { | ||
const editionsCrosswords = validateAsEditionsCrosswordType(body); | ||
console.log(editionsCrosswords); | ||
res.sendStatus(200); | ||
const { html, prefetchScripts } = renderCrosswordHtml({ | ||
editionsCrosswords, | ||
}); | ||
|
||
res.status(200).set('Link', makePrefetchHeader(prefetchScripts)).send(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
interface Props { | ||
html: string; | ||
scriptTags: string[]; | ||
} | ||
|
||
export const htmlCrosswordPageTemplate = (props: Props): string => { | ||
const { html, scriptTags } = props; | ||
|
||
return `<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> | ||
<meta name="robots" content="noindex"> | ||
${scriptTags.join('\n')} | ||
</head> | ||
<body> | ||
${html} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { isString } from '@guardian/libs'; | ||
import { EditionsCrosswordPage } from '../components/EditionsCrosswordPage'; | ||
import { generateScriptTags, getPathFromManifest } from '../lib/assets'; | ||
import { renderToStringWithEmotion } from '../lib/emotion'; | ||
import type { FEEditionsCrosswords } from '../types/editionsCrossword'; | ||
import { htmlCrosswordPageTemplate } from './htmlCrosswordPageTemplate'; | ||
|
||
interface Props { | ||
editionsCrosswords: FEEditionsCrosswords; | ||
} | ||
|
||
export const renderCrosswordHtml = ({ | ||
editionsCrosswords, | ||
}: Props): { html: string; prefetchScripts: string[] } => { | ||
const { html } = renderToStringWithEmotion( | ||
<EditionsCrosswordPage editionsCrosswords={editionsCrosswords} />, | ||
); | ||
|
||
const prefetchScripts = [ | ||
getPathFromManifest('client.editionsCrossword', 'index.js'), | ||
].filter(isString); | ||
|
||
const scriptTags = generateScriptTags(prefetchScripts); | ||
|
||
const pageHtml = htmlCrosswordPageTemplate({ | ||
html, | ||
scriptTags, | ||
}); | ||
|
||
return { html: pageHtml, prefetchScripts }; | ||
}; |
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
Oops, something went wrong.