Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check url with locale prefixes and compare with redirectSlug #16

Merged
merged 8 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 locales prefixes. The locale prefix must be added to the `redirectSlug` to avoid conflicts with other locales.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locale prefixes


## 1.2.2 (2023-03-06)

- Removes `apostrophe` as a peer dependency.
Expand Down
44 changes: 20 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,34 +146,30 @@ 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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get redirections with and without query params. Then I still perform a find on both depending on the ignoreQueryString property.
I don't think we need a regex here, or maybe just to remove the last slash?
BTW using redirectSlug instead of slug here because with the latter it doesn't work when slug is deduplicated, by an archived piece for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying "uncle" here. Using slug for speed is just too messy. So please add a mongodb database index on redirectSlug. See the doc module for examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncle? Ok doing that.


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) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic as before here just condensed and avoid two useless iterations (some in the if statements).

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.redirect(status, target._newPage[0].slug);
Copy link
Contributor Author

@ValJed ValJed Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to take target._newPage[0].slug instead of target._newPage[0]._url because I was ended with redirection to pages like /fr/fr/page since the locale prefix is automatically added.
Didn't see potential issues by using slug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use rawRedirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

} else if (target.urlType === 'external' && target.externalUrl.length) {
return req.res.redirect(status, target.externalUrl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawRedirect here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

return await emitAndRedirectOrNext();
} catch (e) {
self.apos.util.error(e);
Expand Down