Skip to content

Commit

Permalink
✨ Dynamic meta tags for public crates
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Aug 18, 2021
1 parent 5504572 commit 5d925a7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 15 deletions.
64 changes: 50 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"homepage": "https://github.com/betahuhn/webcrate#README",
"dependencies": {
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.10",
"compression": "^1.7.4",
"cors": "^2.8.5",
"deta": "^1.0.0",
Expand Down
4 changes: 3 additions & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import compression from 'compression'
import cors from 'cors'

import routes from './router'
import { routeLog, sendResponse, disableCaching, checkIfSetup } from './middleware'
import { routeLog, sendResponse, disableCaching, checkIfSetup, renderMetaTags } from './middleware'
import log from './utils/log'

const app = express()
Expand All @@ -25,6 +25,8 @@ app.use(cors())
// Disable caching for /, reference: https://www.notion.so/Turning-off-Caching-on-the-Root-9879ed9411a4486dbeaf4cc57697d610
app.use(disableCaching)

app.use(renderMetaTags)

// Serve Nuxt static files
app.use(express.static(path.join(__dirname, '../dist')))

Expand Down
42 changes: 42 additions & 0 deletions server/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import fs from 'fs'
import path from 'path'
import express from 'express'
import cheerio from 'cheerio'

import log from '../utils/log'
import { messages } from '../utils/status'
import { isSetup } from '../utils/isSetup'
import emojis from '../utils/emojis'

import { Crate } from '../models/crate'
import { Stat } from '../models/stats'
Expand Down Expand Up @@ -33,6 +37,44 @@ export function disableCaching(req: express.Request, res: express.Response, next
next()
}

export async function renderMetaTags(req: express.Request, res: express.Response, next: express.NextFunction) {
// Only run on public crates
if (req.originalUrl.startsWith('/crate/public')) {

// Parse the crate id from the URL
const id = req.originalUrl.split('/crate/public/')[1]

// Check if the crate exists
const crate = await Crate.findOne({ id, public: true })
if (!crate) return next()

// Read the html file
const file = await fs.promises.readFile(path.join(__dirname, '../../dist/200.html'))
const html = file.toString()

// Load the html file into cheerio
const $ = cheerio.load(html)

// New title and description values
const title = `${ emojis[crate.icon] } ${ crate.name } | WebCrate`
const description = `${ crate.description } - View on WebCrate`

// Replace title
$('title').text(title)
$(`meta[name='title']`).attr('content', title)
$(`meta[name='og:title']`).attr('content', title)

// Replace description
$(`meta[name='description']`).attr('content', description)
$(`meta[name='og:description']`).attr('content', description)

// Render HTML
return res.send($.html())
}

next()
}

export async function checkIfSetup(req: express.Request, res: express.Response, next: express.NextFunction) {
// Check if WebCrate already set up
const setup = await isSetup()
Expand Down

0 comments on commit 5d925a7

Please sign in to comment.