Skip to content

Commit

Permalink
Support rel and referrer policy attributes (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike authored Oct 16, 2021
1 parent 89f7a96 commit c747e48
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/utils/DefaultTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ export default class DefaultTrigger {
return enable !== false && (!exclude || !element.matches(exclude));
}

/**
* Load a resource with element attribute support.
* @see [Follow the hyperlink | HTML Standard]{@link https://html.spec.whatwg.org/multipage/links.html#following-hyperlinks-2}
* @see [Plan to navigate | HTML Standard]{@link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#plan-to-navigate}
*/
load(
resource: RequestInfo,
subject: Link | HTMLFormElement,
): void {
/**
* The RequestInit to align the request to send by the element.
*/
const requestInit: RequestInit = {};

/**
* Referrer policy that specified on the element.
* Will cause a TypeError in the later Request constructing step if the attribute is invalid.
* Not bypassing forms here as it is supposed to be supported in the future.
* @see [Add referrerpolicy to <form> · whatwg/html]{@link https://github.com/whatwg/html/issues/4320}
*/
const referrerPolicy = subject.getAttribute('referrerpolicy')?.toLowerCase();
if (referrerPolicy !== undefined) requestInit.referrerPolicy = referrerPolicy as ReferrerPolicy;

/**
* Use no referrer if specified in the link types.
* Not reading from `.relList` here as browsers haven't shipped it for forms yet.
* @see [Add <form rel> initial compat data · mdn/browser-compat-data]{@link https://github.com/mdn/browser-compat-data/pull/9130}
*/
if (subject.getAttribute('rel')?.split(/\s+/)
.some((type) => type.toLowerCase() === 'noreferrer')) {
requestInit.referrer = '';
}

this.pjax.load(new Request(resource, requestInit)).catch(() => {});
}

onLinkOpen(event: Event): void {
if (event.defaultPrevented) return;

Expand All @@ -61,7 +97,7 @@ export default class DefaultTrigger {

event.preventDefault();

this.pjax.load(link.href).catch(() => {});
this.load(link.href, link);
}

onFormSubmit(event: SubmitEvent): void {
Expand All @@ -85,7 +121,8 @@ export default class DefaultTrigger {
if (url.origin !== window.location.origin) return;

event.preventDefault();
this.pjax.load(requestInfo).catch(() => {});

this.load(requestInfo, form);
}

register(): void {
Expand Down

0 comments on commit c747e48

Please sign in to comment.