Skip to content

Commit

Permalink
feat(schema): type speculationrules API
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 4, 2022
1 parent f275ab6 commit 2163e63
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/schema/src/script.ts
@@ -1,3 +1,4 @@
import type { ReferrerPolicy } from '@zhead/schema/src/shared'
import type { Booleanable, MaybePromiseProps } from './utils'
import type { HttpEventAttributes } from './attributes'

Expand Down Expand Up @@ -66,15 +67,7 @@ export interface ScriptBase {
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-referrerpolicy
*/
referrerpolicy?: '' |
'no-referrer' |
'no-referrer-when-downgrade' |
'origin' |
'origin-when-cross-origin' |
'same-origin' |
'strict-origin' |
'strict-origin-when-cross-origin' |
'unsafe-url'
referrerpolicy?: ReferrerPolicy
/**
* This attribute specifies the URI of an external script;
* this can be used as an alternative to embedding a script directly within a document.
Expand All @@ -92,7 +85,8 @@ export interface ScriptBase {
'module' |
'application/json' |
'application/ld+json' |
string
'speculationrules' |
(string & Record<never, never>)
/**
* This attribute defines the unique ID.
*/
Expand Down
52 changes: 52 additions & 0 deletions packages/schema/src/struct/speculationRules.ts
@@ -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
}
112 changes: 112 additions & 0 deletions test/schema/speculationRules.test.ts
@@ -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'],
},
],
}
})
})

0 comments on commit 2163e63

Please sign in to comment.