diff --git a/src/controllers/accounts/follow.js b/src/controllers/accounts/follow.js index 7a28374582..dac4902701 100644 --- a/src/controllers/accounts/follow.js +++ b/src/controllers/accounts/follow.js @@ -1,11 +1,15 @@ 'use strict'; +const nconf = require('nconf'); + const user = require('../../user'); const helpers = require('../helpers'); const pagination = require('../../pagination'); const followController = module.exports; +const url = nconf.get('url'); + followController.getFollowing = async function (req, res, next) { await getFollow('account/following', 'following', req, res, next); }; @@ -39,5 +43,12 @@ async function getFollow(tpl, name, req, res, next) { payload.breadcrumbs = helpers.buildBreadcrumbs([{ text: username, url: `/user/${userslug}` }, { text: `[[user:${name}]]` }]); + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}${req.url.replace(/^\/api/, '')}`, + }, + ]; + res.render(tpl, payload); } diff --git a/src/controllers/accounts/groups.js b/src/controllers/accounts/groups.js index 3a9d66243e..fbe9a39407 100644 --- a/src/controllers/accounts/groups.js +++ b/src/controllers/accounts/groups.js @@ -1,11 +1,15 @@ 'use strict'; +const nconf = require('nconf'); + const user = require('../../user'); const groups = require('../../groups'); const helpers = require('../helpers'); const groupsController = module.exports; +const url = nconf.get('url'); + groupsController.get = async function (req, res) { const { username, userslug } = await user.getUserFields(res.locals.uid, ['username', 'userslug']); @@ -21,5 +25,11 @@ groupsController.get = async function (req, res) { payload.groups = groupsData; payload.title = `[[pages:account/groups, ${username}]]`; payload.breadcrumbs = helpers.buildBreadcrumbs([{ text: username, url: `/user/${userslug}` }, { text: '[[global:header.groups]]' }]); + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}${req.url.replace(/^\/api/, '')}`, + }, + ]; res.render('account/groups', payload); }; diff --git a/src/controllers/accounts/posts.js b/src/controllers/accounts/posts.js index adb8fbffc8..f36834ef0e 100644 --- a/src/controllers/accounts/posts.js +++ b/src/controllers/accounts/posts.js @@ -1,5 +1,7 @@ 'use strict'; +const nconf = require('nconf'); + const db = require('../../database'); const user = require('../../user'); const posts = require('../../posts'); @@ -13,6 +15,8 @@ const utils = require('../../utils'); const postsController = module.exports; +const url = nconf.get('url'); + const templateToData = { 'account/bookmarks': { type: 'posts', @@ -240,6 +244,13 @@ async function getPostsFromUserSet(template, req, res) { option.selected = option.url.includes(`sort=${req.query.sort}`); }); + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}${req.url.replace(/^\/api/, '')}`, + }, + ]; + res.render(template, payload); } diff --git a/src/controllers/accounts/profile.js b/src/controllers/accounts/profile.js index e24415794d..0f2cf3cf9c 100644 --- a/src/controllers/accounts/profile.js +++ b/src/controllers/accounts/profile.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); +const nconf = require('nconf'); const db = require('../../database'); const user = require('../../user'); @@ -14,6 +15,8 @@ const utils = require('../../utils'); const profileController = module.exports; +const url = nconf.get('url'); + profileController.get = async function (req, res, next) { const { userData } = res.locals; if (!userData) { @@ -151,4 +154,11 @@ function addMetaTags(res, userData) { } ); } + + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}/user/${userData.userslug}`, + }, + ]; } diff --git a/src/controllers/groups.js b/src/controllers/groups.js index 76c1b00f97..4638384b6e 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -12,6 +12,8 @@ const privileges = require('../privileges'); const groupsController = module.exports; +const url = nconf.get('url'); + groupsController.list = async function (req, res) { const sort = req.query.sort || 'alpha'; const page = parseInt(req.query.page, 10) || 1; @@ -20,6 +22,13 @@ groupsController.list = async function (req, res) { getGroups(req, sort, page), ]); + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}${req.url.replace(/^\/api/, '')}`, + }, + ]; + res.render('groups/list', { groups: groupData, allowGroupCreation: allowGroupCreation, @@ -101,6 +110,13 @@ groupsController.details = async function (req, res, next) { return next(); } + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}/groups/${lowercaseSlug}`, + }, + ]; + res.render('groups/details', { title: `[[pages:group, ${groupData.displayName}]]`, group: groupData, diff --git a/src/controllers/tags.js b/src/controllers/tags.js index 392ff9201e..01c91f345c 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -14,6 +14,8 @@ const helpers = require('./helpers'); const tagsController = module.exports; +const url = nconf.get('url'); + tagsController.getTag = async function (req, res) { const tag = validator.escape(utils.cleanUpTag(req.params.tag, meta.config.maximumTagLength)); const page = parseInt(req.query.page, 10) || 1; @@ -89,6 +91,13 @@ tagsController.getTags = async function (req, res) { topics.getCategoryTagsData(cids, 0, 99), ]); + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}/tags`, + }, + ]; + res.render('tags', { tags: tags.filter(Boolean), displayTagSearch: canSearch, diff --git a/src/controllers/users.js b/src/controllers/users.js index 41194e6c82..e2deb1d93b 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -1,5 +1,7 @@ 'use strict'; +const nconf = require('nconf'); + const user = require('../user'); const meta = require('../meta'); @@ -12,6 +14,8 @@ const utils = require('../utils'); const usersController = module.exports; +const url = nconf.get('url'); + usersController.index = async function (req, res, next) { const section = req.query.section || 'joindate'; const sectionToController = { @@ -206,6 +210,13 @@ async function render(req, res, data) { data['reputation:disabled'] = meta.config['reputation:disabled']; + res.locals.linkTags = [ + { + rel: 'canonical', + href: `${url}${req.url.replace(/^\/api/, '')}`, + }, + ]; + res.append('X-Total-Count', data.userCount); res.render('users', data); }