fix: fix outgoing url on error page

also refactor some related code
This commit is contained in:
zyachel 2023-01-15 21:41:09 +05:30
parent 44229f8702
commit 595b720ee1
9 changed files with 32 additions and 35 deletions

4
app.js
View file

@ -50,9 +50,7 @@ app.use(
if (process.env.NODE_ENV === 'development') app.use(morgan('dev')); // for logging during development if (process.env.NODE_ENV === 'development') app.use(morgan('dev')); // for logging during development
// middleware to add baseUrl to req object // middleware to add baseUrl to req object
app.use((req, res, next) => { app.use((req, res, next) => {
req.urlObj = new URL( req.urlObj = new URL(req.originalUrl, `${req.protocol}://${req.get('host')}`);
`${req.protocol}://${req.get('host')}${req.originalUrl}`
);
next(); next();
}); });

View file

@ -35,9 +35,8 @@ const sendErrorResponse = (err, req, res, devMode = false) => {
}, },
meta: { meta: {
title: 'Error', title: 'Error',
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
urlObj: req.urlObj,
description: `ERROR: ${err.message}. Please try again later.`, description: `ERROR: ${err.message}. Please try again later.`,
}, },
}); });

View file

@ -5,7 +5,7 @@
import catchAsyncErrors from '../utils/catchAsyncErrors.js'; import catchAsyncErrors from '../utils/catchAsyncErrors.js';
import getAnswers from '../fetchers/getAnswers.js'; import getAnswers from '../fetchers/getAnswers.js';
import getTopic from '../fetchers/getTopic.js'; import getTopic from '../fetchers/getTopic.js';
import { nonSlugRoutes } from '../utils/constants.js'; import { acceptedLanguages, nonSlugRoutes } from '../utils/constants.js';
import getProfile from '../fetchers/getProfile.js'; import getProfile from '../fetchers/getProfile.js';
import getSearch from '../fetchers/getSearch.js'; import getSearch from '../fetchers/getSearch.js';
@ -16,7 +16,7 @@ export const about = (req, res, next) => {
res.render('about', { res.render('about', {
meta: { meta: {
title: 'About', title: 'About',
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: description:
'Quetre is a libre front-end for Quora. See any answer without being tracked, without being required to log in, and without being bombarded by pesky ads.', 'Quetre is a libre front-end for Quora. See any answer without being tracked, without being required to log in, and without being bombarded by pesky ads.',
@ -28,7 +28,7 @@ export const privacy = (req, res, next) => {
res.render('privacy', { res.render('privacy', {
meta: { meta: {
title: 'Privacy', title: 'Privacy',
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: 'Privacy Policy of Quetre, a libre front-end for Quora.', description: 'Privacy Policy of Quetre, a libre front-end for Quora.',
}, },
@ -44,14 +44,14 @@ export const answers = catchAsyncErrors(async (req, res, next) => {
const answersData = await getAnswers(slug, lang); const answersData = await getAnswers(slug, lang);
const title = answersData.question.text[0].spans const title = answersData.question.text[0].spans
.map((span) => span.text) .map(span => span.text)
.join(''); .join('');
return res.status(200).render('answers', { return res.status(200).render('answers', {
data: answersData, data: answersData,
meta: { meta: {
title, title,
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: `Answers to ${title}`, description: `Answers to ${title}`,
}, },
@ -68,7 +68,7 @@ export const topic = catchAsyncErrors(async (req, res, next) => {
data: topicData, data: topicData,
meta: { meta: {
title: topicData.name, title: topicData.name,
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: `Information about ${topicData.name} topic.`, description: `Information about ${topicData.name} topic.`,
}, },
@ -84,7 +84,7 @@ export const profile = catchAsyncErrors(async (req, res, next) => {
data: profileData, data: profileData,
meta: { meta: {
title: profileData.basic.name, title: profileData.basic.name,
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: `${profileData.basic.name}'s profile.`, description: `${profileData.basic.name}'s profile.`,
}, },
@ -101,7 +101,7 @@ export const search = catchAsyncErrors(async (req, res, next) => {
data: searchData, data: searchData,
meta: { meta: {
title: searchText || 'Search', title: searchText || 'Search',
url: req.urlObj.href, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
description: searchText ? `results for '${searchText}'` : 'search page', description: searchText ? `results for '${searchText}'` : 'search page',
}, },
@ -118,9 +118,8 @@ export const unimplemented = (req, res, next) => {
data, data,
meta: { meta: {
title: 'Not yet implemented', title: 'Not yet implemented',
url: `${req.urlObj.origin}${req.urlObj.pathname}`, url: req.urlObj,
imageUrl: `${req.urlObj.origin}/icon.svg`, imageUrl: `${req.urlObj.origin}/icon.svg`,
urlObj: req.urlObj,
description: data.message, description: data.message,
}, },
}); });

View file

@ -43,8 +43,7 @@ const fetcher = async (
return true; return true;
}); });
if (!rawData || !Object.entries(rawData).length) if (!rawData) throw new AppError("couldn't retrieve data", 500);
throw new AppError("couldn't retrieve data", 500);
return JSON.parse(rawData); return JSON.parse(rawData);
} catch (err) { } catch (err) {

View file

@ -13,25 +13,25 @@ mixin proxifyImg(imgUrl)
img(src=imgUrl.replace('https://', '/api/v1/image/'), loading='lazy')&attributes(attributes) img(src=imgUrl.replace('https://', '/api/v1/image/'), loading='lazy')&attributes(attributes)
mixin quetrefyUrl(url, text) mixin quetrefyUrl(url, text)
- -
const acceptedLanguages=['en','es','fr','de','it','ja','id','pt','hi','nl','da','fi','nb','sv','mr','bn','ta','ar','he','gu','kn','ml','te','po'];
let link; let link;
const acceptedLanguages=['en','es','fr','de','it','ja','id','pt','hi','nl','da','fi','nb','sv','mr','bn','ta','ar','he','gu','kn','ml','te','po',]; const match = /^https:\/\/(.{2,})\.quora\.com(\/.*)$/.exec(url);
try {
link = new URL(url); if (match) {
const subdomain = link.hostname.split('.')[0]; const [_, subdomain, rest] = match;
if (!link.hostname.includes('quora.com')) link = url; if (acceptedLanguages.includes(subdomain)) link = link = `${rest}${rest.includes('?') ? '&' : '?'}lang=${subdomain}`;
else if (subdomain === 'www') return link.pathname; else if (subdomain === 'www') link = rest;
else if (acceptedLanguages.includes(subdomain)) link = `${link.pathname}?lang=${subdomain}`; else link = `/space/${subdomain}${rest}`;
else link = `/space/${subdomain}${link.pathname}`; } else {
} catch {
link = url; link = url;
} }
a(href=link)&attributes(attributes)= text a(href=link)&attributes(attributes)= text
mixin quorafyUrl(url) mixin quorafyUrl(url, text='View on Quora')
- -
const link = new URL(url, 'https://www.quora.com'); const link = new URL(`${url.pathname}${url.search}`, 'https://www.quora.com');
const lang = link.searchParams.get('lang'); const lang = link.searchParams.get('lang');
const match = /(?<=^\/space\/)([^\/]+)\/(.*)$/.exec(link.pathname); // taking space name and rest of pathname out. const match = /(?<=^\/space\/)([^\/]+)\/(.*)$/.exec(link.pathname); // taking space name and rest of pathname out.
if(lang) { if(lang) {
@ -42,4 +42,4 @@ mixin quorafyUrl(url)
link.pathname = match[2]; link.pathname = match[2];
} }
a(href=link.href, rel='noreferrer', target='_blank')&attributes(attributes) View on Quora a(href=link.href, rel='noreferrer', target='_blank')&attributes(attributes)= text

View file

@ -4,6 +4,7 @@
extends ../base extends ../base
include ../mixins/_formatText include ../mixins/_formatText
include ../mixins/_answer include ../mixins/_answer
include ../mixins/_utils
//-////////////////////////////////////////////////////// //-//////////////////////////////////////////////////////
//- MAIN CONTENT //- MAIN CONTENT
@ -18,7 +19,7 @@ block content
.answers__metadata .answers__metadata
p.answers__answers-total= `${ data.numAnswers ? 'Total answers: ' + data.numAnswers : 'Unanswered'}` p.answers__answers-total= `${ data.numAnswers ? 'Total answers: ' + data.numAnswers : 'Unanswered'}`
p.answers__answers-shown Viewable answers: #{data.answers.length} p.answers__answers-shown Viewable answers: #{data.answers.length}
+quorafyUrl(data.question.url)(class='answers__question-link answers__link') +quorafyUrl(meta.url)(class='answers__question-link answers__link')
//- ANSWERS TO THIS QUESTION //- ANSWERS TO THIS QUESTION
.answers-box.answers__answers-box .answers-box.answers__answers-box

View file

@ -2,6 +2,7 @@
//- INCLUDES/EXTENDS //- INCLUDES/EXTENDS
//-////////////////////////////////////////////////////// //-//////////////////////////////////////////////////////
extends ../base extends ../base
include ../mixins/_utils
//-////////////////////////////////////////////////////// //-//////////////////////////////////////////////////////
//- MAIN CONTENT //- MAIN CONTENT
@ -22,5 +23,5 @@ block content
a.error__link(href="/") Home Page a.error__link(href="/") Home Page
|. |.
p.error__return Or view this route&nbsp; p.error__return Or view this route&nbsp;
a.error__link(href="https://www.quora.com" + meta.urlObj.pathname) on Quora +quorafyUrl(meta.url, 'on Quora')(class='error__link')
|. |.

View file

@ -60,7 +60,7 @@ block content
+addMetadataSecondary('flower', 'Deceased', '', 'empty') +addMetadataSecondary('flower', 'Deceased', '', 'empty')
if data.basic.isBusiness if data.basic.isBusiness
+addMetadataSecondary('briefcase', 'Business', '', 'empty') +addMetadataSecondary('briefcase', 'Business', '', 'empty')
+quorafyUrl(data.basic.profile)(class='link') +quorafyUrl(meta.url)(class='link')
if data.profileFeed.description[0].spans[0].text if data.profileFeed.description[0].spans[0].text
.profile-meta__description .profile-meta__description

View file

@ -24,7 +24,7 @@ block content
+addMetadataSecondary('question','Questions', data.numQuestions) +addMetadataSecondary('question','Questions', data.numQuestions)
if data.isAdult if data.isAdult
+addMetadataSecondary('danger', 'Adult Topic', '18+', true) +addMetadataSecondary('danger', 'Adult Topic', '18+', true)
+quorafyUrl(data.url)(class='link') +quorafyUrl(meta.url)(class='link')
//- AUTHORS RELATED TO THE TOPIC AND METADATA //- AUTHORS RELATED TO THE TOPIC AND METADATA
.topic__famous-authors.famous-authors .topic__famous-authors.famous-authors