Skip to content

Commit

Permalink
SEO optimization for pvpq.net with express and cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammers21 committed Aug 2, 2024
1 parent cae3d22 commit 3e102a6
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 2 deletions.
231 changes: 231 additions & 0 deletions frontend/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 frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@testing-library/user-event": "^13.5.0",
"axios": "^1.4.0",
"chart.js": "^4.4.1",
"cheerio": "^1.0.0-rc.12",
"clone": "^2.1.2",
"dayjs": "^1.11.9",
"dayjs-ext": "^2.2.0",
Expand Down
71 changes: 69 additions & 2 deletions frontend/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
const express = require('express');
const cheerio = require('cheerio');
const bodyParser = require('body-parser')
const path = require('path');
const fs = require('fs');


const app = express();
const indexHtml = path.join(__dirname, 'build', 'index.html');
const indexHtmlContent = fs.readFileSync(indexHtml, 'utf8');
const $ = cheerio.load(indexHtmlContent);

function capitalizeFirstLetter(str) {
if (!str) return;
return str.charAt(0).toUpperCase() + str.slice(1);
}

function renderCharacter(region, realm, name) {
const title = `${capitalizeFirstLetter(name)}-${capitalizeFirstLetter(realm)} on ${region.toUpperCase()}`;
const clone = cheerio.load($.html());
clone('title').replaceWith('<title>' + title + '</title>');
return clone.html();
}

function renderActivity(region, activity, bracket) {
let title;
if (bracket === "shuffle-multiclass") {
title = "Shuffle Multiclassers Leaderboard on " + region.toUpperCase();
} else {
title = `${capitalizeFirstLetter(bracket)} ${capitalizeFirstLetter(activity)} on ${region.toUpperCase()}`;
}
const clone = cheerio.load($.html());
clone('title').replaceWith('<title>' + title + '</title>');
return clone.html();
}

function renderThree(one, two, three) {
const regionLc = one.toLowerCase();
const activity = two.toLowerCase();
const bracket = three.toLowerCase();
if (
(regionLc === 'eu' || regionLc === 'us') &&
(activity === 'activity' || activity === 'ladder') &&
(bracket === '2v2' || bracket === '3v3' || bracket === 'rbg' || bracket === 'shuffle' || bracket === 'shuffle-multiclass')
) {
return renderActivity(regionLc, activity, bracket);
} else {
return renderCharacter(one, two, three);
}
}

app.use(express.static(path.join(__dirname, 'build')));

app.get('/ping', function (req, res) {
return res.send('pong');
});

app.get('/:pone/:ptwo', function (req, res) {
const path = req.path;
const pone = req.params.pone;
const ptwo = req.params.ptwo;
console.log('TWO: ', pone, ptwo);
res.send($.html());
});

app.get('/:pone/:ptwo/:pthree', function (req, res) {
const path = req.path;
const pone = req.params.pone;
const ptwo = req.params.ptwo;
const pthree = req.params.pthree;
console.log('THREE: ', pone, ptwo, pthree);
res.send(renderThree(pone, ptwo, pthree));
});

app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
console.log('NO MATCH: ', req.path);
res.send($.html());
});

app.listen(process.env.PORT || 8080)
let port = process.env.PORT || 8080;
console.log('Starting server on port ' + port)
app.listen(port)

0 comments on commit 3e102a6

Please sign in to comment.