@nuxt/docs 3.20.1 → 3.21.0
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 +5 -5
- package/1.getting-started/02.installation.md +4 -7
- package/1.getting-started/03.configuration.md +32 -32
- package/1.getting-started/04.views.md +10 -8
- package/1.getting-started/05.assets.md +8 -8
- package/1.getting-started/06.styling.md +24 -18
- package/1.getting-started/07.routing.md +11 -11
- package/1.getting-started/08.seo-meta.md +10 -6
- package/1.getting-started/09.transitions.md +12 -12
- package/1.getting-started/10.data-fetching.md +18 -18
- package/1.getting-started/11.state-management.md +7 -7
- package/1.getting-started/12.error-handling.md +28 -22
- package/1.getting-started/13.server.md +9 -9
- package/1.getting-started/14.layers.md +52 -18
- package/1.getting-started/15.prerendering.md +10 -4
- package/1.getting-started/16.deployment.md +3 -2
- package/1.getting-started/17.testing.md +18 -8
- package/1.getting-started/18.upgrade.md +111 -60
- 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 +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.assets.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.components.md +13 -9
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.composables.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.content.md +2 -2
- package/2.directory-structure/1.layers.md +87 -0
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.layouts.md +37 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.middleware.md +8 -8
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.modules.md +10 -4
- 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.pages.md +43 -22
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.plugins.md +6 -3
- 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 +29 -9
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.shared.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.utils.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.env.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtignore.md +2 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtrc.md +6 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.app-config.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.app.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.error.md +10 -8
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.nuxt-config.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.package.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.tsconfig.md +3 -2
- package/2.directory-structure/index.md +65 -0
- package/3.guide/0.index.md +28 -0
- package/{2.guide/2.concepts/3.rendering.md → 3.guide/1.concepts/1.rendering.md} +8 -33
- package/{2.guide/2.concepts/2.vuejs-development.md → 3.guide/1.concepts/10.vuejs-development.md} +11 -10
- package/{2.guide/2.concepts/10.nuxt-lifecycle.md → 3.guide/1.concepts/2.nuxt-lifecycle.md} +34 -27
- package/{2.guide/2.concepts/1.auto-imports.md → 3.guide/1.concepts/3.auto-imports.md} +10 -10
- 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 +15 -2
- package/{2.guide/2.concepts → 3.guide/1.concepts}/7.esm.md +5 -4
- package/{2.guide/2.concepts → 3.guide/1.concepts}/8.typescript.md +11 -17
- 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 +5 -5
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +277 -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 +330 -0
- package/3.guide/4.modules/4.recipes-advanced.md +243 -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 +4 -4
- package/{2.guide/4.recipes → 3.guide/5.recipes}/2.vite-plugin.md +5 -1
- package/{2.guide/4.recipes → 3.guide/5.recipes}/3.custom-usefetch.md +3 -3
- package/{2.guide/4.recipes → 3.guide/5.recipes}/4.sessions-and-authentication.md +2 -2
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.events.md +3 -4
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.experimental-features.md +212 -87
- package/3.guide/6.going-further/1.features.md +108 -0
- 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 +3 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/11.nightly-release-channel.md +1 -1
- package/{2.guide/3.going-further → 3.guide/6.going-further}/2.hooks.md +7 -7
- package/{2.guide/3.going-further → 3.guide/6.going-further}/4.kit.md +3 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/6.nuxt-app.md +6 -6
- package/{2.guide/3.going-further → 3.guide/6.going-further}/7.layers.md +40 -14
- package/{2.guide/3.going-further → 3.guide/6.going-further}/9.debugging.md +3 -2
- package/{3.api → 4.api}/1.components/1.nuxt-client-fallback.md +5 -1
- package/{3.api → 4.api}/1.components/10.nuxt-picture.md +1 -1
- package/{3.api → 4.api}/1.components/11.teleports.md +2 -2
- 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 +4 -4
- package/{3.api → 4.api}/1.components/3.nuxt-layout.md +7 -7
- package/{3.api → 4.api}/1.components/4.nuxt-link.md +13 -13
- package/{3.api → 4.api}/1.components/5.nuxt-loading-indicator.md +2 -2
- package/{3.api → 4.api}/1.components/6.nuxt-error-boundary.md +2 -2
- package/{3.api → 4.api}/1.components/7.nuxt-welcome.md +2 -2
- package/{3.api → 4.api}/1.components/8.nuxt-island.md +9 -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 +5 -5
- package/4.api/2.composables/use-cookie.md +183 -0
- package/{3.api → 4.api}/2.composables/use-error.md +3 -3
- package/{3.api → 4.api}/2.composables/use-fetch.md +37 -37
- package/{3.api → 4.api}/2.composables/use-head-safe.md +1 -1
- package/{3.api → 4.api}/2.composables/use-head.md +22 -7
- package/{3.api → 4.api}/2.composables/use-lazy-async-data.md +1 -1
- package/{3.api → 4.api}/2.composables/use-lazy-fetch.md +9 -9
- package/{3.api → 4.api}/2.composables/use-nuxt-app.md +11 -9
- 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 +5 -5
- package/{3.api → 4.api}/2.composables/use-seo-meta.md +2 -2
- package/{3.api → 4.api}/2.composables/use-server-seo-meta.md +2 -2
- package/{3.api → 4.api}/2.composables/use-state.md +12 -2
- package/{3.api → 4.api}/3.utils/$fetch.md +2 -2
- 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 +2 -4
- package/{3.api → 4.api}/3.utils/clear-error.md +1 -1
- package/{3.api → 4.api}/3.utils/create-error.md +7 -7
- package/{3.api → 4.api}/3.utils/define-lazy-hydration-component.md +5 -5
- package/{3.api → 4.api}/3.utils/define-nuxt-component.md +1 -1
- package/{3.api → 4.api}/3.utils/define-nuxt-plugin.md +12 -12
- package/{3.api → 4.api}/3.utils/define-nuxt-route-middleware.md +3 -3
- package/{3.api → 4.api}/3.utils/define-page-meta.md +23 -16
- package/{3.api → 4.api}/3.utils/define-route-rules.md +2 -2
- package/{3.api → 4.api}/3.utils/navigate-to.md +14 -14
- 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/preload-route-components.md +2 -2
- package/{3.api → 4.api}/3.utils/refresh-cookie.md +0 -2
- package/{3.api → 4.api}/3.utils/refresh-nuxt-data.md +1 -1
- package/{3.api → 4.api}/3.utils/reload-nuxt-app.md +1 -1
- package/{3.api → 4.api}/3.utils/set-page-layout.md +36 -0
- package/{3.api → 4.api}/3.utils/set-response-status.md +3 -3
- package/{3.api → 4.api}/3.utils/show-error.md +4 -4
- 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 +31 -18
- package/{3.api → 4.api}/5.kit/10.templates.md +23 -23
- package/{3.api → 4.api}/5.kit/11.nitro.md +36 -36
- package/{3.api → 4.api}/5.kit/12.resolving.md +2 -2
- package/{3.api → 4.api}/5.kit/14.builder.md +35 -23
- package/{3.api → 4.api}/5.kit/16.layers.md +16 -16
- package/{3.api → 4.api}/5.kit/2.programmatic.md +2 -2
- package/{3.api → 4.api}/5.kit/3.compatibility.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/{3.api → 4.api}/5.kit/6.context.md +1 -1
- package/{3.api → 4.api}/5.kit/8.layout.md +1 -1
- package/4.api/6.advanced/1.hooks.md +105 -0
- package/{3.api → 4.api}/index.md +7 -7
- package/5.community/4.contribution.md +10 -10
- package/5.community/5.framework-contribution.md +9 -9
- package/5.community/6.roadmap.md +25 -25
- package/5.community/7.changelog.md +20 -0
- package/6.bridge/1.overview.md +9 -1
- package/6.bridge/3.bridge-composition-api.md +2 -2
- package/6.bridge/4.plugins-and-middleware.md +4 -4
- package/6.bridge/5.nuxt3-compatible-api.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +6 -6
- package/7.migration/20.module-authors.md +3 -3
- package/7.migration/3.auto-imports.md +1 -1
- package/7.migration/4.meta.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +3 -3
- package/7.migration/6.pages-and-layouts.md +9 -9
- package/7.migration/7.component-options.md +6 -6
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
- package/2.guide/0.index.md +0 -25
- package/2.guide/3.going-further/1.features.md +0 -103
- 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-router.md +0 -94
- 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}/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}/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/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-hydration.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-loading-indicator.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-nuxt-data.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-fetch.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-runtime-hook.md +0 -0
- /package/{3.api → 4.api}/3.utils/.navigation.yml +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/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/prerender-routes.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/13.logging.md +0 -0
- /package/{3.api → 4.api}/5.kit/15.examples.md +0 -0
- /package/{3.api → 4.api}/5.kit/7.pages.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}/6.nuxt-config.md +0 -0
|
@@ -0,0 +1,183 @@
|
|
|
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/3.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/3.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://datatracker.ietf.org/doc/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://datatracker.ietf.org/doc/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://datatracker.ietf.org/doc/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://datatracker.ietf.org/doc/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://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4). By default, the path is considered the ["default path"](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4). |
|
|
71
|
+
| `sameSite` | `boolean \| string` | `undefined` | Sets the [`SameSite` `Set-Cookie` attribute](https://datatracker.ietf.org/doc/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.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/3.x/examples/advanced/use-cookie"}
|
|
@@ -22,8 +22,8 @@ You can use this composable in your components, pages, or plugins to access or r
|
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
interface NuxtError<DataT = unknown> {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
status: number
|
|
26
|
+
statusText: string
|
|
27
27
|
message: string
|
|
28
28
|
data?: DataT
|
|
29
29
|
error?: true
|
|
@@ -52,4 +52,4 @@ if (error.value) {
|
|
|
52
52
|
</script>
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
:read-more{to="/docs/getting-started/error-handling"}
|
|
55
|
+
:read-more{to="/docs/3.x/getting-started/error-handling"}
|
|
@@ -25,8 +25,8 @@ const { data, status, error, refresh, clear } = await useFetch('/api/modules', {
|
|
|
25
25
|
</script>
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
::warning
|
|
29
|
-
If you're using a custom useFetch wrapper, do not await it in the composable
|
|
28
|
+
::warning{to="/docs/3.x/guide/recipes/custom-usefetch#custom-usefetchuseasyncdata"}
|
|
29
|
+
If you're using a custom `useFetch` wrapper, do not await it in the composable as that can cause unexpected behavior. See recipe for custom async data fetcher.
|
|
30
30
|
::
|
|
31
31
|
|
|
32
32
|
::note
|
|
@@ -96,11 +96,11 @@ If you encounter the `data` variable destructured from a `useFetch` returns a st
|
|
|
96
96
|
|
|
97
97
|
:video-accordion{title="Watch the video from Alexander Lichter to avoid using useFetch the wrong way" videoId="njsGVmcWviY"}
|
|
98
98
|
|
|
99
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
99
|
+
:read-more{to="/docs/3.x/getting-started/data-fetching"}
|
|
100
100
|
|
|
101
101
|
### Reactive Fetch Options
|
|
102
102
|
|
|
103
|
-
Fetch options can be provided as reactive, supporting `computed`, `ref` and [computed getters](https://vuejs.org/guide/essentials/computed
|
|
103
|
+
Fetch options can be provided as reactive, supporting `computed`, `ref` and [computed getters](https://vuejs.org/guide/essentials/computed). When a reactive fetch option is updated it will trigger a refetch using the updated resolved reactive value.
|
|
104
104
|
|
|
105
105
|
```ts
|
|
106
106
|
const searchQuery = ref('initial')
|
|
@@ -183,29 +183,29 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
183
183
|
|
|
184
184
|
- `options` (object): Configuration for the fetch request. Extends [unjs/ofetch](https://github.com/unjs/ofetch) options and [`AsyncDataOptions`](/docs/3.x/api/composables/use-async-data#params). All options can be a static value, a `ref`, or a computed value.
|
|
185
185
|
|
|
186
|
-
| Option
|
|
187
|
-
|
|
188
|
-
| `key`
|
|
189
|
-
| `method`
|
|
190
|
-
| `query`
|
|
191
|
-
| `params`
|
|
192
|
-
| `body`
|
|
193
|
-
| `headers`
|
|
194
|
-
| `baseURL`
|
|
195
|
-
| `timeout`
|
|
196
|
-
| `cache`
|
|
197
|
-
| `server`
|
|
198
|
-
| `lazy`
|
|
199
|
-
| `immediate`
|
|
200
|
-
| `default`
|
|
201
|
-
| `timeout`
|
|
202
|
-
| `transform`
|
|
203
|
-
| `getCachedData
|
|
204
|
-
| `pick`
|
|
205
|
-
| `watch`
|
|
206
|
-
| `deep`
|
|
207
|
-
| `dedupe`
|
|
208
|
-
| `$fetch`
|
|
186
|
+
| Option | Type | Default | Description |
|
|
187
|
+
|-----------------|-------------------------------------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------|
|
|
188
|
+
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from URL and options. |
|
|
189
|
+
| `method` | `MaybeRefOrGetter<string>` | `'GET'` | HTTP request method. |
|
|
190
|
+
| `query` | `MaybeRefOrGetter<SearchParams>` | - | Query/search params to append to the URL. Alias: `params`. |
|
|
191
|
+
| `params` | `MaybeRefOrGetter<SearchParams>` | - | Alias for `query`. |
|
|
192
|
+
| `body` | `MaybeRefOrGetter<RequestInit['body'] \| Record<string, any>>` | - | Request body. Objects are automatically stringified. |
|
|
193
|
+
| `headers` | `MaybeRefOrGetter<Record<string, string> \| [key, value][] \| Headers>` | - | Request headers. |
|
|
194
|
+
| `baseURL` | `MaybeRefOrGetter<string>` | - | Base URL for the request. |
|
|
195
|
+
| `timeout` | `MaybeRefOrGetter<number>` | - | Timeout in milliseconds to abort the request. |
|
|
196
|
+
| `cache` | `boolean \| string` | - | Cache control. Boolean disables cache, or use Fetch API values: `default`, `no-store`, etc. |
|
|
197
|
+
| `server` | `boolean` | `true` | Whether to fetch on the server. |
|
|
198
|
+
| `lazy` | `boolean` | `false` | If true, resolves after route loads (does not block navigation). |
|
|
199
|
+
| `immediate` | `boolean` | `true` | If false, prevents request from firing immediately. |
|
|
200
|
+
| `default` | `() => DataT` | - | Factory for default value of `data` before async resolves. |
|
|
201
|
+
| `timeout` | `number` | - | A number in milliseconds to wait before timing out the request (defaults to `undefined`, which means no timeout) |
|
|
202
|
+
| `transform` | `(input: DataT) => DataT \| Promise<DataT>` | - | Function to transform the result after resolving. |
|
|
203
|
+
| `getCachedData` | `(key, nuxtApp, ctx) => DataT \| undefined` | - | Function to return cached data. See below for default. |
|
|
204
|
+
| `pick` | `string[]` | - | Only pick specified keys from the result. |
|
|
205
|
+
| `watch` | `MultiWatchSources \| false` | - | Array of reactive sources to watch and auto-refresh. `false` disables watching. |
|
|
206
|
+
| `deep` | `boolean` | `false` | Return data in a deep ref object. |
|
|
207
|
+
| `dedupe` | `'cancel' \| 'defer'` | `'cancel'` | Avoid fetching same key more than once at a time. |
|
|
208
|
+
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. See [Custom useFetch in Nuxt](/docs/3.x/guide/recipes/custom-usefetch) |
|
|
209
209
|
|
|
210
210
|
::note
|
|
211
211
|
All fetch options can be given a `computed` or `ref` value. These will be watched and new requests made automatically with any new values if they are updated.
|
|
@@ -222,14 +222,14 @@ This only caches data when `experimental.payloadExtraction` in `nuxt.config` is
|
|
|
222
222
|
|
|
223
223
|
## Return Values
|
|
224
224
|
|
|
225
|
-
| Name
|
|
226
|
-
|
|
227
|
-
| `data`
|
|
228
|
-
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data. By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
|
229
|
-
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`.
|
|
230
|
-
| `error`
|
|
231
|
-
| `status`
|
|
232
|
-
| `clear`
|
|
225
|
+
| Name | Type | Description |
|
|
226
|
+
|-----------|-----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
227
|
+
| `data` | `Ref<DataT \| undefined>` | The result of the asynchronous fetch. |
|
|
228
|
+
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data. By default, Nuxt waits until a `refresh` is finished before it can be executed again. |
|
|
229
|
+
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
230
|
+
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
231
|
+
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. See below for possible values. |
|
|
232
|
+
| `clear` | `() => void` | Resets `data` to `undefined` (or the value of `options.default()` if provided), `error` to `undefined`, set `status` to `idle`, and cancels any pending requests. |
|
|
233
233
|
|
|
234
234
|
### Status values
|
|
235
235
|
|
|
@@ -244,6 +244,6 @@ If you have not fetched data on the server (for example, with `server: false`),
|
|
|
244
244
|
|
|
245
245
|
### Examples
|
|
246
246
|
|
|
247
|
-
:link-example{to="/docs/examples/advanced/use-custom-fetch-composable"}
|
|
247
|
+
:link-example{to="/docs/3.x/examples/advanced/use-custom-fetch-composable"}
|
|
248
248
|
|
|
249
|
-
:link-example{to="/docs/examples/features/data-fetching"}
|
|
249
|
+
:link-example{to="/docs/3.x/examples/features/data-fetching"}
|
|
@@ -50,7 +50,7 @@ This composable does not return any value.
|
|
|
50
50
|
|
|
51
51
|
## Example
|
|
52
52
|
|
|
53
|
-
```vue [
|
|
53
|
+
```vue [pages/user-profile.vue]
|
|
54
54
|
<script setup lang="ts">
|
|
55
55
|
// User-generated content that might contain malicious code
|
|
56
56
|
const userBio = ref('<script>alert("xss")<' + '/script>')
|
|
@@ -12,7 +12,7 @@ links:
|
|
|
12
12
|
|
|
13
13
|
The `useHead` composable allows you to manage your head tags in a programmatic and reactive way, powered by [Unhead](https://unhead.unjs.io). It lets you customize the meta tags, links, scripts, and other elements in the `<head>` section of your HTML document.
|
|
14
14
|
|
|
15
|
-
```vue [app
|
|
15
|
+
```vue [app.vue]
|
|
16
16
|
<script setup lang="ts">
|
|
17
17
|
useHead({
|
|
18
18
|
title: 'My App',
|
|
@@ -38,7 +38,7 @@ The properties of `useHead` can be dynamic, accepting `ref`, `computed` and `rea
|
|
|
38
38
|
## Type
|
|
39
39
|
|
|
40
40
|
```ts [Signature]
|
|
41
|
-
export function useHead (meta: MaybeComputedRef<MetaObject>):
|
|
41
|
+
export function useHead (meta: MaybeComputedRef<MetaObject>): ActiveHeadEntry<UseHeadInput>
|
|
42
42
|
|
|
43
43
|
interface MetaObject {
|
|
44
44
|
title?: string
|
|
@@ -52,6 +52,21 @@ interface MetaObject {
|
|
|
52
52
|
htmlAttrs?: HtmlAttributes
|
|
53
53
|
bodyAttrs?: BodyAttributes
|
|
54
54
|
}
|
|
55
|
+
|
|
56
|
+
interface ActiveHeadEntry<Input> {
|
|
57
|
+
/**
|
|
58
|
+
* Updates the entry with new input.
|
|
59
|
+
*
|
|
60
|
+
* Will first clear any side effects for previous input.
|
|
61
|
+
*/
|
|
62
|
+
patch: (input: Input) => void
|
|
63
|
+
/**
|
|
64
|
+
* Dispose the entry, removing it from the active head.
|
|
65
|
+
*
|
|
66
|
+
* Will queue side effects for removal.
|
|
67
|
+
*/
|
|
68
|
+
dispose: () => void
|
|
69
|
+
}
|
|
55
70
|
```
|
|
56
71
|
|
|
57
72
|
See [@unhead/schema](https://github.com/unjs/unhead/blob/main/packages/vue/src/types/schema.ts) for more detailed types.
|
|
@@ -81,7 +96,7 @@ This composable does not return any value. It registers the head metadata with U
|
|
|
81
96
|
|
|
82
97
|
### Basic Meta Tags
|
|
83
98
|
|
|
84
|
-
```vue [
|
|
99
|
+
```vue [pages/about.vue]
|
|
85
100
|
<script setup lang="ts">
|
|
86
101
|
useHead({
|
|
87
102
|
title: 'About Us',
|
|
@@ -96,7 +111,7 @@ useHead({
|
|
|
96
111
|
|
|
97
112
|
### Reactive Meta Tags
|
|
98
113
|
|
|
99
|
-
```vue [
|
|
114
|
+
```vue [pages/profile.vue]
|
|
100
115
|
<script setup lang="ts">
|
|
101
116
|
const profile = ref({ name: 'John Doe' })
|
|
102
117
|
|
|
@@ -114,7 +129,7 @@ useHead({
|
|
|
114
129
|
|
|
115
130
|
### Using a Function for Full Reactivity
|
|
116
131
|
|
|
117
|
-
```vue [
|
|
132
|
+
```vue [pages/dynamic.vue]
|
|
118
133
|
<script setup lang="ts">
|
|
119
134
|
const count = ref(0)
|
|
120
135
|
|
|
@@ -129,7 +144,7 @@ useHead(() => ({
|
|
|
129
144
|
|
|
130
145
|
### Adding External Scripts and Styles
|
|
131
146
|
|
|
132
|
-
```vue [
|
|
147
|
+
```vue [pages/external.vue]
|
|
133
148
|
<script setup lang="ts">
|
|
134
149
|
useHead({
|
|
135
150
|
link: [
|
|
@@ -150,7 +165,7 @@ useHead({
|
|
|
150
165
|
|
|
151
166
|
### Body and HTML Attributes
|
|
152
167
|
|
|
153
|
-
```vue [
|
|
168
|
+
```vue [pages/themed.vue]
|
|
154
169
|
<script setup lang="ts">
|
|
155
170
|
const isDark = ref(true)
|
|
156
171
|
|
|
@@ -16,7 +16,7 @@ By default, [`useAsyncData`](/docs/3.x/api/composables/use-async-data) blocks na
|
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
|
-
```vue [
|
|
19
|
+
```vue [pages/index.vue]
|
|
20
20
|
<script setup lang="ts">
|
|
21
21
|
const { status, data: posts } = await useLazyAsyncData('posts', () => $fetch('/api/posts'))
|
|
22
22
|
</script>
|
|
@@ -14,7 +14,7 @@ links:
|
|
|
14
14
|
|
|
15
15
|
By default, [`useFetch`](/docs/3.x/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` allows navigation to proceed immediately, with data being fetched in the background.
|
|
16
16
|
|
|
17
|
-
```vue [
|
|
17
|
+
```vue [pages/index.vue]
|
|
18
18
|
<script setup lang="ts">
|
|
19
19
|
const { status, data: posts } = await useLazyFetch('/api/posts')
|
|
20
20
|
</script>
|
|
@@ -69,14 +69,14 @@ export function useLazyFetch<DataT, ErrorT> (
|
|
|
69
69
|
|
|
70
70
|
Returns the same `AsyncData` object as [`useFetch`](/docs/3.x/api/composables/use-fetch):
|
|
71
71
|
|
|
72
|
-
| Name
|
|
73
|
-
|
|
74
|
-
| `data`
|
|
75
|
-
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data.
|
|
76
|
-
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`.
|
|
77
|
-
| `error`
|
|
78
|
-
| `status`
|
|
79
|
-
| `clear`
|
|
72
|
+
| Name | Type | Description |
|
|
73
|
+
|-----------|-----------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
|
|
74
|
+
| `data` | `Ref<DataT \| undefined>` | The result of the asynchronous fetch. |
|
|
75
|
+
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data. |
|
|
76
|
+
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
77
|
+
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
78
|
+
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. |
|
|
79
|
+
| `clear` | `() => void` | Resets `data` to `undefined`, `error` to `undefined`, sets `status` to `idle`, and cancels any pending requests. |
|
|
80
80
|
|
|
81
81
|
:read-more{to="/docs/3.x/api/composables/use-fetch#return-values"}
|
|
82
82
|
|
|
@@ -16,7 +16,7 @@ const nuxtApp = useNuxtApp()
|
|
|
16
16
|
</script>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
19
|
+
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](/docs/3.x/api/composables/use-nuxt-app#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
20
20
|
|
|
21
21
|
<!--
|
|
22
22
|
note
|
|
@@ -30,7 +30,7 @@ By default, the shared runtime context of Nuxt is namespaced under the [`buildId
|
|
|
30
30
|
|
|
31
31
|
### `provide (name, value)`
|
|
32
32
|
|
|
33
|
-
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/3.x/
|
|
33
|
+
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/3.x/directory-structure/plugins). Use the `provide` function to create Nuxt plugins to make values and helper methods available in your Nuxt application across all composables and components.
|
|
34
34
|
|
|
35
35
|
`provide` function accepts `name` and `value` parameters.
|
|
36
36
|
|
|
@@ -46,7 +46,7 @@ As you can see in the example above, `$hello` has become the new and custom part
|
|
|
46
46
|
|
|
47
47
|
### `hook(name, cb)`
|
|
48
48
|
|
|
49
|
-
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/3.x/
|
|
49
|
+
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/3.x/directory-structure/plugins) to hook into the rendering lifecycle.
|
|
50
50
|
|
|
51
51
|
`hook` function is useful for adding custom logic by hooking into the rendering lifecycle at a specific point. `hook` function is mostly used when creating Nuxt plugins.
|
|
52
52
|
|
|
@@ -80,12 +80,12 @@ await nuxtApp.callHook('my-plugin:init')
|
|
|
80
80
|
|
|
81
81
|
### `vueApp`
|
|
82
82
|
|
|
83
|
-
`vueApp` is the global Vue.js [application instance](https://vuejs.org/api/application
|
|
83
|
+
`vueApp` is the global Vue.js [application instance](https://vuejs.org/api/application#application-api) that you can access through `nuxtApp`.
|
|
84
84
|
|
|
85
85
|
Some useful methods:
|
|
86
|
-
- [`component()`](https://vuejs.org/api/application
|
|
87
|
-
- [`directive()`](https://vuejs.org/api/application
|
|
88
|
-
- [`use()`](https://vuejs.org/api/application
|
|
86
|
+
- [`component()`](https://vuejs.org/api/application#app-component) - Registers a global component if passing both a name string and a component definition, or retrieves an already registered one if only the name is passed.
|
|
87
|
+
- [`directive()`](https://vuejs.org/api/application#app-directive) - Registers a global custom directive if passing both a name string and a directive definition, or retrieves an already registered one if only the name is passed[(example)](/docs/3.x/directory-structure/plugins#vue-directives).
|
|
88
|
+
- [`use()`](https://vuejs.org/api/application#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins)** [(example)](/docs/3.x/directory-structure/plugins#vue-plugins).
|
|
89
89
|
|
|
90
90
|
:read-more{icon="i-simple-icons-vuedotjs" to="https://vuejs.org/api/application.html#application-api"}
|
|
91
91
|
|
|
@@ -120,7 +120,7 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
120
120
|
|
|
121
121
|
After fetching the value of `count` using [`useAsyncData`](/docs/3.x/api/composables/use-async-data) in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there.
|
|
122
122
|
|
|
123
|
-
When accessing the same `payload.data` from [`ssrcontext`](#ssrcontext), you can access the same value on the server side as well.
|
|
123
|
+
When accessing the same `payload.data` from [`ssrcontext`](/docs/3.x/api/composables/use-nuxt-app#ssrcontext), you can access the same value on the server side as well.
|
|
124
124
|
|
|
125
125
|
- `state` (object) - When you use [`useState`](/docs/3.x/api/composables/use-state) composable in Nuxt to set shared state, this state data is accessed through `payload.state.[name-of-your-state]`.
|
|
126
126
|
|
|
@@ -136,6 +136,8 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
136
136
|
|
|
137
137
|
It is also possible to use more advanced types, such as `ref`, `reactive`, `shallowRef`, `shallowReactive` and `NuxtError`.
|
|
138
138
|
|
|
139
|
+
#### Custom Reducer/Reviver
|
|
140
|
+
|
|
139
141
|
Since [Nuxt v3.4](https://nuxt.com/blog/v3-4#payload-enhancements), it is possible to define your own reducer/reviver for types that are not supported by Nuxt.
|
|
140
142
|
|
|
141
143
|
:video-accordion{title="Watch a video from Alexander Lichter about serializing payloads, especially with regards to classes" videoId="8w6ffRBs8a4"}
|
|
@@ -268,7 +270,7 @@ Using a new experimental feature, it is possible to enable native async context
|
|
|
268
270
|
Native async context support works currently in Bun and Node.
|
|
269
271
|
::
|
|
270
272
|
|
|
271
|
-
:read-more{to="/docs/guide/going-further/experimental-features#asynccontext"}
|
|
273
|
+
:read-more{to="/docs/3.x/guide/going-further/experimental-features#asynccontext"}
|
|
272
274
|
|
|
273
275
|
## tryUseNuxtApp
|
|
274
276
|
|
|
@@ -37,7 +37,7 @@ header.value = 'my-value'
|
|
|
37
37
|
</template>
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/3.x/
|
|
40
|
+
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/3.x/directory-structure/middleware) to set a response header for all pages.
|
|
41
41
|
|
|
42
42
|
```ts [middleware/my-header-middleware.ts]
|
|
43
43
|
export default defineNuxtRouteMiddleware((to, from) => {
|
|
@@ -76,7 +76,7 @@ The `useRoute()` composable should only be used in the setup function of a Vue c
|
|
|
76
76
|
This applies to any composable that uses `useRoute()` internally too.
|
|
77
77
|
::
|
|
78
78
|
|
|
79
|
-
::read-more{to="/docs/3.x/
|
|
79
|
+
::read-more{to="/docs/3.x/directory-structure/middleware"}
|
|
80
80
|
Read more about accessing the route in the middleware section.
|
|
81
81
|
::
|
|
82
82
|
|
|
@@ -84,4 +84,4 @@ Read more about accessing the route in the middleware section.
|
|
|
84
84
|
|
|
85
85
|
Browsers don't send [URL fragments](https://url.spec.whatwg.org/#concept-url-fragment) (for example `#foo`) when making requests. So using `route.fullPath` to affect the template can trigger hydration issues because this will include the fragment on client but not the server.
|
|
86
86
|
|
|
87
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/
|
|
87
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/routelocationnormalizedloaded"}
|