🐛 Fixed throwing errors during link redirects
no issue Errors weren't correctly passed on to Express in the middleware.
This commit is contained in:
parent
3b6759ca6d
commit
4436a1cd18
@ -85,34 +85,38 @@ class LinkRedirectsService {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async handleRequest(req, res, next) {
|
async handleRequest(req, res, next) {
|
||||||
// skip handling if original url doesn't match the prefix
|
try {
|
||||||
const fullURLWithRedirectPrefix = `${this.#baseURL.pathname}${this.#redirectURLPrefix}`;
|
// skip handling if original url doesn't match the prefix
|
||||||
// @NOTE: below is equivalent to doing:
|
const fullURLWithRedirectPrefix = `${this.#baseURL.pathname}${this.#redirectURLPrefix}`;
|
||||||
// router.get('/r/'), (req, res) ...
|
// @NOTE: below is equivalent to doing:
|
||||||
// To make it cleaner we should rework it to:
|
// router.get('/r/'), (req, res) ...
|
||||||
// linkRedirects.service.handleRequest(router);
|
// To make it cleaner we should rework it to:
|
||||||
// and mount routes on top like for example sitemapHandler does
|
// linkRedirects.service.handleRequest(router);
|
||||||
// Cleanup issue: https://github.com/TryGhost/Toolbox/issues/516
|
// and mount routes on top like for example sitemapHandler does
|
||||||
if (!req.originalUrl.startsWith(fullURLWithRedirectPrefix)) {
|
// Cleanup issue: https://github.com/TryGhost/Toolbox/issues/516
|
||||||
return next();
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user