fix: open graph tags for production (uffa)

This commit is contained in:
2023-01-11 20:38:49 +01:00
parent d6fbbde329
commit 71280fba5c
4 changed files with 35 additions and 16 deletions
+27 -13
View File
@@ -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
}