Skip to content

Commit

Permalink
Fix handling search and hash in requests
Browse files Browse the repository at this point in the history
- Fix: Search or hash in a requested URL cause redirect loop even if
    though domain and pathname are correct.
- Fix: Search isn't kept when redirecting to a corrected URL.
  • Loading branch information
rinart73 committed Jul 9, 2023
1 parent 5856cac commit 9637cea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,24 @@ describe('checkPage', () => {
expect(checkPage(LP, 'ru')).toMatchObject([PageCheckResult.NotLocalized]);
expect(consoleError).toHaveBeenCalledTimes(0);
});
it('handles URLs with search', () => {
initDefault();
localizeParam(1, 'l_about', {
en: 'about-us',
ru: 'o-nas'
});
let LP = localizePage(
new URL('http://foo.com/ru/o-nas?foo=bar'),
'/[[lang=lang]]/[l_about]'
);
expect(checkPage(LP, 'ru')).toMatchObject([PageCheckResult.Success]);
LP = localizePage(new URL('http://foo.com/ru/about?foo=bar'), '/[[lang=lang]]/[l_about]');
expect(checkPage(LP, 'ru')).toMatchObject([
PageCheckResult.Corrected,
'http://foo.com/ru/o-nas?foo=bar'
]);
expect(consoleError).toHaveBeenCalledTimes(0);
});
});

describe('validatePage', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,18 +584,23 @@ export function checkPage(
page: LocalizedPage,
locale: string
): [PageCheckResult.Corrected, string] | [PageCheckResult] {
const correctPath = href(page, locale, {
const correctHref = href(page, locale, {
base: page._url.pathname,
params: getPageParams(page, true)
});
if (!correctPath) {

if (!correctHref) {
// not available for this locale
page._isNotAvailableForLocale = true;
return [PageCheckResult.NotLocalized];
}
if (correctPath !== page._url.href) {
return [PageCheckResult.Corrected, correctPath];

const correctUrl = new URL(correctHref);
if (correctUrl.host !== page._url.host || correctUrl.pathname !== page._url.pathname) {
correctUrl.search = page._url.search;
return [PageCheckResult.Corrected, correctUrl.href];
}

return [PageCheckResult.Success];
}

Expand Down

0 comments on commit 9637cea

Please sign in to comment.