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:
parent
f7aefa2feb
commit
b7be02f057
1 changed files with 25 additions and 21 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue