-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
81 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
import { | ||
getQueryWithoutSlugTypeParameterFromParsedUrlQuery, | ||
getUrlWithoutGetParameters, | ||
} from 'helpers/parsing/urlParsing'; | ||
import { getUrlWithoutGetParameters } from 'helpers/parsing/urlParsing'; | ||
import { getStringWithoutTrailingSlash } from 'helpers/parsing/stringWIthoutSlash'; | ||
import { INTERNAL_QUERY_PARAMETERS } from 'helpers/queryParamNames'; | ||
import { NextRouter } from 'next/router'; | ||
import { | ||
PAGE_QUERY_PARAMETER_NAME, | ||
SEARCH_QUERY_PARAMETER_NAME, | ||
FILTER_QUERY_PARAMETER_NAME, | ||
SORT_QUERY_PARAMETER_NAME, | ||
} from 'helpers/queryParamNames'; | ||
|
||
const DEFAULT_CANONICAL_QUERY_PARAMS = [ | ||
PAGE_QUERY_PARAMETER_NAME, | ||
SEARCH_QUERY_PARAMETER_NAME, | ||
FILTER_QUERY_PARAMETER_NAME, | ||
SORT_QUERY_PARAMETER_NAME, | ||
] as const; | ||
|
||
export type CanonicalQueryParameters = (typeof DEFAULT_CANONICAL_QUERY_PARAMS)[number][]; | ||
|
||
export const generateCanonicalUrl = (router: NextRouter, url: string): string | null => { | ||
export const generateCanonicalUrl = ( | ||
router: NextRouter, | ||
url: string, | ||
canonicalQueryParams?: CanonicalQueryParameters, | ||
): string | null => { | ||
const newQueryOverwrite: Record<string, string> = {}; | ||
const queryWithoutAllParameter = getQueryWithoutSlugTypeParameterFromParsedUrlQuery(router.query); | ||
const queries = router.query; | ||
|
||
for (const queryParam in queryWithoutAllParameter) { | ||
if ((INTERNAL_QUERY_PARAMETERS as string[]).includes(queryParam)) { | ||
const queryParamValue = queryWithoutAllParameter[queryParam]?.toString(); | ||
for (const queryParam in queries) { | ||
if ((canonicalQueryParams || DEFAULT_CANONICAL_QUERY_PARAMS).includes(queryParam as any)) { | ||
const queryParamValue = queries[queryParam]?.toString(); | ||
|
||
if (queryParamValue !== undefined) { | ||
if (queryParamValue) { | ||
newQueryOverwrite[queryParam] = queryParamValue; | ||
} | ||
} | ||
} | ||
|
||
if (JSON.stringify(newQueryOverwrite) === JSON.stringify(queryWithoutAllParameter)) { | ||
if (JSON.stringify(newQueryOverwrite) === JSON.stringify(queries)) { | ||
return null; | ||
} | ||
|
||
const queryParams = new URLSearchParams(newQueryOverwrite).toString(); | ||
const canonicalUrl = `${getStringWithoutTrailingSlash(url)}${getUrlWithoutGetParameters(router.asPath)}`; | ||
|
||
if (queryParams.length === 0) { | ||
return `${getStringWithoutTrailingSlash(url)}${getUrlWithoutGetParameters(router.asPath)}`; | ||
if (!queryParams.length) { | ||
return canonicalUrl; | ||
} | ||
|
||
return `${getStringWithoutTrailingSlash(url)}${getUrlWithoutGetParameters(router.asPath)}?${queryParams}`; | ||
return `${canonicalUrl}?${queryParams}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters