🔗 get-canonical-url

Determines the current page's canonical URL and optionally normalizes it via normalize-url for consistency.
Install
npm install get-canonical-url
yarn add get-canonical-url
Or pull directly from unpkg in-browser:
<script src="https://unpkg.com/get-canonical-url/dist/get-canonical-url.min.js"></script>
Usage
With <link rel="canonical">
<!doctype html>
<html>
<head>
<link rel="canonical" href="https://www.example.com/this/doesnt/exist.aspx?no=really&it=doesnt#gocheck">
</head>
</html>
import canonicalUrl from "get-canonical-url";
canonicalUrl();
canonicalUrl({
normalize: true,
normalizeOptions: {
stripProtocol: true,
stripWWW: true,
stripHash: true
}
})
Without <link rel="canonical">
import canonicalUrl from "get-canonical-url";
canonicalUrl({
guess: true,
normalize: true
});
API
canonicalUrl(options?)
options
Type: object
normalize
Type: boolean
Default: false
Clean-up and normalize the determined canonical URL.
normalizeOptions
Type: NormalizeOptions
Default:
{
stripWWW: false,
stripHash: true,
removeQueryParameters: true,
removeTrailingSlash: false
}
Options passed directly to normalize-url
.
Requires options.normalize = true
.
guess
Type: boolean
Default: false
Make an educated guess using other clues if canonical isn't explicitly set in the page's <head>
.
License
MIT