Skip to content

Commit

Permalink
refactor(script)!: remove early connections
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 23, 2024
1 parent 335f419 commit 0ebacad
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
11 changes: 2 additions & 9 deletions packages/schema/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ export type ScriptInstance<T> = {
remove: () => boolean
} & Promise<T>

export interface UseScriptOptions<T> extends Omit<HeadEntryOptions, 'transform'> {
/**
* Should the `dns-prefetch` tag be skipped.
*
* Useful if loading the script through a local proxy.
*/
skipEarlyConnections?: boolean
export interface UseScriptOptions<T> extends HeadEntryOptions {
/**
* Resolve the script instance from the window.
*/
Expand All @@ -35,10 +29,9 @@ export interface UseScriptOptions<T> extends Omit<HeadEntryOptions, 'transform'>
stub?: ((ctx: { script: ScriptInstance<T>, fn: string | symbol }) => any)
/**
* The trigger to load the script:
* - `manual` - Load the script manually by calling `$script.load()` or `$script.waitForLoad()`.
* - `manual` - Load the script manually by calling `$script.load()`.
* - `Promise` - Load the script when the promise resolves.
*/
trigger?: 'idle' | 'manual' | Promise<void>
trigger?: 'manual' | Promise<void>
/**
* Context to run events with. This is useful in Vue to attach the current instance context before
Expand Down
8 changes: 1 addition & 7 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,8 @@ export function useScript<T>(_input: UseScriptInput, _options?: UseScriptOptions
const trigger = options.trigger
if (options.trigger)
trigger instanceof Promise && trigger.then(script.load)
// if we're lazy it's likely it will load within the first 10 seconds, otherwise we just prefetch the DNS for a quicker load
maybeHintEarlyConnection(isIdle ? 'preconnect' : 'dns-prefetch')
}
else {
else
script.load()
// safe to preconnect as we'll load this script quite early
maybeHintEarlyConnection('preconnect')
}

function resolveInnerHtmlLoad(ctx: DomRenderTagContext) {
// we don't know up front if they'll be innerHTML or src due to the transform step
Expand Down
3 changes: 1 addition & 2 deletions test/unhead/ssr/useScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ describe('dom useScript', () => {
"bodyAttrs": "",
"bodyTags": "",
"bodyTagsOpen": "",
"headTags": "<link rel="preconnect" href="https://cdn.example.com" data-hid="7b546a7">
<script defer fetchpriority="low" src="https://cdn.example.com/script.js" onload="this.dataset.onload = true" onerror="this.dataset.onerror = true" onabort="this.dataset.onabort = true" onprogress="this.dataset.onprogress = true" onloadstart="this.dataset.onloadstart = true" data-hid="438d65b" data-unhead-events=""></script>",
"headTags": "<script defer fetchpriority="low" src="https://cdn.example.com/script.js" onload="this.dataset.onload = true" onerror="this.dataset.onerror = true" onloadstart="this.dataset.onloadstart = true" data-hid="438d65b" data-unhead-events=""></script>",
"htmlAttrs": "",
}
`)
Expand Down

0 comments on commit 0ebacad

Please sign in to comment.