Skip to content

Commit 11af301

Browse files
committed
fix: drop sameOrigin but move to same origin default;
- update tests & readme - 726 gz / 592 br
1 parent d2a7870 commit 11af301

4 files changed

Lines changed: 7 additions & 18 deletions

File tree

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ The above options are best for multi-page sites. Single-page apps have a few opt
8080
* `timeout`: Integer for the `requestIdleCallback` timeout. A time in milliseconds by which the browser must execute prefetching. Defaults to 2 seconds.
8181
* `timeoutFn`: Function for specifying a timeout. Defaults to `requestIdleCallback`. Can also be swapped out for a custom function like [networkIdleCallback](https://github.com/pastelsky/network-idle-callback) (see demos)
8282
* `priority`: Boolean specifying preferred priority for fetches. Defaults to `false`. `true` will attempt to use the `fetch()` API where supported (rather than rel=prefetch)
83-
* `sameOrigin`: Restricts prefetching to URLs of the same origin. Defaults to `false`. Any truthy value will ensure no cross-domain requests are sent & will override/ignore any `options.origins` value.
84-
* `origins`: Static array of URL hostname strings that are allowed to be prefetched. Defaults to an empty array, which _allows all_ URLs to be prefetched.
83+
* `origins`: Static array of URL hostname strings that are allowed to be prefetched. Defaults to the same domain origin, which prevents _any_ cross-origin requests.
8584

8685
TODO:
8786
* Explore detecting file-extension of resources and using [rel=preload](https://w3c.github.io/preload/) for high priority fetches
@@ -141,17 +140,9 @@ Defaults to low-priority (`rel=prefetch` or XHR). For high-priority (`priority:
141140
quicklink({ priority: true });
142141
```
143142

144-
**Allow same-origin requests only**
145-
146-
Forcibly disables all cross-domain requests. All domains are allowed by default.
147-
148-
```js
149-
quicklink({ sameOrigin: true });
150-
```
151-
152143
**Specify a custom list of allowed origins**
153144

154-
Provide a list of hostnames that should be prefetch-able. All domains are allowed by default.
145+
Provide a list of hostnames that should be prefetch-able. Only the same origin is allowed by default.
155146

156147
> **Important:** You must also include your own hostname!
157148

src/index.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function prefetcher(url) {
4848
* @param {Array} options.urls - Array of URLs to prefetch (override)
4949
* @param {Object} options.el - DOM element to prefetch in-viewport links of
5050
* @param {Boolean} options.priority - Attempt higher priority fetch (low or high)
51-
* @param {Boolean} options.sameOrigin - If truthy, restricts prefetching to assets with same origin.
5251
* @param {Array} options.origins - Allowed origins to prefetch (empty allows all)
5352
* @param {Number} options.timeout - Timeout after which prefetching will occur
5453
* @param {Function} options.timeoutFn - Custom timeout function
@@ -63,7 +62,7 @@ export default function (options) {
6362

6463
observer.priority = options.priority;
6564

66-
const allowed = options.sameOrigin ? [location.hostname] : options.origins || [];
65+
const allowed = options.origins || [location.hostname];
6766

6867
options.timeoutFn(() => {
6968
// If URLs are given, prefetch them.

test/quicklink.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe('quicklink tests', function () {
9696
expect(responseURLs).to.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
9797
});
9898

99-
it('should only prefetch links of same origin', async function () {
99+
100+
it('should only prefetch links of same origin (default)', async function () {
100101
const responseURLs = [];
101102
page.on('response', resp => {
102103
responseURLs.push(resp.url());
@@ -105,7 +106,7 @@ describe('quicklink tests', function () {
105106

106107
await page.waitFor(1000);
107108
expect(responseURLs).to.be.an('array');
108-
//=> sameOrigin: true
109+
//=> origins: [location.hostname] (default)
109110
expect(responseURLs).to.include(`${server}/2.html`);
110111
expect(responseURLs).to.not.include('https://example.com/1.html');
111112
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');

test/test-same-origin.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
1919
<script src="../dist/quicklink.umd.js"></script>
2020
<script>
21-
quicklink({
22-
sameOrigin: true
23-
});
21+
quicklink();
2422
</script>
2523
</body>
2624
</html>

0 commit comments

Comments
 (0)