Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema): type speculationrules API
- Loading branch information
Showing
3 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { ReferrerPolicy } from '../shared' | ||
|
||
export interface SpeculationRules { | ||
prefetch?: (SpeculationRuleList | SpeculationRuleDocument)[] | ||
prerender?: (SpeculationRuleList | SpeculationRuleDocument)[] | ||
} | ||
|
||
export interface SpeculationRuleBase { | ||
/** | ||
* A hint about how likely the user is to navigate to the URL | ||
* | ||
* @see https://github.com/WICG/nav-speculation/blob/main/triggers.md#scores | ||
*/ | ||
score?: number | ||
/** | ||
* Parse urls/patterns relative to the document's base url. | ||
* | ||
* @see https://github.com/WICG/nav-speculation/blob/main/triggers.md#using-the-documents-base-url-for-external-speculation-rule-sets | ||
*/ | ||
relative_to?: 'document' | ||
/** | ||
* Assertions in the rule about the capabilities of the user agent while executing them. | ||
* | ||
* @see https://github.com/WICG/nav-speculation/blob/main/triggers.md#requirements | ||
*/ | ||
requires?: 'anonymous-client-ip-when-cross-origin'[] | ||
/** | ||
* Indicating where the page expects the prerendered content to be activated. | ||
* | ||
* @see https://github.com/WICG/nav-speculation/blob/main/triggers.md#window-name-targeting-hints | ||
*/ | ||
target_hint?: '_blank' | '_self' | '_parent' | '_top' | ||
/** | ||
* The policy to use for the speculative request. | ||
* | ||
* @see https://github.com/WICG/nav-speculation/blob/main/triggers.md#explicit-referrer-policy | ||
*/ | ||
'referrer_policy'?: ReferrerPolicy | ||
} | ||
|
||
export interface SpeculationRuleList extends SpeculationRuleBase { | ||
source: 'list' | ||
urls: string[] | ||
} | ||
|
||
export type SpeculationRuleFn = 'and' | 'or' | 'href_matches' | 'selector_matches' | 'not' | ||
export type SpeculationRuleWhere = Partial<Record<SpeculationRuleFn, Partial<(Record<SpeculationRuleFn, (Partial<Record<SpeculationRuleFn, string>>) | string>)>[]>> | ||
|
||
export interface SpeculationRuleDocument extends SpeculationRuleBase { | ||
source: 'document' | ||
where: SpeculationRuleWhere | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { describe, it } from 'vitest' | ||
import type { SpeculationRules } from '@zhead/schema/src/struct/speculationRules' | ||
|
||
describe('speculationRules', () => { | ||
it('types', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const a: SpeculationRules = { | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: ['/my-url'], | ||
requires: ['anonymous-client-ip-when-cross-origin'], | ||
}, | ||
], | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const b: SpeculationRules = { | ||
prerender: [ | ||
{ | ||
source: 'list', | ||
target_hint: '_self', | ||
urls: ['page.html'], | ||
}, | ||
{ | ||
source: 'list', | ||
target_hint: '_blank', | ||
urls: ['page.html'], | ||
}], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const c: SpeculationRules = { | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: ['https://en.wikipedia.org/wiki/Lethe'], | ||
referrer_policy: 'no-referrer', | ||
}, | ||
], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const d: SpeculationRules = { | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: ['/item?id=32480009'], | ||
score: 0.8, | ||
}, | ||
{ | ||
source: 'list', | ||
urls: [ | ||
'https://support.signal.org/hc/en-us/articles/4850133017242', | ||
'https://discord.com/blog/how-discord-supercharges-network-disks-for-extreme-low-latency', | ||
'https://github.com/containers/krunvm', | ||
], | ||
score: 0.5, | ||
}, | ||
], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const e: SpeculationRules = { | ||
prerender: [ | ||
{ | ||
source: 'document', | ||
where: { | ||
and: [ | ||
{ href_matches: '/*\\?*' }, | ||
{ not: { href_matches: '/logout\\?*' } }, | ||
{ not: { selector_matches: '.no-prerender' } }, | ||
], | ||
}, | ||
score: 0.1, | ||
}, | ||
], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const f: SpeculationRules = { | ||
prerender: [ | ||
{ | ||
source: 'list', | ||
urls: ['/home', '/about'], | ||
}, | ||
], | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: ['https://en.wikipedia.org/wiki/Hamster_racing'], | ||
requires: ['anonymous-client-ip-when-cross-origin'], | ||
}, | ||
], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const g: SpeculationRules = { | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: [ | ||
'/item?id=32480009', | ||
'https://support.signal.org/hc/en-us/articles/4850133017242', | ||
'https://discord.com/blog/how-discord-supercharges-network-disks-for-extreme-low-latency', | ||
'https://github.com/containers/krunvm', | ||
], | ||
requires: ['anonymous-client-ip-when-cross-origin'], | ||
}, | ||
], | ||
} | ||
}) | ||
}) |