Skip to content

Commit

Permalink
chore: improve read me
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 21, 2024
1 parent e040050 commit 3ae21f2
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,43 @@ Plus Nuxt goodies:
- 🕵️ Privacy Features - Trigger scripts loading on cookie consent, honour DoNotTrack.
- 🪵 DevTools integration - see all your loaded scripts with function logs

## Installation
## Quick Start

1. Install `@nuxt/scripts` dependency to your project:
To get started, simply run:

```bash
pnpm add -D @nuxt/scripts
#
yarn add -D @nuxt/scripts
#
npm install -D @nuxt/scripts
npx nuxi@latest module add @nuxt/scripts
```

2. Add it to your `modules` section in your `nuxt.config`:
To start using Nuxt Scripts, you can use the [useScript](https://unhead.unjs.io/usage/composables/use-script) composable to load your third-party scripts.

### Confetti Example

If you want to get a feel for how the module works, you can load the `js-confetti` library:

```ts
export default defineNuxtConfig({
modules: ['@nuxt/scripts']
type JSConfettiApi = { addConfetti: (options?: { emojis: string[] }) => void }

Check failure on line 45 in README.md

View workflow job for this annotation

GitHub Actions / ci

Use an `interface` instead of a `type`

const { addConfetti } = useScript<JSConfettiApi>('https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js', {
trigger: 'idle', // load on onNuxtReady
assetStrategy: 'bundle', //
use() {
return new window.JSConfetti()
},
})
// will run once the script loads
addConfetti({ emojis: ['🌈', '⚡️', '💥', '', '💫', '🌸'] })
```

## Background

Loading third-party IIFE scripts using `useHead` composable is easy. However,
things start getting more complicated quickly around SSR, lazy loading, and type safety.

Nuxt Scripts was created to solve these issues and more with the goal of making third-party scripts a breeze to use.

## Usage

### Getting Started

To start using Nuxt Scripts, you can use the `useScript` composable to load your third-party scripts.

```ts
useScript('https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js')
```
Nuxt Scripts was created to solve these issues and more with the goal of making third-party scripts more performant,
have better privacy and be better DX overall.

See the Unhead [useScript](https://unhead.unjs.io/usage/composables/use-script) guide for next steps.
## Guides

### Bundling Scripts

Expand All @@ -81,6 +80,27 @@ useScript('https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.brow
// js-confetti.browser.js will be downloaded and bundled with your app as a static asset
```

### Overriding Scripts

When working with modules that use Nuxt Script, you may want to modify the
behavior of the script. This is especially useful for
changing the asset strategy of a script as it needs to be defined statically.

To do so you can use the `overrides` module option.

```ts
export default defineNuxtConfig({
scripts: {
overrides: {
// the key is either a specified key or the script src
'confetti': {

Check failure on line 96 in README.md

View workflow job for this annotation

GitHub Actions / ci

Properties shouldn't be quoted as all quotes are redundant
assetStrategy: 'bundle'
}
}
}
})
```

### Privacy and Cookie Consent

Nuxt Scripts provides a `createScriptConsentTrigger` composable that allows you to load scripts based on user's consent.
Expand Down

0 comments on commit 3ae21f2

Please sign in to comment.