Skip to content

Commit

Permalink
Merge pull request #16 from apostrophecms/pro-4493-fix-locale-with-pr…
Browse files Browse the repository at this point in the history
…efix

raw redirects to internal url and compare with redirectSlug
  • Loading branch information
ValJed authored Aug 16, 2023
2 parents 847b013 + 94d01b1 commit 7a09dfb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fixes redirections when using locale prefixes. The locale prefix must be added to the `redirectSlug` to avoid conflicts with other locales.

## 1.2.2 (2023-03-06)

- Removes `apostrophe` as a peer dependency.
Expand Down
52 changes: 28 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
},
init(self) {
self.addUnlocalizedMigration();
self.createIndexes();
},
handlers(self) {
return {
Expand Down Expand Up @@ -146,34 +147,34 @@ module.exports = {
return {
async checkRedirect(req, res, next) {
try {
const slug = req.url;
const pathOnly = slug.split('?')[0];
const redirectRegEx = new RegExp(`^redirect-${self.apos.util.regExpQuote(pathOnly)}(\\?.*)?$`);
const results = await self.find(req, { slug: redirectRegEx }).toArray();
let target;
if (results) {
if (results.some(result => result.redirectSlug === slug)) {
target = results.find(result => result.redirectSlug === slug);
} else if (results.some(result => result.redirectSlug === pathOnly && result.ignoreQueryString)) {
target = results.find(result => result.redirectSlug === pathOnly && result.ignoreQueryString);
}
const slug = req.originalUrl;
const [ pathOnly ] = slug.split('?');
const results = await self
.find(req, { $or: [ { redirectSlug: slug }, { redirectSlug: pathOnly } ] })
.toArray();

if (target) {
let status = parseInt(target.statusCode);
if (!results.length) {
return await emitAndRedirectOrNext();
}

if (isNaN(status) || !status) {
status = 302;
}
const target = results.find(({ redirectSlug }) => redirectSlug === slug) ||
results.find(({
redirectSlug,
ignoreQueryString
}) => redirectSlug === pathOnly && ignoreQueryString);

if (target.urlType === 'internal' && target._newPage && target._newPage[0]) {
return req.res.redirect(status, target._newPage[0]._url);
} else if (target.urlType === 'external' && target.externalUrl.length) {
return req.res.redirect(status, target.externalUrl);
} else {
return await emitAndRedirectOrNext();
}
}
if (!target) {
return await emitAndRedirectOrNext();
}

const parsedCode = parseInt(target.statusCode);
const status = (parsedCode && !isNaN(parsedCode)) ? parsedCode : 302;
if (target.urlType === 'internal' && target._newPage && target._newPage[0]) {
return req.res.rawRedirect(status, target._newPage[0]._url);
} else if (target.urlType === 'external' && target.externalUrl.length) {
return req.res.rawRedirect(status, target.externalUrl);
}

return await emitAndRedirectOrNext();
} catch (e) {
self.apos.util.error(e);
Expand Down Expand Up @@ -214,6 +215,9 @@ module.exports = {
}
}
});
},
createIndexes() {
self.apos.doc.db.createIndex({ redirectSlug: 1 });
}
};
}
Expand Down

0 comments on commit 7a09dfb

Please sign in to comment.