@nuxt/docs-nightly 4.3.0-29356103.2f7957ac → 4.3.0-29430576.f48ea4c8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/1.getting-started/01.introduction.md +1 -1
- package/1.getting-started/02.installation.md +4 -6
- package/1.getting-started/03.configuration.md +27 -27
- package/1.getting-started/04.views.md +5 -5
- package/1.getting-started/05.assets.md +8 -8
- package/1.getting-started/06.styling.md +15 -15
- package/1.getting-started/07.routing.md +10 -6
- package/1.getting-started/08.seo-meta.md +3 -3
- package/1.getting-started/09.transitions.md +10 -10
- package/1.getting-started/10.data-fetching.md +16 -16
- package/1.getting-started/11.state-management.md +3 -3
- package/1.getting-started/12.error-handling.md +6 -6
- package/1.getting-started/13.server.md +6 -6
- package/1.getting-started/14.layers.md +32 -13
- package/1.getting-started/16.deployment.md +1 -1
- package/1.getting-started/17.testing.md +36 -5
- package/1.getting-started/18.upgrade.md +43 -35
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.nuxt.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.output.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.assets.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.components.md +6 -6
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.composables.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.layouts.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.middleware.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.pages.md +17 -17
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.plugins.md +3 -7
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.utils.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.app.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.error.md +1 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.content.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.modules.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.node_modules.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.public.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.server.md +7 -7
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.shared.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.env.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtignore.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtrc.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.nuxt-config.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.package.md +1 -1
- package/2.directory-structure/3.tsconfig.md +69 -0
- package/2.directory-structure/index.md +61 -0
- package/{2.guide → 3.guide}/0.index.md +10 -7
- package/{2.guide/2.concepts/3.rendering.md → 3.guide/1.concepts/1.rendering.md} +4 -30
- package/{2.guide/2.concepts/2.vuejs-development.md → 3.guide/1.concepts/10.vuejs-development.md} +7 -6
- package/{2.guide/2.concepts/10.nuxt-lifecycle.md → 3.guide/1.concepts/2.nuxt-lifecycle.md} +32 -25
- package/{2.guide/2.concepts/1.auto-imports.md → 3.guide/1.concepts/3.auto-imports.md} +7 -7
- package/{2.guide/2.concepts → 3.guide/1.concepts}/4.server-engine.md +3 -3
- package/{2.guide/2.concepts → 3.guide/1.concepts}/5.modules.md +2 -2
- package/{2.guide/2.concepts → 3.guide/1.concepts}/7.esm.md +3 -2
- package/{2.guide/2.concepts → 3.guide/1.concepts}/8.typescript.md +15 -38
- package/{2.guide/2.concepts → 3.guide/1.concepts}/9.code-style.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/hydration.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/performance.md +2 -2
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +255 -0
- package/3.guide/3.ai/2.llms-txt.md +65 -0
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +299 -0
- package/3.guide/4.modules/4.recipes-advanced.md +231 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/{2.guide/4.recipes → 3.guide/5.recipes}/1.custom-routing.md +5 -5
- package/{2.guide/4.recipes → 3.guide/5.recipes}/2.vite-plugin.md +1 -1
- package/{2.guide/4.recipes → 3.guide/5.recipes}/3.custom-usefetch.md +1 -1
- package/{2.guide/4.recipes → 3.guide/5.recipes}/4.sessions-and-authentication.md +1 -1
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.events.md +2 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.experimental-features.md +10 -10
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.features.md +1 -1
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.internals.md +5 -4
- package/{2.guide/3.going-further → 3.guide/6.going-further}/10.runtime-config.md +2 -2
- package/{2.guide/3.going-further → 3.guide/6.going-further}/2.hooks.md +3 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/4.kit.md +1 -1
- package/{2.guide/3.going-further → 3.guide/6.going-further}/6.nuxt-app.md +5 -5
- package/{2.guide/3.going-further → 3.guide/6.going-further}/7.layers.md +42 -25
- package/{2.guide/3.going-further → 3.guide/6.going-further}/9.debugging.md +1 -1
- package/{3.api → 4.api}/1.components/10.nuxt-picture.md +1 -1
- package/{3.api → 4.api}/1.components/11.teleports.md +1 -1
- package/{3.api → 4.api}/1.components/12.nuxt-route-announcer.md +1 -3
- package/{3.api → 4.api}/1.components/13.nuxt-time.md +0 -2
- package/{3.api → 4.api}/1.components/2.nuxt-page.md +3 -3
- package/{3.api → 4.api}/1.components/3.nuxt-layout.md +5 -5
- package/{3.api → 4.api}/1.components/4.nuxt-link.md +11 -11
- package/{3.api → 4.api}/1.components/5.nuxt-loading-indicator.md +1 -1
- package/{3.api → 4.api}/1.components/6.nuxt-error-boundary.md +1 -1
- package/{3.api → 4.api}/1.components/7.nuxt-welcome.md +2 -2
- package/{3.api → 4.api}/2.composables/use-app-config.md +1 -1
- package/{3.api → 4.api}/2.composables/use-async-data.md +76 -13
- package/4.api/2.composables/use-cookie.md +183 -0
- package/{3.api → 4.api}/2.composables/use-fetch.md +33 -33
- package/{3.api → 4.api}/2.composables/use-head-safe.md +37 -20
- package/4.api/2.composables/use-head.md +184 -0
- package/{3.api → 4.api}/2.composables/use-hydration.md +24 -18
- package/4.api/2.composables/use-lazy-async-data.md +96 -0
- package/4.api/2.composables/use-lazy-fetch.md +111 -0
- package/{3.api → 4.api}/2.composables/use-nuxt-app.md +7 -7
- package/{3.api → 4.api}/2.composables/use-nuxt-data.md +1 -1
- package/{3.api → 4.api}/2.composables/use-request-fetch.md +1 -1
- package/{3.api → 4.api}/2.composables/use-response-header.md +1 -1
- package/{3.api → 4.api}/2.composables/use-route-announcer.md +0 -2
- package/{3.api → 4.api}/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +94 -0
- package/{3.api → 4.api}/2.composables/use-runtime-config.md +1 -1
- package/{3.api → 4.api}/2.composables/use-runtime-hook.md +1 -1
- package/{3.api → 4.api}/2.composables/use-state.md +1 -1
- package/{3.api → 4.api}/3.utils/$fetch.md +1 -1
- package/{3.api → 4.api}/3.utils/abort-navigation.md +3 -3
- package/{3.api → 4.api}/3.utils/add-route-middleware.md +1 -1
- package/{3.api → 4.api}/3.utils/call-once.md +0 -2
- package/{3.api → 4.api}/3.utils/define-lazy-hydration-component.md +4 -4
- package/{3.api → 4.api}/3.utils/define-nuxt-component.md +1 -1
- package/4.api/3.utils/define-nuxt-plugin.md +102 -0
- package/{3.api → 4.api}/3.utils/define-nuxt-route-middleware.md +2 -2
- package/{3.api → 4.api}/3.utils/define-page-meta.md +14 -14
- package/{3.api → 4.api}/3.utils/navigate-to.md +15 -15
- package/{3.api → 4.api}/3.utils/on-before-route-leave.md +1 -1
- package/{3.api → 4.api}/3.utils/on-before-route-update.md +1 -1
- package/{3.api → 4.api}/3.utils/refresh-cookie.md +1 -3
- package/{3.api → 4.api}/3.utils/update-app-config.md +2 -2
- package/{3.api → 4.api}/4.commands/add.md +11 -11
- package/4.api/4.commands/analyze.md +42 -0
- package/4.api/4.commands/build-module.md +42 -0
- package/4.api/4.commands/build.md +47 -0
- package/{3.api → 4.api}/4.commands/cleanup.md +6 -6
- package/4.api/4.commands/dev.md +60 -0
- package/{3.api → 4.api}/4.commands/devtools.md +7 -7
- package/4.api/4.commands/generate.md +42 -0
- package/4.api/4.commands/info.md +33 -0
- package/4.api/4.commands/init.md +50 -0
- package/4.api/4.commands/module.md +84 -0
- package/4.api/4.commands/prepare.md +41 -0
- package/4.api/4.commands/preview.md +44 -0
- package/4.api/4.commands/test.md +40 -0
- package/4.api/4.commands/typecheck.md +44 -0
- package/4.api/4.commands/upgrade.md +37 -0
- package/{3.api → 4.api}/5.kit/1.modules.md +18 -18
- package/{3.api → 4.api}/5.kit/10.templates.md +23 -23
- package/{3.api → 4.api}/5.kit/11.nitro.md +35 -35
- package/{3.api → 4.api}/5.kit/14.builder.md +21 -21
- package/{3.api → 4.api}/5.kit/16.layers.md +12 -12
- package/{3.api → 4.api}/5.kit/2.programmatic.md +2 -2
- package/{3.api → 4.api}/5.kit/4.autoimports.md +18 -18
- package/4.api/5.kit/5.components.md +146 -0
- package/4.api/6.advanced/1.hooks.md +105 -0
- package/{3.api → 4.api}/6.nuxt-config.md +29 -28
- package/5.community/3.reporting-bugs.md +1 -1
- package/5.community/4.contribution.md +4 -4
- package/5.community/5.framework-contribution.md +8 -8
- package/5.community/6.roadmap.md +25 -25
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +1 -1
- package/6.bridge/2.typescript.md +1 -1
- package/6.bridge/3.bridge-composition-api.md +1 -1
- package/6.bridge/4.plugins-and-middleware.md +2 -2
- package/7.migration/11.server.md +1 -1
- package/7.migration/2.configuration.md +5 -5
- package/7.migration/20.module-authors.md +3 -3
- package/7.migration/3.auto-imports.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +2 -2
- package/7.migration/6.pages-and-layouts.md +6 -6
- package/README.md +1 -1
- package/package.json +1 -1
- package/2.guide/1.directory-structure/3.tsconfig.md +0 -38
- package/2.guide/3.going-further/3.modules.md +0 -901
- package/3.api/2.composables/use-cookie.md +0 -183
- package/3.api/2.composables/use-head.md +0 -69
- package/3.api/2.composables/use-lazy-async-data.md +0 -47
- package/3.api/2.composables/use-lazy-fetch.md +0 -55
- package/3.api/2.composables/use-router.md +0 -94
- package/3.api/3.utils/define-nuxt-plugin.md +0 -102
- package/3.api/4.commands/analyze.md +0 -42
- package/3.api/4.commands/build-module.md +0 -42
- package/3.api/4.commands/build.md +0 -47
- package/3.api/4.commands/dev.md +0 -60
- package/3.api/4.commands/generate.md +0 -42
- package/3.api/4.commands/info.md +0 -33
- package/3.api/4.commands/init.md +0 -50
- package/3.api/4.commands/module.md +0 -84
- package/3.api/4.commands/prepare.md +0 -41
- package/3.api/4.commands/preview.md +0 -44
- package/3.api/4.commands/test.md +0 -40
- package/3.api/4.commands/typecheck.md +0 -44
- package/3.api/4.commands/upgrade.md +0 -37
- package/3.api/5.kit/5.components.md +0 -146
- package/3.api/6.advanced/1.hooks.md +0 -105
- /package/{2.guide/1.directory-structure → 2.directory-structure}/.navigation.yml +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/.navigation.yml +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.app-config.md +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/2.gitignore.md +0 -0
- /package/{2.guide → 3.guide}/.navigation.yml +0 -0
- /package/{2.guide/2.concepts → 3.guide/1.concepts}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/plugins.md +0 -0
- /package/{2.guide/4.recipes → 3.guide/5.recipes}/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/6.going-further}/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/6.going-further}/11.nightly-release-channel.md +0 -0
- /package/{2.guide/3.going-further → 3.guide/6.going-further}/index.md +0 -0
- /package/{3.api → 4.api}/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/1.client-only.md +0 -0
- /package/{3.api → 4.api}/1.components/1.dev-only.md +0 -0
- /package/{3.api → 4.api}/1.components/1.nuxt-client-fallback.md +0 -0
- /package/{3.api → 4.api}/1.components/8.nuxt-island.md +0 -0
- /package/{3.api → 4.api}/1.components/9.nuxt-img.md +0 -0
- /package/{3.api → 4.api}/2.composables/.navigation.yml +0 -0
- /package/{3.api → 4.api}/2.composables/on-prehydrate.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-error.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-loading-indicator.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-preview-mode.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-event.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-header.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-headers.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-url.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-seo-meta.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-server-seo-meta.md +0 -0
- /package/{3.api → 4.api}/3.utils/.navigation.yml +0 -0
- /package/{3.api → 4.api}/3.utils/clear-error.md +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-state.md +0 -0
- /package/{3.api → 4.api}/3.utils/create-error.md +0 -0
- /package/{3.api → 4.api}/3.utils/define-route-rules.md +0 -0
- /package/{3.api → 4.api}/3.utils/on-nuxt-ready.md +0 -0
- /package/{3.api → 4.api}/3.utils/prefetch-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/preload-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/preload-route-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/prerender-routes.md +0 -0
- /package/{3.api → 4.api}/3.utils/refresh-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/3.utils/reload-nuxt-app.md +0 -0
- /package/{3.api → 4.api}/3.utils/set-page-layout.md +0 -0
- /package/{3.api → 4.api}/3.utils/set-response-status.md +0 -0
- /package/{3.api → 4.api}/3.utils/show-error.md +0 -0
- /package/{3.api → 4.api}/4.commands/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/10.runtime-config.md +0 -0
- /package/{3.api → 4.api}/5.kit/12.resolving.md +0 -0
- /package/{3.api → 4.api}/5.kit/13.logging.md +0 -0
- /package/{3.api → 4.api}/5.kit/15.examples.md +0 -0
- /package/{3.api → 4.api}/5.kit/3.compatibility.md +0 -0
- /package/{3.api → 4.api}/5.kit/6.context.md +0 -0
- /package/{3.api → 4.api}/5.kit/7.pages.md +0 -0
- /package/{3.api → 4.api}/5.kit/8.layout.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.head.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.plugins.md +0 -0
- /package/{3.api → 4.api}/6.advanced/.navigation.yml +0 -0
- /package/{3.api → 4.api}/6.advanced/2.import-meta.md +0 -0
- /package/{3.api → 4.api}/index.md +0 -0
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: 'useCookie'
|
|
3
|
-
description: useCookie is an SSR-friendly composable to read and write cookies.
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/cookie.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
Within your pages, components, and plugins, you can use `useCookie` to read and write cookies in an SSR-friendly way.
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
const cookie = useCookie(name, options)
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
::note
|
|
20
|
-
`useCookie` only works in the [Nuxt context](/docs/4.x/guide/going-further/nuxt-app#the-nuxt-context).
|
|
21
|
-
::
|
|
22
|
-
|
|
23
|
-
::tip
|
|
24
|
-
The returned ref will automatically serialize and deserialize cookie values to JSON.
|
|
25
|
-
::
|
|
26
|
-
|
|
27
|
-
## Type
|
|
28
|
-
|
|
29
|
-
```ts [Signature]
|
|
30
|
-
import type { Ref } from 'vue'
|
|
31
|
-
import type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es'
|
|
32
|
-
|
|
33
|
-
export interface CookieOptions<T = any> extends Omit<CookieSerializeOptions & CookieParseOptions, 'decode' | 'encode'> {
|
|
34
|
-
decode?(value: string): T
|
|
35
|
-
encode?(value: T): string
|
|
36
|
-
default?: () => T | Ref<T>
|
|
37
|
-
watch?: boolean | 'shallow'
|
|
38
|
-
readonly?: boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface CookieRef<T> extends Ref<T> {}
|
|
42
|
-
|
|
43
|
-
export function useCookie<T = string | null | undefined> (
|
|
44
|
-
name: string,
|
|
45
|
-
options?: CookieOptions<T>
|
|
46
|
-
): CookieRef<T>
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Parameters
|
|
50
|
-
|
|
51
|
-
`name`: The name of the cookie.
|
|
52
|
-
|
|
53
|
-
`options`: Options to control cookie behavior. The object can have the following properties:
|
|
54
|
-
|
|
55
|
-
Most of the options will be directly passed to the [cookie](https://github.com/jshttp/cookie) package.
|
|
56
|
-
|
|
57
|
-
| Property | Type | Default | Description |
|
|
58
|
-
| --- | --- | --- | --- |
|
|
59
|
-
| `decode` | `(value: string) => T` | `decodeURIComponent` + [destr](https://github.com/unjs/destr). | Custom function to decode the cookie value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously encoded cookie value into a JavaScript string or other object. <br/> **Note:** If an error is thrown from this function, the original, non-decoded cookie value will be returned as the cookie's value. |
|
|
60
|
-
| `encode` | `(value: T) => string` | `JSON.stringify` + `encodeURIComponent` | Custom function to encode the cookie value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value. |
|
|
61
|
-
| `default` | `() => T \| Ref<T>` | `undefined` | Function returning the default value if the cookie does not exist. The function can also return a `Ref`. |
|
|
62
|
-
| `watch` | `boolean \| 'shallow'` | `true` | Whether to watch for changes and update the cookie. `true` for deep watch, `'shallow'` for shallow watch, i.e. data changes for only top level properties, `false` to disable. <br/> **Note:** Refresh `useCookie` values manually when a cookie has changed with [`refreshCookie`](/docs/4.x/api/utils/refresh-cookie). |
|
|
63
|
-
| `readonly` | `boolean` | `false` | If `true`, disables writing to the cookie. |
|
|
64
|
-
| `maxAge` | `number` | `undefined` | Max age in seconds for the cookie, i.e. the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2). The given number will be converted to an integer by rounding down. By default, no maximum age is set. |
|
|
65
|
-
| `expires` | `Date` | `undefined` | Expiration date for the cookie. By default, no expiration is set. Most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application. <br/> **Note:** The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and `maxAge` is set, then `maxAge` takes precedence, but not all clients may obey this, so if both are set, they should point to the same date and time! <br/>If neither of `expires` and `maxAge` is set, the cookie will be session-only and removed when the user closes their browser. |
|
|
66
|
-
| `httpOnly` | `boolean` | `false` | Sets the HttpOnly attribute. <br/> **Note:** Be careful when setting this to `true`, as compliant clients will not allow client-side JavaScript to see the cookie in `document.cookie`. |
|
|
67
|
-
| `secure` | `boolean` | `false` | Sets the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5). <br/>**Note:** Be careful when setting this to `true`, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection. This can lead to hydration errors. |
|
|
68
|
-
| `partitioned` | `boolean` | `false` | Sets the [`Partitioned` `Set-Cookie` attribute](https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1). <br/>**Note:** This is an attribute that has not yet been fully standardized, and may change in the future. <br/>This also means many clients may ignore this attribute until they understand it.<br/>More information can be found in the [proposal](https://github.com/privacycg/CHIPS). |
|
|
69
|
-
| `domain` | `string` | `undefined` | Sets the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3). By default, no domain is set, and most clients will consider applying the cookie only to the current domain. |
|
|
70
|
-
| `path` | `string` | `'/'` | Sets the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4). By default, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4). |
|
|
71
|
-
| `sameSite` | `boolean \| string` | `undefined` | Sets the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7). <br/>- `true` will set the `SameSite` attribute to `Strict` for strict same-site enforcement.<br/>- `false` will not set the `SameSite` attribute.<br/>- `'lax'` will set the `SameSite` attribute to `Lax` for lax same-site enforcement.<br/>- `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.<br/>- `'strict'` will set the `SameSite` attribute to `Strict` for strict same-site enforcement. |
|
|
72
|
-
|
|
73
|
-
## Return Values
|
|
74
|
-
|
|
75
|
-
Returns a Vue `Ref<T>` representing the cookie value. Updating the ref will update the cookie (unless `readonly` is set). The ref is SSR-friendly and will work on both client and server.
|
|
76
|
-
|
|
77
|
-
## Examples
|
|
78
|
-
|
|
79
|
-
### Basic Usage
|
|
80
|
-
|
|
81
|
-
The example below creates a cookie called `counter`. If the cookie doesn't exist, it is initially set to a random value. Whenever we update the `counter` variable, the cookie will be updated accordingly.
|
|
82
|
-
|
|
83
|
-
```vue [app/app.vue]
|
|
84
|
-
<script setup lang="ts">
|
|
85
|
-
const counter = useCookie('counter')
|
|
86
|
-
|
|
87
|
-
counter.value ||= Math.round(Math.random() * 1000)
|
|
88
|
-
</script>
|
|
89
|
-
|
|
90
|
-
<template>
|
|
91
|
-
<div>
|
|
92
|
-
<h1>Counter: {{ counter || '-' }}</h1>
|
|
93
|
-
<button @click="counter = null">
|
|
94
|
-
reset
|
|
95
|
-
</button>
|
|
96
|
-
<button @click="counter--">
|
|
97
|
-
-
|
|
98
|
-
</button>
|
|
99
|
-
<button @click="counter++">
|
|
100
|
-
+
|
|
101
|
-
</button>
|
|
102
|
-
</div>
|
|
103
|
-
</template>
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Readonly Cookies
|
|
107
|
-
|
|
108
|
-
```vue
|
|
109
|
-
<script setup lang="ts">
|
|
110
|
-
const user = useCookie(
|
|
111
|
-
'userInfo',
|
|
112
|
-
{
|
|
113
|
-
default: () => ({ score: -1 }),
|
|
114
|
-
watch: false,
|
|
115
|
-
},
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
if (user.value) {
|
|
119
|
-
// the actual `userInfo` cookie will not be updated
|
|
120
|
-
user.value.score++
|
|
121
|
-
}
|
|
122
|
-
</script>
|
|
123
|
-
|
|
124
|
-
<template>
|
|
125
|
-
<div>User score: {{ user?.score }}</div>
|
|
126
|
-
</template>
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### Writable Cookies
|
|
130
|
-
|
|
131
|
-
```vue
|
|
132
|
-
<script setup lang="ts">
|
|
133
|
-
const list = useCookie(
|
|
134
|
-
'list',
|
|
135
|
-
{
|
|
136
|
-
default: () => [],
|
|
137
|
-
watch: 'shallow',
|
|
138
|
-
},
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
function add () {
|
|
142
|
-
list.value?.push(Math.round(Math.random() * 1000))
|
|
143
|
-
// list cookie won't be updated with this change
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function save () {
|
|
147
|
-
// the actual `list` cookie will be updated
|
|
148
|
-
list.value &&= [...list.value]
|
|
149
|
-
}
|
|
150
|
-
</script>
|
|
151
|
-
|
|
152
|
-
<template>
|
|
153
|
-
<div>
|
|
154
|
-
<h1>List</h1>
|
|
155
|
-
<pre>{{ list }}</pre>
|
|
156
|
-
<button @click="add">
|
|
157
|
-
Add
|
|
158
|
-
</button>
|
|
159
|
-
<button @click="save">
|
|
160
|
-
Save
|
|
161
|
-
</button>
|
|
162
|
-
</div>
|
|
163
|
-
</template>
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Cookies in API Routes
|
|
167
|
-
|
|
168
|
-
You can use `getCookie` and `setCookie` from [`h3`](https://github.com/h3js/h3) package to set cookies in server API routes.
|
|
169
|
-
|
|
170
|
-
```ts [server/api/counter.ts]
|
|
171
|
-
export default defineEventHandler((event) => {
|
|
172
|
-
// Read counter cookie
|
|
173
|
-
let counter = getCookie(event, 'counter') || 0
|
|
174
|
-
|
|
175
|
-
// Increase counter cookie by 1
|
|
176
|
-
setCookie(event, 'counter', ++counter)
|
|
177
|
-
|
|
178
|
-
// Send JSON response
|
|
179
|
-
return { counter }
|
|
180
|
-
})
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
:link-example{to="/docs/4.x/examples/advanced/use-cookie"}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: useHead
|
|
3
|
-
description: useHead customizes the head properties of individual pages of your Nuxt app.
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/unjs/unhead/blob/main/packages/vue/src/composables.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
The [`useHead`](/docs/4.x/api/composables/use-head) composable function allows you to manage your head tags in a programmatic and reactive way, powered by [Unhead](https://unhead.unjs.io). If the data comes from a user or other untrusted source, we recommend you check out [`useHeadSafe`](/docs/4.x/api/composables/use-head-safe).
|
|
12
|
-
|
|
13
|
-
:read-more{to="/docs/4.x/getting-started/seo-meta"}
|
|
14
|
-
|
|
15
|
-
## Type
|
|
16
|
-
|
|
17
|
-
```ts [Signature]
|
|
18
|
-
export function useHead (meta: MaybeComputedRef<MetaObject>): void
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Below are the non-reactive types for [`useHead`](/docs/4.x/api/composables/use-head) .
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
interface MetaObject {
|
|
25
|
-
title?: string
|
|
26
|
-
titleTemplate?: string | ((title?: string) => string)
|
|
27
|
-
base?: Base
|
|
28
|
-
link?: Link[]
|
|
29
|
-
meta?: Meta[]
|
|
30
|
-
style?: Style[]
|
|
31
|
-
script?: Script[]
|
|
32
|
-
noscript?: Noscript[]
|
|
33
|
-
htmlAttrs?: HtmlAttributes
|
|
34
|
-
bodyAttrs?: BodyAttributes
|
|
35
|
-
}
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
See [@unhead/vue](https://github.com/unjs/unhead/blob/main/packages/vue/src/types/schema.ts) for more detailed types.
|
|
39
|
-
|
|
40
|
-
::note
|
|
41
|
-
The properties of `useHead` can be dynamic, accepting `ref`, `computed` and `reactive` properties. `meta` parameter can also accept a function returning an object to make the entire object reactive.
|
|
42
|
-
::
|
|
43
|
-
|
|
44
|
-
## Params
|
|
45
|
-
|
|
46
|
-
### `meta`
|
|
47
|
-
|
|
48
|
-
**Type**: `MetaObject`
|
|
49
|
-
|
|
50
|
-
An object accepting the following head metadata:
|
|
51
|
-
|
|
52
|
-
- `meta`: Each element in the array is mapped to a newly-created `<meta>` tag, where object properties are mapped to the corresponding attributes.
|
|
53
|
-
- **Type**: `Array<Record<string, any>>`
|
|
54
|
-
- `link`: Each element in the array is mapped to a newly-created `<link>` tag, where object properties are mapped to the corresponding attributes.
|
|
55
|
-
- **Type**: `Array<Record<string, any>>`
|
|
56
|
-
- `style`: Each element in the array is mapped to a newly-created `<style>` tag, where object properties are mapped to the corresponding attributes.
|
|
57
|
-
- **Type**: `Array<Record<string, any>>`
|
|
58
|
-
- `script`: Each element in the array is mapped to a newly-created `<script>` tag, where object properties are mapped to the corresponding attributes.
|
|
59
|
-
- **Type**: `Array<Record<string, any>>`
|
|
60
|
-
- `noscript`: Each element in the array is mapped to a newly-created `<noscript>` tag, where object properties are mapped to the corresponding attributes.
|
|
61
|
-
- **Type**: `Array<Record<string, any>>`
|
|
62
|
-
- `titleTemplate`: Configures dynamic template to customize the page title on an individual page.
|
|
63
|
-
- **Type**: `string` | `((title: string) => string)`
|
|
64
|
-
- `title`: Sets static page title on an individual page.
|
|
65
|
-
- **Type**: `string`
|
|
66
|
-
- `bodyAttrs`: Sets attributes of the `<body>` tag. Each object property is mapped to the corresponding attribute.
|
|
67
|
-
- **Type**: `Record<string, any>`
|
|
68
|
-
- `htmlAttrs`: Sets attributes of the `<html>` tag. Each object property is mapped to the corresponding attribute.
|
|
69
|
-
- **Type**: `Record<string, any>`
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: useLazyAsyncData
|
|
3
|
-
description: This wrapper around useAsyncData triggers navigation immediately.
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## Description
|
|
12
|
-
|
|
13
|
-
By default, [`useAsyncData`](/docs/4.x/api/composables/use-async-data) blocks navigation until its async handler is resolved. `useLazyAsyncData` provides a wrapper around [`useAsyncData`](/docs/4.x/api/composables/use-async-data) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
14
|
-
|
|
15
|
-
::note
|
|
16
|
-
`useLazyAsyncData` has the same signature as [`useAsyncData`](/docs/4.x/api/composables/use-async-data).
|
|
17
|
-
::
|
|
18
|
-
|
|
19
|
-
:read-more{to="/docs/4.x/api/composables/use-async-data"}
|
|
20
|
-
|
|
21
|
-
## Example
|
|
22
|
-
|
|
23
|
-
```vue [app/pages/index.vue]
|
|
24
|
-
<script setup lang="ts">
|
|
25
|
-
/* Navigation will occur before fetching is complete.
|
|
26
|
-
Handle 'pending' and 'error' states directly within your component's template
|
|
27
|
-
*/
|
|
28
|
-
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
|
29
|
-
|
|
30
|
-
watch(count, (newCount) => {
|
|
31
|
-
// Because count might start out null, you won't have access
|
|
32
|
-
// to its contents immediately, but you can watch it.
|
|
33
|
-
})
|
|
34
|
-
</script>
|
|
35
|
-
|
|
36
|
-
<template>
|
|
37
|
-
<div>
|
|
38
|
-
{{ status === 'pending' ? 'Loading' : count }}
|
|
39
|
-
</div>
|
|
40
|
-
</template>
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
::warning
|
|
44
|
-
`useLazyAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyAsyncData`.
|
|
45
|
-
::
|
|
46
|
-
|
|
47
|
-
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: 'useLazyFetch'
|
|
3
|
-
description: This wrapper around useFetch triggers navigation immediately.
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/fetch.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## Description
|
|
12
|
-
|
|
13
|
-
By default, [`useFetch`](/docs/4.x/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` provides a wrapper around [`useFetch`](/docs/4.x/api/composables/use-fetch) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
14
|
-
|
|
15
|
-
::note
|
|
16
|
-
`useLazyFetch` has the same signature as [`useFetch`](/docs/4.x/api/composables/use-fetch).
|
|
17
|
-
::
|
|
18
|
-
|
|
19
|
-
::note
|
|
20
|
-
Awaiting `useLazyFetch` in this mode only ensures the call is initialized. On client-side navigation, data may not be immediately available, and you should make sure to handle the pending state in your app.
|
|
21
|
-
::
|
|
22
|
-
|
|
23
|
-
:read-more{to="/docs/4.x/api/composables/use-fetch"}
|
|
24
|
-
|
|
25
|
-
## Example
|
|
26
|
-
|
|
27
|
-
```vue [app/pages/index.vue]
|
|
28
|
-
<script setup lang="ts">
|
|
29
|
-
/* Navigation will occur before fetching is complete.
|
|
30
|
-
* Handle 'pending' and 'error' states directly within your component's template
|
|
31
|
-
*/
|
|
32
|
-
const { status, data: posts } = await useLazyFetch('/api/posts')
|
|
33
|
-
watch(posts, (newPosts) => {
|
|
34
|
-
// Because posts might start out null, you won't have access
|
|
35
|
-
// to its contents immediately, but you can watch it.
|
|
36
|
-
})
|
|
37
|
-
</script>
|
|
38
|
-
|
|
39
|
-
<template>
|
|
40
|
-
<div v-if="status === 'pending'">
|
|
41
|
-
Loading ...
|
|
42
|
-
</div>
|
|
43
|
-
<div v-else>
|
|
44
|
-
<div v-for="post in posts">
|
|
45
|
-
<!-- do something -->
|
|
46
|
-
</div>
|
|
47
|
-
</div>
|
|
48
|
-
</template>
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
::note
|
|
52
|
-
`useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch`.
|
|
53
|
-
::
|
|
54
|
-
|
|
55
|
-
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "useRouter"
|
|
3
|
-
description: "The useRouter composable returns the router instance."
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/router.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
```vue [app/pages/index.vue]
|
|
12
|
-
<script setup lang="ts">
|
|
13
|
-
const router = useRouter()
|
|
14
|
-
</script>
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
If you only need the router instance within your template, use `$router`:
|
|
18
|
-
|
|
19
|
-
```vue [app/pages/index.vue]
|
|
20
|
-
<template>
|
|
21
|
-
<button @click="$router.back()">
|
|
22
|
-
Back
|
|
23
|
-
</button>
|
|
24
|
-
</template>
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
If you have a `app/pages/` directory, `useRouter` is identical in behavior to the one provided by `vue-router`.
|
|
28
|
-
|
|
29
|
-
::read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/Router.html#Properties-currentRoute" target="_blank"}
|
|
30
|
-
Read `vue-router` documentation about the `Router` interface.
|
|
31
|
-
::
|
|
32
|
-
|
|
33
|
-
## Basic Manipulation
|
|
34
|
-
|
|
35
|
-
- [`addRoute()`](https://router.vuejs.org/api/interfaces/Router.html#addRoute): Add a new route to the router instance. `parentName` can be provided to add new route as the child of an existing route.
|
|
36
|
-
- [`removeRoute()`](https://router.vuejs.org/api/interfaces/Router.html#removeRoute): Remove an existing route by its name.
|
|
37
|
-
- [`getRoutes()`](https://router.vuejs.org/api/interfaces/Router.html#getRoutes): Get a full list of all the route records.
|
|
38
|
-
- [`hasRoute()`](https://router.vuejs.org/api/interfaces/Router.html#hasRoute): Checks if a route with a given name exists.
|
|
39
|
-
- [`resolve()`](https://router.vuejs.org/api/interfaces/Router.html#resolve): Returns the normalized version of a route location. Also includes an `href` property that includes any existing base.
|
|
40
|
-
|
|
41
|
-
```ts [Example]
|
|
42
|
-
const router = useRouter()
|
|
43
|
-
|
|
44
|
-
router.addRoute({ name: 'home', path: '/home', component: Home })
|
|
45
|
-
router.removeRoute('home')
|
|
46
|
-
router.getRoutes()
|
|
47
|
-
router.hasRoute('home')
|
|
48
|
-
router.resolve({ name: 'home' })
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
::note
|
|
52
|
-
`router.addRoute()` adds route details into an array of routes and it is useful while building [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins) while `router.push()` on the other hand, triggers a new navigation immediately and it is useful in pages, Vue components and composable.
|
|
53
|
-
::
|
|
54
|
-
|
|
55
|
-
## Based on History API
|
|
56
|
-
|
|
57
|
-
- [`back()`](https://router.vuejs.org/api/interfaces/Router.html#back): Go back in history if possible, same as `router.go(-1)`.
|
|
58
|
-
- [`forward()`](https://router.vuejs.org/api/interfaces/Router.html#forward): Go forward in history if possible, same as `router.go(1)`.
|
|
59
|
-
- [`go()`](https://router.vuejs.org/api/interfaces/Router.html#go): Move forward or backward through the history without the hierarchical restrictions enforced in `router.back()` and `router.forward()`.
|
|
60
|
-
- [`push()`](https://router.vuejs.org/api/interfaces/Router.html#push): Programmatically navigate to a new URL by pushing an entry in the history stack. **It is recommended to use [`navigateTo`](/docs/4.x/api/utils/navigate-to) instead.**
|
|
61
|
-
- [`replace()`](https://router.vuejs.org/api/interfaces/Router.html#replace): Programmatically navigate to a new URL by replacing the current entry in the routes history stack. **It is recommended to use [`navigateTo`](/docs/4.x/api/utils/navigate-to) instead.**
|
|
62
|
-
|
|
63
|
-
```ts [Example]
|
|
64
|
-
const router = useRouter()
|
|
65
|
-
|
|
66
|
-
router.back()
|
|
67
|
-
router.forward()
|
|
68
|
-
router.go(3)
|
|
69
|
-
router.push({ path: '/home' })
|
|
70
|
-
router.replace({ hash: '#bio' })
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/History" target="_blank"}
|
|
74
|
-
Read more about the browser's History API.
|
|
75
|
-
::
|
|
76
|
-
|
|
77
|
-
## Navigation Guards
|
|
78
|
-
|
|
79
|
-
`useRouter` composable provides `afterEach`, `beforeEach` and `beforeResolve` helper methods that acts as navigation guards.
|
|
80
|
-
|
|
81
|
-
However, Nuxt has a concept of **route middleware** that simplifies the implementation of navigation guards and provides a better developer experience.
|
|
82
|
-
|
|
83
|
-
:read-more{to="/docs/4.x/guide/directory-structure/app/middleware"}
|
|
84
|
-
|
|
85
|
-
## Promise and Error Handling
|
|
86
|
-
|
|
87
|
-
- [`isReady()`](https://router.vuejs.org/api/interfaces/Router.html#isReady): Returns a Promise that resolves when the router has completed the initial navigation.
|
|
88
|
-
- [`onError`](https://router.vuejs.org/api/interfaces/Router.html#onError): Adds an error handler that is called every time a non caught error happens during navigation.
|
|
89
|
-
|
|
90
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/Router.html#Methods" title="Vue Router Docs" target="_blank"}
|
|
91
|
-
|
|
92
|
-
## Universal Router Instance
|
|
93
|
-
|
|
94
|
-
If you do not have a `app/pages/` folder, then [`useRouter`](/docs/4.x/api/composables/use-router) will return a universal router instance with similar helper methods, but be aware that not all features may be supported or behave in exactly the same way as with `vue-router`.
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "defineNuxtPlugin"
|
|
3
|
-
description: defineNuxtPlugin() is a helper function for creating Nuxt plugins.
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/nuxt.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
`defineNuxtPlugin` is a helper function for creating Nuxt plugins with enhanced functionality and type safety. This utility normalizes different plugin formats into a consistent structure that works seamlessly within Nuxt's plugin system.
|
|
12
|
-
|
|
13
|
-
```ts twoslash [plugins/hello.ts]
|
|
14
|
-
export default defineNuxtPlugin((nuxtApp) => {
|
|
15
|
-
// Doing something with nuxtApp
|
|
16
|
-
})
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
:read-more{to="/docs/4.x/guide/directory-structure/app/plugins#creating-plugins"}
|
|
20
|
-
|
|
21
|
-
## Type
|
|
22
|
-
|
|
23
|
-
```ts [Signature]
|
|
24
|
-
export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plugin<T> | ObjectPlugin<T>): Plugin<T> & ObjectPlugin<T>
|
|
25
|
-
|
|
26
|
-
type Plugin<T> = (nuxt: NuxtApp) => Promise<void> | Promise<{ provide?: T }> | void | { provide?: T }
|
|
27
|
-
|
|
28
|
-
interface ObjectPlugin<T> {
|
|
29
|
-
name?: string
|
|
30
|
-
enforce?: 'pre' | 'default' | 'post'
|
|
31
|
-
dependsOn?: string[]
|
|
32
|
-
order?: number
|
|
33
|
-
parallel?: boolean
|
|
34
|
-
setup?: Plugin<T>
|
|
35
|
-
hooks?: Partial<RuntimeNuxtHooks>
|
|
36
|
-
env?: {
|
|
37
|
-
islands?: boolean
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Parameters
|
|
43
|
-
|
|
44
|
-
**plugin**: A plugin can be defined in two ways:
|
|
45
|
-
1. **Function Plugin**: A function that receives the [`NuxtApp`](/docs/4.x/guide/going-further/internals#the-nuxtapp-interface) instance and can return a promise with an potential object with a [`provide`](/docs/4.x/guide/directory-structure/plugins#providing-helpers) property if you want to provide a helper on [`NuxtApp`](/docs/4.x/guide/going-further/internals#the-nuxtapp-interface) instance.
|
|
46
|
-
2. **Object Plugin**: An object that can include various properties to configure the plugin's behavior, such as `name`, `enforce`, `dependsOn`, `order`, `parallel`, `setup`, `hooks`, and `env`.
|
|
47
|
-
|
|
48
|
-
| Property | Type | Required | Description |
|
|
49
|
-
| ------------------ | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
|
|
50
|
-
| `name` | `string` | `false` | Optional name for the plugin, useful for debugging and dependency management. |
|
|
51
|
-
| `enforce` | `'pre'` \| `'default'` \| `'post'` | `false` | Controls when the plugin runs relative to other plugins. |
|
|
52
|
-
| `dependsOn` | `string[]` | `false` | Array of plugin names this plugin depends on. Ensures proper execution order. |
|
|
53
|
-
| `order` | `number` | `false` | This allows more granular control over plugin order and should only be used by advanced users. **It overrides the value of `enforce` and is used to sort plugins.** |
|
|
54
|
-
| `parallel` | `boolean` | `false` | Whether to execute the plugin in parallel with other parallel plugins. |
|
|
55
|
-
| `setup` | `Plugin<T>`{lang="ts"} | `false` | The main plugin function, equivalent to a function plugin. |
|
|
56
|
-
| `hooks` | `Partial<RuntimeNuxtHooks>`{lang="ts"} | `false` | Nuxt app runtime hooks to register directly. |
|
|
57
|
-
| `env` | `{ islands?: boolean }`{lang="ts"} | `false` | Set this value to `false` if you don't want the plugin to run when rendering server-only or island components. |
|
|
58
|
-
|
|
59
|
-
:video-accordion{title="Watch a video from Alexander Lichter about the Object Syntax for Nuxt plugins" videoId="2aXZyXB1QGQ"}
|
|
60
|
-
|
|
61
|
-
## Examples
|
|
62
|
-
|
|
63
|
-
### Basic Usage
|
|
64
|
-
|
|
65
|
-
The example below demonstrates a simple plugin that adds global functionality:
|
|
66
|
-
|
|
67
|
-
```ts twoslash [plugins/hello.ts]
|
|
68
|
-
export default defineNuxtPlugin((nuxtApp) => {
|
|
69
|
-
// Add a global method
|
|
70
|
-
return {
|
|
71
|
-
provide: {
|
|
72
|
-
hello: (name: string) => `Hello ${name}!`,
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### Object Syntax Plugin
|
|
79
|
-
|
|
80
|
-
The example below shows the object syntax with advanced configuration:
|
|
81
|
-
|
|
82
|
-
```ts twoslash [plugins/advanced.ts]
|
|
83
|
-
export default defineNuxtPlugin({
|
|
84
|
-
name: 'my-plugin',
|
|
85
|
-
enforce: 'pre',
|
|
86
|
-
async setup (nuxtApp) {
|
|
87
|
-
// Plugin setup logic
|
|
88
|
-
const data = await $fetch('/api/config')
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
provide: {
|
|
92
|
-
config: data,
|
|
93
|
-
},
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
hooks: {
|
|
97
|
-
'app:created' () {
|
|
98
|
-
console.log('App created!')
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
})
|
|
102
|
-
```
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "nuxt analyze"
|
|
3
|
-
description: "Analyze the production bundle or your Nuxt application."
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/cli/blob/main/packages/nuxi/src/commands/analyze.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
<!--analyze-cmd-->
|
|
12
|
-
```bash [Terminal]
|
|
13
|
-
npx nuxt analyze [ROOTDIR] [--cwd=<directory>] [--logLevel=<silent|info|verbose>] [--dotenv] [-e, --extends=<layer-name>] [--name=<name>] [--no-serve]
|
|
14
|
-
```
|
|
15
|
-
<!--/analyze-cmd-->
|
|
16
|
-
|
|
17
|
-
The `analyze` command builds Nuxt and analyzes the production bundle (experimental).
|
|
18
|
-
|
|
19
|
-
## Arguments
|
|
20
|
-
|
|
21
|
-
<!--analyze-args-->
|
|
22
|
-
Argument | Description
|
|
23
|
-
--- | ---
|
|
24
|
-
`ROOTDIR="."` | Specifies the working directory (default: `.`)
|
|
25
|
-
<!--/analyze-args-->
|
|
26
|
-
|
|
27
|
-
## Options
|
|
28
|
-
|
|
29
|
-
<!--analyze-opts-->
|
|
30
|
-
Option | Default | Description
|
|
31
|
-
--- | --- | ---
|
|
32
|
-
`--cwd=<directory>` | | Specify the working directory, this takes precedence over ROOTDIR (default: `.`)
|
|
33
|
-
`--logLevel=<silent\|info\|verbose>` | | Specify build-time log level
|
|
34
|
-
`--dotenv` | | Path to `.env` file to load, relative to the root directory
|
|
35
|
-
`-e, --extends=<layer-name>` | | Extend from a Nuxt layer
|
|
36
|
-
`--name=<name>` | `default` | Name of the analysis
|
|
37
|
-
`--no-serve` | | Skip serving the analysis results
|
|
38
|
-
<!--/analyze-opts-->
|
|
39
|
-
|
|
40
|
-
::note
|
|
41
|
-
This command sets `process.env.NODE_ENV` to `production`.
|
|
42
|
-
::
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: 'nuxt build-module'
|
|
3
|
-
description: 'Nuxt command to build your Nuxt module before publishing.'
|
|
4
|
-
links:
|
|
5
|
-
- label: Source
|
|
6
|
-
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/module-builder/blob/main/src/cli.ts
|
|
8
|
-
size: xs
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
<!--build-module-cmd-->
|
|
12
|
-
```bash [Terminal]
|
|
13
|
-
npx nuxt build-module [ROOTDIR] [--cwd=<directory>] [--logLevel=<silent|info|verbose>] [--build] [--stub] [--sourcemap] [--prepare]
|
|
14
|
-
```
|
|
15
|
-
<!--/build-module-cmd-->
|
|
16
|
-
|
|
17
|
-
The `build-module` command runs `@nuxt/module-builder` to generate `dist` directory within your `rootDir` that contains the full build for your **nuxt-module**.
|
|
18
|
-
|
|
19
|
-
## Arguments
|
|
20
|
-
|
|
21
|
-
<!--build-module-args-->
|
|
22
|
-
Argument | Description
|
|
23
|
-
--- | ---
|
|
24
|
-
`ROOTDIR="."` | Specifies the working directory (default: `.`)
|
|
25
|
-
<!--/build-module-args-->
|
|
26
|
-
|
|
27
|
-
## Options
|
|
28
|
-
|
|
29
|
-
<!--build-module-opts-->
|
|
30
|
-
Option | Default | Description
|
|
31
|
-
--- | --- | ---
|
|
32
|
-
`--cwd=<directory>` | | Specify the working directory, this takes precedence over ROOTDIR (default: `.`)
|
|
33
|
-
`--logLevel=<silent\|info\|verbose>` | | Specify build-time log level
|
|
34
|
-
`--build` | `false` | Build module for distribution
|
|
35
|
-
`--stub` | `false` | Stub dist instead of actually building it for development
|
|
36
|
-
`--sourcemap` | `false` | Generate sourcemaps
|
|
37
|
-
`--prepare` | `false` | Prepare module for local development
|
|
38
|
-
<!--/build-module-opts-->
|
|
39
|
-
|
|
40
|
-
::read-more{to="https://github.com/nuxt/module-builder" icon="i-simple-icons-github" target="\_blank"}
|
|
41
|
-
Read more about `@nuxt/module-builder`.
|
|
42
|
-
::
|