Skip to content

Commit

Permalink
chore: fix types & lint
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 13, 2024
1 parent 2099671 commit 947154e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 4 additions & 4 deletions docs/content/1.usage/2.composables/4.use-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The `useScript` composable aims to solve these issues and more with the goal of
```ts
const { gtag } = useScript<GoogleTag>('https://www.google-analytics.com/analytics.js', {
trigger: 'idle', // loads the script on requestIdleCallback hook
use() {
use() {
return { gtag: window.gtag }
}
})
Expand Down Expand Up @@ -117,7 +117,7 @@ myFunction('hello')

### Triggering Script Load

The `trigger` option is used to control when the script is loaded by the browser.
The `trigger` option is used to control when the script is loaded by the browser.

It can be one of the following:
- `idle`: Load the script when the browser calls the [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) hook.
Expand All @@ -138,7 +138,7 @@ $script.load()

```ts [Promise]
const { $script } = useScript('https://example.com/script.js', {
trigger: new Promise(resolve => {
trigger: new Promise((resolve) => {
setTimeout(resolve, 10000) // load after 10 seconds
})
})
Expand All @@ -165,7 +165,7 @@ const { gtag } = useScript<GoogleTag>('https://www.google-analytics.com/analytic
use() {
return { gtag: window.gtag }
},
stub () {
stub() {
if (process.server) {
return (fn: 'event', opt: string, opt2: { [key: string]: string }) => {
// send fetch to ga
Expand Down
3 changes: 1 addition & 2 deletions packages/unhead/src/plugins/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ export default defineHeadPlugin(head => ({
tag._eventHandlers![key] = value
}
})
if (head.ssr && tag._eventHandlers && (tag.props.src || tag.props.href)) {
if (head.ssr && tag._eventHandlers && (tag.props.src || tag.props.href))
tag.key = tag.key || hashCode(tag.props.src || tag.props.href)
}
}
},
'dom:renderTag': function (ctx) {
Expand Down
11 changes: 9 additions & 2 deletions packages/vue/src/composables/useScript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { UseScriptInput as BaseUseScriptInput, ScriptBase, ScriptInstance, UseScriptOptions, UseScriptStatus } from '@unhead/schema'
import type {
UseScriptInput as BaseUseScriptInput,
SchemaAugmentations,
ScriptBase,
ScriptInstance,
UseScriptOptions,
UseScriptStatus,
} from '@unhead/schema'
import { useScript as _useScript } from 'unhead'
import type { ComputedRef, Ref } from 'vue'
import { computed, getCurrentInstance, ref } from 'vue'
Expand All @@ -11,7 +18,7 @@ export interface VueScriptInstance<T> extends Omit<ScriptInstance<T>, 'loaded' |
status: Ref<UseScriptStatus>
}

export type UseScriptInput = string | (MaybeComputedRefEntriesOnly<Omit<ScriptBase, 'src'>> & { src: string })
export type UseScriptInput = string | (MaybeComputedRefEntriesOnly<Omit<ScriptBase & SchemaAugmentations['script'], 'src'>> & { src: string })

export function useScript<T>(_input: UseScriptInput, _options?: UseScriptOptions<T>): T & { $script: VueScriptInstance<T> } {
const input = typeof _input === 'string' ? { src: _input } : _input
Expand Down

0 comments on commit 947154e

Please sign in to comment.