0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

🐛 Fixed throwing errors during link redirects

no issue

Errors weren't correctly passed on to Express in the middleware.
This commit is contained in:
Simon Backx 2023-02-08 11:49:15 +01:00 committed by Daniel Lockyer
parent f7aefa2feb
commit b7be02f057
No known key found for this signature in database

View file

@ -85,28 +85,32 @@ class LinkRedirectsService {
* @returns {Promise<void>}
*/
async handleRequest(req, res, next) {
// skip handling if original url doesn't match the prefix
const fullURLWithRedirectPrefix = `${this.#baseURL.pathname}${this.#redirectURLPrefix}`;
if (!req.originalUrl.startsWith(fullURLWithRedirectPrefix)) {
return next();
try {
// skip handling if original url doesn't match the prefix
const fullURLWithRedirectPrefix = `${this.#baseURL.pathname}${this.#redirectURLPrefix}`;
if (!req.originalUrl.startsWith(fullURLWithRedirectPrefix)) {
return next();
}
const url = new URL(req.originalUrl, this.#baseURL);
const link = await this.#linkRedirectRepository.getByURL(url);
if (!link) {
return next();
}
const event = RedirectEvent.create({
url,
link
});
DomainEvents.dispatch(event);
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
return res.redirect(link.to.href);
} catch (e) {
return next(e);
}
const url = new URL(req.originalUrl, this.#baseURL);
const link = await this.#linkRedirectRepository.getByURL(url);
if (!link) {
return next();
}
const event = RedirectEvent.create({
url,
link
});
DomainEvents.dispatch(event);
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
return res.redirect(link.to.href);
}
}