Gathering detailed insights and metrics for @github/include-fragment-element
Gathering detailed insights and metrics for @github/include-fragment-element
Gathering detailed insights and metrics for @github/include-fragment-element
Gathering detailed insights and metrics for @github/include-fragment-element
npm install @github/include-fragment-element
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
543 Stars
413 Commits
54 Forks
293 Watching
3 Branches
3,728 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (69.02%)
TypeScript (28.09%)
Dockerfile (2.89%)
Cumulative downloads
Total Downloads
Last day
-14.4%
4,019
Compared to previous day
Last week
-6.2%
27,563
Compared to previous week
Last month
-0.5%
121,301
Compared to previous month
Last year
134.5%
1,403,031
Compared to previous year
A Client Side Includes tag.
$ npm install --save @github/include-fragment-element
All include-fragment
elements must have a src
attribute from which to retrieve an HTML element fragment.
The initial page load should include fallback content to be displayed if the resource could not be fetched immediately.
1import '@github/include-fragment-element'
Original
1<div class="tip"> 2 <include-fragment src="/tips"> 3 <p>Loading tip…</p> 4 </include-fragment> 5</div>
On page load, the include-fragment
element fetches the URL, the response is parsed into an HTML element, which replaces the include-fragment
element entirely.
Result
1<div class="tip"> 2 <p>You look nice today</p> 3</div>
The server must respond with an HTML fragment to replace the include-fragment
element. It should not contain another include-fragment
element or the server will be polled in an infinite loop.
This attribute tells <include-fragment/>
what to send as the Accept
header, as part of the fetch request. If omitted, or if set to an empty value, the default behaviour will be text/html
. It is important that the server responds with HTML, but you may wish to change the accept header to help negotiate the right content with the server.
This indicates when the contents should be fetched:
eager
: Fetches and load the content immediately, regardless of whether or not the <include-fragment/>
is currently within the visible viewport (this is the default value).lazy
: Defers fetching and loading the content until the <include-fragment/>
tag reaches a calculated distance from the viewport. The intent is to avoid the network and storage bandwidth needed to handle the content until it's reasonably certain that it will be needed.If the URL fails to load, the include-fragment
element is left in the page and tagged with an is-error
CSS class that can be used for styling.
Request lifecycle events are dispatched on the <include-fragment>
element.
loadstart
- The server fetch has started.load
- The request completed successfully.error
- The request failed.loadend
- The request has completed.include-fragment-replace
(cancelable) - The success response has been parsed. It comes with event.detail.fragment
that will replace the current element.include-fragment-replaced
- The element has been replaced by the fragment.1const loader = document.querySelector('include-fragment') 2const container = loader.parentElement 3loader.addEventListener('loadstart', () => container.classList.add('is-loading')) 4loader.addEventListener('loadend', () => container.classList.remove('is-loading')) 5loader.addEventListener('load', () => container.classList.add('is-success')) 6loader.addEventListener('error', () => container.classList.add('is-error'))
Attribute | Options | Description |
---|---|---|
src | URL string | Required URL from which to load the replacement HTML element fragment. |
The request for replacement markup from the server starts when the src
attribute becomes available on the <include-fragment>
element. Most often this will happen at page load when the element is rendered. However, if we omit the src
attribute until some later time, we can defer loading the content at all.
The <details-menu>
element uses this technique to defer loading menu content until the menu is first opened.
Deferring the display of markup is typically done in the following usage patterns.
A user action begins a slow running background job on the server, like backing up files stored on the server. While the backup job is running, a progress bar is shown to the user. When it's complete, the include-fragment element is replaced with a link to the backup files.
The first time a user visits a page that contains a time-consuming piece of markup to generate, a loading indicator is displayed. When the markup is finished building on the server, it's stored in memcache and sent to the browser to replace the include-fragment loader. Subsequent visits to the page render the cached markup directly, without going through a include-fragment element.
You can call setCSPTrustedTypesPolicy(policy: TrustedTypePolicy | Promise<TrustedTypePolicy> | null)
from JavaScript to set a CSP trusted types policy, which can perform (synchronous) filtering or rejection of the fetch
response before it is inserted into the page:
1import IncludeFragmentElement from "include-fragment-element"; 2import DOMPurify from "dompurify"; // Using https://github.com/cure53/DOMPurify 3 4// This policy removes all HTML markup except links. 5const policy = trustedTypes.createPolicy("links-only", { 6 createHTML: (htmlText: string) => { 7 return DOMPurify.sanitize(htmlText, { 8 ALLOWED_TAGS: ["a"], 9 ALLOWED_ATTR: ["href"], 10 RETURN_TRUSTED_TYPE: true, 11 }); 12 }, 13}); 14IncludeFragmentElement.setCSPTrustedTypesPolicy(policy);
The policy has access to the fetch
response object. Due to platform constraints, only synchronous information from the response (in addition to the HTML text body) can be used in the policy:
1import IncludeFragmentElement from "include-fragment-element";
2
3const policy = trustedTypes.createPolicy("require-server-header", {
4 createHTML: (htmlText: string, response: Response) => {
5 if (response.headers.get("X-Server-Sanitized") !== "sanitized=true") {
6 // Note: this will reject the contents, but the error may be caught before it shows in the JS console.
7 throw new Error("Rejecting HTML that was not marked by the server as sanitized.");
8 }
9 return htmlText;
10 },
11});
12IncludeFragmentElement.setCSPTrustedTypesPolicy(policy);
Note that:
IncludeFragmentElement
fetches.setCSPTrustedTypesPolicy()
ahead of any other load of include-fragment-element
in your code.
Promise<TrustedTypePolicy>
.null
to remove the policy.This declarative approach is very similar to SSI or ESI directives. In fact, an edge implementation could replace the markup before its actually delivered to the client.
1<include-fragment src="/github/include-fragment/commit-count" timeout="100"> 2 <p>Counting commits…</p> 3</include-fragment>
A proxy may attempt to fetch and replace the fragment if the request finishes before the timeout. Otherwise the tag is delivered to the client. This library only implements the client side aspect.
Browsers without native custom element support require a polyfill. Legacy browsers require various other polyfills. See examples/index.html
for details.
npm install
npm test
Distributed under the MIT license. See LICENSE for details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
9 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 7
Reason
Found 6/9 approved changesets -- score normalized to 6
Reason
4 existing vulnerabilities detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 3
Details
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More