From 2450ec3a87926d5ddbebbb7e2257e1ab901122d5 Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Sat, 29 Oct 2022 23:14:43 +0200 Subject: [PATCH] fix: don't rewrite production urls fixes #165 --- src/steps/utils.js | 24 +----------------------- test/steps/utils.test.js | 20 +------------------- 2 files changed, 2 insertions(+), 42 deletions(-) diff --git a/src/steps/utils.js b/src/steps/utils.js index a59db148..ac8285e6 100644 --- a/src/steps/utils.js +++ b/src/steps/utils.js @@ -175,19 +175,6 @@ export function getAbsoluteUrl(state, url) { return resolveUrl(`https://${state.config.host}/`, url); } -/** - * Checks if the given `str` matches any of the given regs or if `regs` is empty. - * @param {RegExp[]} regs - * @param {string} str - * @returns {boolean} {@code true} if `regs` is empty or if `str` matches any of them. - */ -function matchAny(regs, str) { - if (!regs || regs.length === 0) { - return true; - } - return regs.findIndex((r) => r.test(str)) >= 0; -} - /** * Rewrites the media, helix or external url. Returns the original if not rewritten. * @param {PipelineState} state @@ -198,9 +185,7 @@ export function rewriteUrl(state, url) { if (!url || !url.startsWith('https://')) { return url; } - const { - host, pathname, search, hash, - } = new URL(url); + const { pathname, search, hash } = new URL(url); if (AZURE_BLOB_REGEXP.test(url)) { const filename = pathname.split('/').pop(); @@ -221,12 +206,5 @@ export function rewriteUrl(state, url) { return `${pathname}${search}${hash}`; } - if (host === state.config?.host && matchAny(state.config.routes, pathname)) { - if (hash && pathname === state.info?.path) { - return hash; - } - return `${pathname}${search}${hash}`; - } - return url; } diff --git a/test/steps/utils.test.js b/test/steps/utils.test.js index ac4d7686..53b7751c 100644 --- a/test/steps/utils.test.js +++ b/test/steps/utils.test.js @@ -152,32 +152,14 @@ describe('Rewrite URLs test', () => { }, 'https://main--pages--adobe.hlx3.page/blog/article?a=42#heading'), '#heading'); }); - it('replaces prod url', () => { + it('does not replace prod url', () => { const state = { config: { host: 'www.adobe.com', }, info: { path: '/blog/article', search: '' }, }; - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog/article'), '/blog/article'); - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog#home'), '/blog#home'); - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog?a=42#home'), '/blog?a=42#home'); - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog/article#heading'), '#heading'); - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog/article?a=42#heading'), '#heading'); - }); - - it('replaces prod url and respects routes', () => { - const state = { - config: { - host: 'www.adobe.com', - routes: [ - /.*\/express\/.*/, - ], - }, - info: { path: '/blog/article', search: '' }, - }; assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/blog/article'), 'https://www.adobe.com/blog/article'); - assert.strictEqual(rewriteUrl(state, 'https://www.adobe.com/express/blog'), '/express/blog'); }); });