-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b47007
improving code, checking originalUrl (with locale prefix) and redirec…
ValJed 67aefd9
adds changelog
ValJed 4743bf6
redirects to slug
ValJed d7bbd62
finds on redirectSlug
ValJed 5015788
makes $or request on slug and pathOnly
ValJed 43626ca
uses rawRedirect, creates indexes on redirectSlug
ValJed 07b340a
fixes typo
ValJed 94d01b1
uses _url with rawRedirect for internal redirects
ValJed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ module.exports = { | |
}, | ||
init(self) { | ||
self.addUnlocalizedMigration(); | ||
self.createIndexes(); | ||
}, | ||
handlers(self) { | ||
return { | ||
|
@@ -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) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same logic as before here just condensed and avoid two useless iterations ( |
||
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); | ||
|
@@ -214,6 +215,9 @@ module.exports = { | |
} | ||
} | ||
}); | ||
}, | ||
createIndexes() { | ||
self.apos.doc.db.createIndex({ redirectSlug: 1 }); | ||
} | ||
}; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.