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

Conversation

ValJed
Copy link
Contributor

@ValJed ValJed commented Jul 24, 2023

PRO-4495
PRO-4493

Summary

  • Modifies the middleware to check the originalUrl instead of url in which the locale prefix has been removed.
  • Removes the regex and performs a $or request on redirect pieces redirectSlug property because there was issues when using the slug since this one can be modified by the server for de duplication for example.
  • When redirecting to internal pages, use the page piece slug property instead of the _url of the new page to avoid being redirected on pages like /fr/fr/page.

What are the specific steps to test this change?

On project with multiple locales using prefixes, when creating a new redirection, you must specify the locale prefix, it allows to avoid conflicts with potential pieces with the same slug in other locales.

What kind of change does this PR introduce?

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

@linear
Copy link

linear bot commented Jul 24, 2023

PRO-4493 Redirect/Localization Issues: Prefixes are removed before consulting redirects

When we are using the redirect module on a site on which we have locale prefixes, it does not work as intended. When you want to redirect a French page like /fr/auto, you cannot because if /auto exists in the EN locale, it will remove the prefix before consulting the redirects.

https://gitlab-dcadcx.michelin.net/pa/apostrophe-enhancements/-/issues/1223

@ValJed ValJed self-assigned this Jul 24, 2023
@ValJed ValJed marked this pull request as draft July 24, 2023 16:01
index.js Outdated
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

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).

index.js Outdated
}
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.

@ValJed ValJed requested a review from boutell July 27, 2023 16:16
@ValJed ValJed marked this pull request as ready for review July 27, 2023 16:20
index.js Outdated
}
const slug = req.originalUrl;
const [ pathOnly ] = slug.split('?');
const results = await self.find(req, { $or: [ { redirectSlug: slug }, { redirectSlug: pathOnly } ] }).toArray();
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.

index.js Outdated
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
Member

Choose a reason for hiding this comment

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

Use rawRedirect.

index.js Outdated
if (target.urlType === 'internal' && target._newPage && target._newPage[0]) {
return req.res.redirect(status, target._newPage[0].slug);
} 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

@boutell
Copy link
Member

boutell commented Aug 3, 2023

Reminder that this request for changes is pending.

@ValJed ValJed requested a review from boutell August 14, 2023 09:05
CHANGELOG.md Outdated
@@ -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

const slug = req.originalUrl;
const [ pathOnly ] = slug.split('?');
const results = await self
.find(req, { $or: [ { redirectSlug: slug }, { redirectSlug: pathOnly } ] })
Copy link
Member

Choose a reason for hiding this comment

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

This may be a slight performance hit, but OK. Time to make this code simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You think it's less efficient than a regex query?
I added an index on redirectSlug also.

@ValJed ValJed requested a review from boutell August 16, 2023 12:38
@ValJed ValJed merged commit 7a09dfb into main Aug 16, 2023
@ValJed ValJed deleted the pro-4493-fix-locale-with-prefix branch August 16, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants