Commit 0341d84e authored by Marcus Thelin's avatar Marcus Thelin
Browse files

Handle external links

parent dc6d6e07
{
"typeAcquisition": {
"include": ["jest"]
}
}
This diff is collapsed.
{
"name": "@24hr/redirect-matcher",
"version": "1.0.5",
"version": "1.0.6",
"description": "Match URL with redirects from WP",
"main": "src/index.js",
"scripts": {
"prepublishOnly": "cp ../README.md .",
"postpublish": "rm -f ./README.md"
"prepublishOnly": "npm test && cp ../README.md .",
"postpublish": "rm -f ./README.md",
"test": "jest",
"test:watch": "jest --watch"
},
"author": "Marcus Thelin <marcus.thelin@24hr.se>",
"license": "ISC",
"repository": "https://gitlab.24hr.se/marcus/wordpress-redirect-options-plugins",
"dependencies": {
"jest": "28.1.0",
"matcher": "^4.0.0"
},
"devDependencies": {
"@types/jest": "27.5.0"
}
}
const { redirectMatcher } = require('../');
const redirects = require('./redirects.json');
describe('Redirect matching', () => {
it('Handles external URLs', () => {
const redirectData = redirectMatcher(redirects, '/utskick');
expect(redirectData.url).toBe('https://rbappprod.page.link/29hQ');
});
it('Returns correct internal redirect', () => {
const redirectData = redirectMatcher(redirects, '/om-oss-foretag/press-media-foretag/');
expect(redirectData.url).toBe('/om-oss/press-media');
expect(redirectData.code).toBe(301);
})
});
[
{
"from": "/om-oss-foretag/",
"to": "/om-oss/",
"code": "301"
},
{
"from": "/om-oss-foretag/press-media-foretag/",
"to": "/om-oss/press-media/",
"code": "301"
},
{
"from": "/om-oss-foretag/press-media-foretag/pressmeddelanden-foretag",
"to": "/om-oss/press-media/pressmeddelanden",
"code": "301"
},
{
"from": "/om-oss-foretag/press-media-foretag/bildbank-foretag",
"to": "/om-oss/press-media/bildbank",
"code": "301"
},
{
"from": "/om-oss-foretag/press-media-foretag/prenumerera-foretag",
"to": "/om-oss/press-media/prenumerera",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag",
"to": "/om-oss/bolagsinformation",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/bolagsstyrning-foretag",
"to": "/om-oss/bolagsinformation/bolagsstyrning",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/styrelsen-foretag",
"to": "/om-oss/bolagsinformation/styrelsen",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/kontrollfunktioner-foretag",
"to": "/om-oss/bolagsinformation/kontrollfunktioner",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/finansiell-information-foretag",
"to": "/om-oss/bolagsinformation/finansiell-information",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/finansiell-information-foretag/upplaningsprogram-mtn-foretag",
"to": "/om-oss/bolagsinformation/finansiell-information/upplaningsprogram-mtn",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/finansiell-information-foretag/tidigare-finansiell-information-foretag",
"to": "/om-oss/bolagsinformation/finansiell-information/tidigare-finansiell-information",
"code": "301"
},
{
"from": "/om-oss-foretag/bolagsinformation-foretag/forsakringsformedling-foretag",
"to": "/om-oss/bolagsinformation/forsakringsformedling",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag",
"to": "/om-oss/integritet-och-sakerhet",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/open-banking-foretag",
"to": "/om-oss/integritet-och-sakerhet/open-banking",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/open-banking-foretag/api-statistics-foretag",
"to": "/om-oss/integritet-och-sakerhet/open-banking/api-statistics",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/gdpr-som-kund-foretag",
"to": "/om-oss/integritet-och-sakerhet/gdpr-som-kund",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/synpunkter-och-klagomal-foretag",
"to": "/om-oss/integritet-och-sakerhet/synpunkter-och-klagomal",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/gdpr-som-partner-foretag",
"to": "/om-oss/integritet-och-sakerhet/gdpr-som-partner",
"code": "301"
},
{
"from": "/om-oss-foretag/integritet-och-sakerhet-foretag/psd2-foretag",
"to": "/om-oss/integritet-och-sakerhet/psd2",
"code": "301"
},
{
"from": "/om-oss-foretag/vart-engagemang-foretag",
"to": "/om-oss/vart-engagemang",
"code": "301"
},
{
"from": "/om-oss-foretag/jobba-hos-oss-foretag",
"to": "/om-oss/jobba-hos-oss",
"code": "301"
},
{
"from": "/foretag/foretagsfinansiering/sparkonto-foretag",
"to": "/foretag/sparkonto-foretag",
"code": "301"
},
{
"from": "/foretag/foretagsfinansiering/sparkonto-foretag/oppna-sparkonto-foretag",
"to": "/foretag/sparkonto-foretag/oppna-sparkonto-foretag",
"code": "301"
},
{
"from": "/foretag/foretagsfinansiering/sparkonto-foretag/insattningsgaranti-foretag",
"to": "/foretag/sparkonto-foretag/insattningsgaranti-foretag",
"code": "301"
},
{
"from": "/foretag/foretagsfinansiering-ny/factoring",
"to": "/foretag/foretagsfinansiering/factoring",
"code": "301"
},
{
"from": "/foretag/foretagsfinansiering-ny/foretagslan",
"to": "/foretag/foretagsfinansiering/foretagslan",
"code": "301"
},
{
"from": "/foretag/factoring/",
"to": "/foretag/foretagsfinansiering/factoring",
"code": "301"
},
{
"from": "/betala/supreme-card/",
"to": "/betala/kreditkort",
"code": "301"
},
{
"from": "/supreme-card/classic/",
"to": "/betala/kreditkort/classic/",
"code": "301"
},
{
"from": "/betala/supreme-card/gold",
"to": "/betala/kreditkort/gold/",
"code": "301"
},
{
"from": "/betala/supreme-card/woman",
"to": "/betala/kreditkort/woman/",
"code": "301"
},
{
"from": "/betala/supreme-card/world",
"to": "/betala/kreditkort/world/",
"code": "301"
},
{
"from": "/betala/supreme-card/jamfor-korten",
"to": "/betala/kreditkort/jamfor-korten/",
"code": "301"
},
{
"from": "/betala/supreme-card/befintlig-kund",
"to": "/betala/kreditkort/befintlig-kund/",
"code": "301"
},
{
"from": "/betala/supreme-card/formaner",
"to": "/betala/kreditkort/befintlig-kund/",
"code": "301"
},
{
"from": "/betala/supreme-card/kampanjer ",
"to": "/betala/kreditkort/",
"code": "301"
},
{
"from": "/betala/supreme-card/ansok-om-supreme-card/",
"to": "/betala/kreditkort/ansok-om-kreditkort/",
"code": "301"
},
{
"from": "/foretag/betallosningar/ta-betalt-online",
"to": "/foretag/betallosningar/betallosningar-for-e-handel",
"code": "301"
},
{
"from": "/foretag/betallosningar/ta-betalt-i-butik",
"to": "/foretag/betallosningar/betallosningar-for-butik",
"code": "301"
},
{
"from": "/foretag/betallosningar/integrationspartners",
"to": "/foretag/betallosningar/integrationer",
"code": "301"
},
{
"from": "/foretag/betallosningar/vara-butikscoacher",
"to": "/foretag/betallosningar",
"code": "301"
},
{
"from": "/foretag/betallosningar/omnikanal",
"to": "/foretag/betallosningar",
"code": "301"
},
{
"from": "/foretag/factoring/fakturabelaning",
"to": "/foretag/foretagsfinansiering/factoring",
"code": "301"
},
{
"from": "/foretag/factoring/fakturakop",
"to": "/foretag/foretagsfinansiering/factoring",
"code": "301"
},
{
"from": "/kundservice/vanliga-fragor",
"to": "/kundservice",
"code": "301"
},
{
"from": "/supremecard4u/",
"to": "/betala/kreditkort/",
"code": "301"
},
{
"from": "/cluster_page/tandlakarkonto",
"to": "/cluster_page/tandkonto/",
"code": "301"
},
{
"from": "/betala/loyo-pay",
"to": "",
"code": "301"
},
{
"from": "/betala/supreme-card/supreme-card-poangshop/",
"to": "/betala/kreditkort/befintlig-kund/",
"code": "301"
},
{
"from": "/betala/supreme-card/delbetala/",
"to": "/betala/kreditkort/",
"code": "301"
},
{
"from": "/betala/supreme-card/betalforsakring-supreme-card/",
"to": "/betala/kreditkort/befintlig-kund/",
"code": "301"
},
{
"from": "/kundservice/dokument-och-blanketter",
"to": "/dokument-och-blanketter",
"code": "301"
},
{
"from": "/betala/kreditkort/woman/",
"to": "/betala/kreditkort/gold/",
"code": "301"
},
{
"from": "/utskick",
"to": "https://rbappprod.page.link/29hQ",
"code": "301"
},
{
"from": "/resurs-insights/res-billigt-men-lyxigt",
"to": "/resursbanken_post/res-billigt-15-tips-for-smarta-och-prisvarda-resor/",
"code": "301"
}
]
const matcher = require('matcher');
const url = require('url');
const queryString = require('query-string');
const removeTrailingSlash = str => str.replace(/\/$/, '');
/**
......@@ -40,11 +41,9 @@ function redirectMatcher(redirects, requestPath) {
if (!matchedRedirect) {
return null;
}
const { pathname, search, hash } = url.parse(matchedRedirect.to);
const to = removeTrailingSlash(matchedRedirect.to);
return {
url: `${removeTrailingSlash(pathname)}${search || ''}${hash || ''}`,
url: `${to}`,
code: Number(matchedRedirect.code),
headers: {
'Cache-Control': 'no-store, max-age=0'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment