-
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
Changes from 6 commits
8b47007
67aefd9
4743bf6
d7bbd62
5015788
43626ca
07b340a
94d01b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,35 @@ 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 } ] }) | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. You think it's less efficient than a regex query? |
||
.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].slug); | ||
} 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 +216,9 @@ module.exports = { | |
} | ||
} | ||
}); | ||
}, | ||
createIndexes() { | ||
self.apos.doc.db.createIndex({ redirectSlug: 1 }); | ||
} | ||
}; | ||
} | ||
|
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.
locale prefixes