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

fix: don't rewrite production urls #175

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions src/steps/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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;
}
20 changes: 1 addition & 19 deletions test/steps/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down