diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 5981cdc47a..0f6b5b4a34 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -58,9 +58,10 @@ helpers.buildQueryString = function (query, key, value) { helpers.addLinkTags = function (params) { params.res.locals.linkTags = params.res.locals.linkTags || []; + const page = params.page > 1 ? `?page=${params.page}` : ''; params.res.locals.linkTags.push({ rel: 'canonical', - href: `${url}/${params.url}`, + href: `${url}/${params.url}${page}`, }); params.tags.forEach((rel) => { diff --git a/src/controllers/recent.js b/src/controllers/recent.js index 32d253edd7..878f74d6b3 100644 --- a/src/controllers/recent.js +++ b/src/controllers/recent.js @@ -91,7 +91,12 @@ recentController.getData = async function (req, url, sort) { const pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage)); data.pagination = pagination.create(page, pageCount, req.query); - helpers.addLinkTags({ url: url, res: req.res, tags: data.pagination.rel }); + helpers.addLinkTags({ + url: url, + res: req.res, + tags: data.pagination.rel, + page: page, + }); return data; }; diff --git a/src/controllers/tags.js b/src/controllers/tags.js index 3165303445..1b1d1a98f2 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -61,7 +61,12 @@ tagsController.getTag = async function (req, res) { const pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage)); templateData.pagination = pagination.create(page, pageCount, req.query); - helpers.addLinkTags({ url: `tags/${tag}`, res: req.res, tags: templateData.pagination.rel }); + helpers.addLinkTags({ + url: `tags/${tag}`, + res: req.res, + tags: templateData.pagination.rel, + page: page, + }); templateData['feeds:disableRSS'] = meta.config['feeds:disableRSS']; templateData.rssFeedUrl = `${nconf.get('relative_path')}/tags/${tag}.rss`; diff --git a/src/controllers/topics.js b/src/controllers/topics.js index d177da7cbc..58ca059502 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -72,15 +72,15 @@ topicsController.get = async function getTopic(req, res, next) { const sort = req.query.sort || settings.topicPostSort; const set = sort === 'most_votes' ? `tid:${tid}:posts:votes` : `tid:${tid}:posts`; const reverse = sort === 'newest_to_oldest' || sort === 'most_votes'; - if (settings.usePagination) { - if (!req.query.page) { - currentPage = calculatePageFromIndex(postIndex, settings); - } else { - const top = ((currentPage - 1) * settings.postsPerPage) + 1; - const bottom = top + settings.postsPerPage; - if (!req.params.post_index || (postIndex < top || postIndex > bottom)) { - postIndex = top; - } + + if (!req.query.page) { + currentPage = calculatePageFromIndex(postIndex, settings); + } + if (settings.usePagination && req.query.page) { + const top = ((currentPage - 1) * settings.postsPerPage) + 1; + const bottom = top + settings.postsPerPage; + if (!req.params.post_index || (postIndex < top || postIndex > bottom)) { + postIndex = top; } } const { start, stop } = calculateStartStop(currentPage, postIndex, settings); @@ -115,7 +115,7 @@ topicsController.get = async function getTopic(req, res, next) { await Promise.all([ buildBreadcrumbs(topicData), addOldCategory(topicData, userPrivileges), - addTags(topicData, req, res), + addTags(topicData, req, res, currentPage), incrementViewCount(req, tid), markAsRead(req, tid), analytics.increment([`pageviews:byCid:${topicData.category.cid}`]), @@ -201,7 +201,7 @@ async function addOldCategory(topicData, userPrivileges) { } } -async function addTags(topicData, req, res) { +async function addTags(topicData, req, res, currentPage) { const postIndex = parseInt(req.params.post_index, 10) || 0; const postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10)); let description = ''; @@ -256,10 +256,11 @@ async function addTags(topicData, req, res) { await addOGImageTags(res, topicData, postAtIndex); + const page = currentPage > 1 ? `?page=${currentPage}` : ''; res.locals.linkTags = [ { rel: 'canonical', - href: `${url}/topic/${topicData.slug}`, + href: `${url}/topic/${topicData.slug}${page}`, }, ]; diff --git a/src/controllers/unread.js b/src/controllers/unread.js index 5db883a12a..d5809d2089 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -51,7 +51,12 @@ unreadController.get = async function (req, res) { data.pageCount = Math.max(1, Math.ceil(data.topicCount / userSettings.topicsPerPage)); data.pagination = pagination.create(page, data.pageCount, req.query); - helpers.addLinkTags({ url: 'unread', res: req.res, tags: data.pagination.rel }); + helpers.addLinkTags({ + url: 'unread', + res: req.res, + tags: data.pagination.rel, + page: page, + }); if (userSettings.usePagination && (page < 1 || page > data.pageCount)) { req.query.page = Math.max(1, Math.min(data.pageCount, page)); diff --git a/src/user/settings.js b/src/user/settings.js index cb982a4014..6f560e58f7 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -12,7 +12,9 @@ const languages = require('../languages'); module.exports = function (User) { User.getSettings = async function (uid) { if (parseInt(uid, 10) <= 0) { - return await onSettingsLoaded(0, {}); + return await onSettingsLoaded(uid, { + usePagination: parseInt(uid, 10) === -1 ? 1 : undefined, // force spiders to use pagination + }); } let settings = await db.getObject(`user:${uid}:settings`); settings = settings || {};