fix: open graph tags for production (uffa)

pull/1/head
Antonio De Lucreziis 2 years ago
parent d6fbbde329
commit 71280fba5c

@ -27,7 +27,7 @@ const SORT_ORDER: Record<SortOrder, boolean> = {
export const HomePage = () => {
const metadata = useContext(MetadataContext)
metadata.title = `PHC Problemi`
metadata.description = 'Bacheca di problemi del PHC'
metadata.description = 'Bacheca di problemi di matematica creato dal PHC'
const [problems] = useResource<(ProblemModel & { solutionsCount: number })[]>('/api/problems', [])

@ -21,11 +21,16 @@
<script data-goatcounter="https://analytics.phc.dm.unipi.it/count" async src="//analytics.phc.dm.unipi.it/count.js"></script>
<!-- INJECT META TAGS -->
<meta property="og:type" content="website" />
<!-- START META TAGS -->
<!-- INJECT META TAGS -->
<!-- END META TAGS -->
</head>
<body>
<!-- START SSR OUTLET -->
<!-- SSR OUTLET -->
<!-- END SSR OUTLET -->
<script type="module" src="/client/entry-client.tsx"></script>
</body>
</html>

@ -33,7 +33,6 @@ async function createDevRouter() {
})
r.use(vite.middlewares)
r.use('*', async (req, res, next) => {
try {
const indexHtml = await fs.readFile(path.resolve('./index.html'), 'utf-8')
@ -47,8 +46,12 @@ async function createDevRouter() {
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`
const metaTagsHtml =
'' +
(metadata.title ? `<meta property="og:title" content="${metadata.title}" />\n` : '') +
(metadata.description ? `<meta property="og:description" content="${metadata.description}" />\n` : '') +
(metadata.title ? `<meta property="og:title" content="${metadata.title ?? 'PHC | Problemi'}" />\n` : '') +
(metadata.description
? `<meta property="og:description" content="${
metadata.description ?? 'Bacheca di problemi di matematica creato dal PHC'
}" />\n`
: '') +
`<meta property="og:url" content="${fullUrl}" />\n`
res.send(transformedTemplate.replace('<!-- INJECT META TAGS -->', metaTagsHtml).replace('<!-- SSR OUTLET -->', html))
@ -69,26 +72,37 @@ async function createProductionRouter() {
const r = express.Router()
r.use('/', express.static('dist/entry-client'))
r.use('*', async (req, res) => {
const handleSSR = async (req, res) => {
const transformedTemplate = await fs.readFile(path.resolve('./dist/entry-client/index.html'), 'utf-8')
const { html, metadata } = render(req.originalUrl)
console.dir(transformedTemplate, { depth: null })
console.dir(html, { depth: null })
console.dir(metadata, { depth: null })
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`
const metaTagsHtml =
'' +
(metadata.title ? `<meta property="og:title" content="${metadata.title ?? 'PHC | Problemi'}" />\n` : '') +
(metadata.title
? `<meta property="og:title" content="${metadata.title}" />\n`
: `<meta property="og:title" content="PHC | Problemi" />\n`) +
(metadata.description
? `<meta property="og:description" content="${
metadata.description ?? 'Bacheca di problemi di matematica creato dal PHC'
}" />\n`
: '') +
? `<meta property="og:description" content="${metadata.description}" />\n`
: `<meta property="og:description" content="Bacheca di problemi di matematica creato dal PHC" />\n`) +
`<meta property="og:url" content="${fullUrl}" />\n`
res.send(transformedTemplate.replace('<!-- INJECT META TAGS -->', metaTagsHtml).replace('<!-- SSR OUTLET -->', html))
})
const finalHtml = transformedTemplate.replace('<!-- INJECT META TAGS -->', metaTagsHtml).replace('<!-- SSR OUTLET -->', html)
console.dir(finalHtml, { depth: null })
res.send(finalHtml)
}
r.get('/', handleSSR)
r.use('/', express.static('dist/entry-client'))
r.use('*', handleSSR)
return r
}

@ -179,7 +179,7 @@ export async function createApiRouter() {
}
// uno studente può modificare solo il campo "content"
if (user.role === 'moderator' && !validateObjectKeys<keyof ProblemModel>(req.body, ['content'])) {
if (user.role === 'moderator' && !validateObjectKeys<keyof ProblemModel>(req.body, ['title', 'content'])) {
res.status(StatusCodes.UNAUTHORIZED)
res.send(`a moderator can only modify the field "content"`)
return

Loading…
Cancel
Save