Skip to content

Latest commit

 

History

History

report-same-referrer

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Send referrer error information to endpoints

npm version Workflow status

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

<script type="importmap">
  {
    "imports": {
      "@w0s/report-same-referrer": "..."
    }
  }
</script>
<script type="module">
  import reportSameReferrer from '@w0s/report-same-referrer';

  await reportSameReferrer('https://report.example.com/referrer', {
    fetchParam: {
      documentURL: 'documentURL',
      referrer: 'referrer',
    },
    fetchContentType: 'application/json',
    fetchHeaders: {
      'X-Requested-With': 'hoge',
    },
    condition: 'origin',
    same: [
      'https://www1.example.com',
      'https://www2.example.com',
    ],
    denyUAs: [
      /Googlebot\/2.1;/,
    ],
  });
</script>

Default function

async (endpoint: string, options: Readonly<Option>): Promise<void>

Parameters

endpoint [required]
URL of the endpoint
options [required]
Information such as transmission conditions

Option

interface Option {
  fetchParam: {
    documentURL: string;
    referrer: string;
  };
  fetchContentType?: 'application/x-www-form-urlencoded' | 'application/json';
  fetchHeaders?: HeadersInit;
  condition?: 'origin' | 'host' | 'hostname';
  same?: string[];
  denyUAs?: RegExp[];
  allowUAs?: RegExp[];
}
fetchParam.documentURL
Field name when sending the URL of the document to an endpoint.
fetchParam.referrer
Field name when sending `document.referrer` to an endpoint.
fetchContentType
Content-Type header to be set in fetch() 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 nor allowUAs is specified, any file name will be accepted.