Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rel and referrer policy attributes #213

Merged
merged 4 commits into from
Oct 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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