Send referrer error information to endpoints
If there are referrers from same site, that information will be sent to the endpoint as an error.
As a practical use case, this script put this script in error pages like 403, 404, 410 error pages to detect the existence of broken links in the same site.
Demo
Examples
import ReportSameReferrer from '@saekitominaga/report-same-referrer';
const reportSameReferrer = new ReportSameReferrer('https://report.example.com/referrer', {
fetchParam: {
location: 'loc',
referrer: 'ref',
},
fetchContentType: 'application/json',
fetchHeaders: {
'X-Requested-With': 'hoge',
},
condition: 'origin',
same: [
'https://www1.example.com',
'https://www2.example.com',
],
denyUAs: [
/Googlebot\/2.1;/,
],
});
await reportSameReferrer.report();
Constructor
new ReportSameReferrer(endpoint: string, options?: Option)
Parameters
-
endpoint
[required] - URL of the endpoint
-
options
[optional] - Information such as transmission conditions
Option
interface Option {
fetchParam?: {
location: string;
referrer: string;
};
fetchContentType?: 'application/x-www-form-urlencoded' | 'application/json';
fetchHeaders?: HeadersInit;
condition?: 'origin' | 'host' | 'hostname';
same?: string[];
denyUAs?: RegExp[];
allowUAs?: RegExp[];
}
fetchParam.location
- Field name when sending `location` to an endpoint. The default value when omitted is `location`. (e.g.
location=https%3A%2F%2Fexample.com%2Fpath%2Fto&referrer=(omit)
) fetchParam.referrer
- Field name when sending `document.referrer` to an endpoint. The default value when omitted is `referrer`. (e.g.
location=(omit)&referrer=https%3A%2F%2Fexternal.example.net%2Fpath%2Fto
) fetchContentType
-
Content-Type
header to be set infetch()
request. fetchHeaders
- Header to add to the
fetch()
request. Specify the HeadersInit type. condition
- Which parts of the referrer to check. Has the same meaning as the URL interface properties. The default value when omitted is
origin
. same
- Domain information treated as the same site. Specify the format according to the value of
condition
.-
condition: origin
→ 'https://www1.example.com' -
condition: host
→ 'www1.example.com:999' -
condition: hostname
→ 'www1.example.com'
-
denyUAs
- If a user agent matches this regular expression, do not send report.
allowUAs
- If a user agent matches this regular expression, send report. If neither
denyUAs
norallowUAs
is specified, any file name will be accepted.