Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fb0ac02
docs(api): add useNuxtApp composable docs
Krutie Aug 19, 2022
a36996e
docs(api): fix linting issues
Krutie Aug 19, 2022
ad78b00
Merge branch 'nuxt:main' into docs/api-composable-use-nuxt-app
Krutie Aug 21, 2022
1474134
fix(docs): codeblocks
Krutie Aug 22, 2022
97e2f34
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
4f67c03
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
f7189f2
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
6166eec
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
be86ba0
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
bad40ac
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
4603101
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
1e35140
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
7e5a3ba
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
82e91f8
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
72af002
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
bf2d2be
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
483d779
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
decaa0c
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
4ac796e
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
3deed14
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
9ea602c
Update docs/content/3.api/1.composables/use-nuxt-app.md
pi0 Aug 22, 2022
51b8e08
Apply suggestions from code review
pi0 Aug 22, 2022
307510d
Merge branch 'main' into docs/api-composable-use-nuxt-app
pi0 Aug 22, 2022
d17bc1d
remove globalName from docs
pi0 Aug 22, 2022
83b8ad3
updates
pi0 Aug 22, 2022
059abaa
refactor ordering
pi0 Aug 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 223 additions & 2 deletions docs/content/3.api/1.composables/use-nuxt-app.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,225 @@
# `useNuxtApp`

::NeedContribution
::
`useNuxtApp` is a built-in composable that provides a shared runtime context. This runtime context is available on both client-side and server-side and it helps you access the Vue instance, runtime hooks, runtime config variables and internal states, such as `ssrContext` and `payload`.
Comment thread
pi0 marked this conversation as resolved.
Outdated

You can use `useNuxtApp` within composable, plugins and components.
Comment thread
pi0 marked this conversation as resolved.
Outdated

```jsx
// app.vue
<script setup>
const nuxtApp = useNuxtApp();
Comment thread
pi0 marked this conversation as resolved.
Outdated
</script>
```
Comment thread
pi0 marked this conversation as resolved.
Outdated

`useNuxtApp` will expose the following properties that you can use to extend and customize your app and share state, data and variables.
Comment thread
pi0 marked this conversation as resolved.
Outdated

## globalName

**`type: string`**

`globalName` allows you to customize the global ID used in the main HTML template as well as the main Vue instance name and other options.
Comment thread
pi0 marked this conversation as resolved.
Outdated

The default value of `globalName` is set to `nuxt`, but you can customize it using `nuxtApp.globalName`.

### Example

```jsx [app.vue]
<script setup>
const nuxtApp = useNuxtApp();
Comment thread
pi0 marked this conversation as resolved.
Outdated
console.log(nuxtApp.globalName) // nuxt
</script>
```

## isHydrating

**`type: boolean`**

You can use `nuxtApp.isHydrating` to check if the Nuxt app is hydrating on the client side.

### Example

```tsx [components/nuxt-error-boundary.ts]

Comment thread
pi0 marked this conversation as resolved.
export default defineComponent({
setup (_props, { slots, emit }) {
// ...
const nuxtApp = useNuxtApp()
onErrorCaptured((err) => {
if (process.client && !nuxtApp.isHydrating) {
emit('error', err)
error.value = err
return false
}
})
// ...
}
})
```

## ssrContext

`ssrContext` is generated using runtime `vue-bundle-renderer` for Vue 3 and it is only available on the server-side. Nuxt exposes the following properties through `ssrContext`.
Comment thread
pi0 marked this conversation as resolved.
Outdated

- **url** - `type: string` - `url` refers to the host url where the current request is being made
- **event** - `type: HTTP request` - `event` allows access to `res` and `res` object for the current request made to Nuxt page
Comment thread
pi0 marked this conversation as resolved.
Outdated
- **runtimeConfig** - `type: runtimeConfig object` - `runtimeConfig` exposes runtime config on the client-side via payload
Comment thread
pi0 marked this conversation as resolved.
Outdated
- **noSSR** - `type: boolean`
- **error** - `type: error object` - returns server-side error if any
- **payload** - returns NuxtApp payload object - See [payload](#payload) section below for more detail.
- **renderMeta** - `type: NuxtMeta` - returns a `Promise` of `NuxtMeta` type that includes HTML, head and body attributes as well as head tags.
Comment thread
pi0 marked this conversation as resolved.
Outdated
- **teleports** - `type: Record object` - renders content outside of the Vue application

> See how the [Vue-meta plugin](https://github.com/nuxt/framework/blob/main/packages/nuxt/src/head/runtime/lib/vue-meta.plugin.ts#L23) on Nuxt uses `renderMeta` in combination with `teleports`.
Comment thread
pi0 marked this conversation as resolved.
Outdated

## vueApp

`vueApp` is the global Vue application instance that you can access through `nuxtApp`.

- **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.
- **config** - exposes aΒ `config` object that contains the configuration settings for that application.
Comment thread
pi0 marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **config** - exposes aΒ `config` object that contains the configuration settings for that application.
- **config** - Exposes aΒ `config` object that contains the configuration settings for that application.

- **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.

> See `directive` example: [https://v3.nuxtjs.org/guide/directory-structure/plugins#vue-directives](https://v3.nuxtjs.org/guide/directory-structure/plugins#vue-directives)
Comment thread
pi0 marked this conversation as resolved.
Outdated

- **mixins** - Applies a global `mixin` (scoped to the application). However, it is not recommended to use mixins with the Vue 3. Their sole purpose is only to provide backward compatibility.
Comment thread
pi0 marked this conversation as resolved.
Outdated
- **mount** - Mounts the application instance in a container element.
- **provide** - Provide a value that can be injected in all descendent components within the application.
Comment thread
pi0 marked this conversation as resolved.
Outdated
- **unmount** - Unmounts a mounted application instance, triggering the unmount lifecycle hooks for all components in the application's component tree.
- **use** - Installs aΒ **[plugin](https://vuejs.org/guide/reusability/plugins.html)**.

> See `use` example: [https://v3.nuxtjs.org/guide/directory-structure/plugins#vue-plugins](https://v3.nuxtjs.org/guide/directory-structure/plugins#vue-plugins)
Comment thread
pi0 marked this conversation as resolved.
Outdated

:ReadMore{link="https://vuejs.org/api/application.html#application-api"}

## $router

**`type: router object`**

You can access the Vue router object using `nuxtApp.$router`.

:ReadMore{link="/api/composables/use-router"}

## provide - fn

`nuxtApp` is a runtime context that you can extend usingΒ [Nuxt Plugins](https://v3.nuxtjs.org/guide/directory-structure/plugins). You can use the `provide` function to create Nuxt plugins to make values and helper methods available in your Nuxt application across all composable and components.
Comment thread
pi0 marked this conversation as resolved.
Outdated

`provide` function accepts `name` and `value` parameters.

### Example

```jsx
const nuxtApp = useNuxtApp()
nuxtApp.provide('hello', (name) => `Hello ${name}!`)

// Prints "Hello name!"
console.log(nuxtApp.$hello('name'))
```

As you can see in the example above, `$hello` has become the new and custom part of `nuxtApp` context and it is available at all places where `nuxtApp` is accessible.
Comment thread
pi0 marked this conversation as resolved.
Outdated

## State and variables

### $config

`nuxtApp.$config` exposes runtime-config and environment variables on the client-side to the Nuxt app context through two keys: `app` and `public`.
Comment thread
pi0 marked this conversation as resolved.
Outdated

Keys found under `app` and `public` are available on the client-side.
Comment thread
pi0 marked this conversation as resolved.
Outdated

- **app:**
- **baseURL** - `type: string`
- **buildAssetsDir** - `type: string`
- **cdnURL** - `type: string`
- **public** - `nuxtApp.$config.public` allows you to access the public runtime config that is set in the `nuxt.config` file of your Nuxt app.

> Same `$config` object is accessible through `nuxtApp.payload.config` as well.
Comment thread
pi0 marked this conversation as resolved.
Outdated

### payload

`payload` exposes data and state variables from server-side to client-side and make them available in the `window.NUXT` object that is accessible from the browser.
Comment thread
pi0 marked this conversation as resolved.
Outdated

`payload` exposes following keys on client-side after they are stringified and passed from the server-side:
Comment thread
pi0 marked this conversation as resolved.
Outdated

- **serverRendered** - `type: boolean`
- **data** - `type: record object` - when you fetch the data from an API endpoint using either `useFetch` or `useAsyncData`, resulting payload can be accessed from the `payload.data` . This data is cached and helps you prevent fetching the same data in case an identical request is made twice.
Comment thread
pi0 marked this conversation as resolved.
Outdated

```js [app.vue]
export default defineComponent({
async setup() {
const { data } = await useAsyncData('count', () => $fetch('/api/count'))
}
})
```

After fetching the value of `count` using `useAsyncData` in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there. And the value `count` is updated accordingly as the page count increases.
Comment thread
pi0 marked this conversation as resolved.
Outdated

When accessing the same `payload.data` from [ssrcontext](#ssrcontext), you can access the same value on the server-side as well.
Comment thread
pi0 marked this conversation as resolved.
Outdated

- **state** - `type: record object` - when you use `useState` composable in Nuxt to set shared state, this state data is accessed through payload.state.[name-of-your-state]
Comment thread
pi0 marked this conversation as resolved.
Outdated

```js [plugins/my-plugin.ts]
export const useColor = () => useState<string>('color', () => 'pink')

export default defineNuxtPlugin((nuxtApp) => {
if (process.server) {
const color = useColor()
}
})
```

- **config** - Same as the [$config](#config) section above.

## Runtime `nuxtApp` hooks

Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue composable and [Nuxt Plugins](/guide/directory-structure/plugins) to hook into the rendering lifecycle.
Comment thread
pi0 marked this conversation as resolved.
Outdated

### hooks

`nuxtApp` lets you call and add the following runtime hooks:

```tsx
// App related runtime hooks
'app:created': (app: App<Element>) => HookResult
'app:beforeMount': (app: App<Element>) => HookResult
'app:mounted': (app: App<Element>) => HookResult
'app:rendered': (ctx: AppRenderedContext) => HookResult
'app:redirected': () => HookResult
'app:suspense:resolve': (Component?: VNode) => HookResult
'app:error': (err: any) => HookResult
'app:error:cleared': (options: { redirect?: string }) => HookResult
'app:data:refresh': (keys?: string[]) => HookResult

// Nuxt page related runtime hooks
'page:start': (Component?: VNode) => HookResult
'page:finish': (Component?: VNode) => HookResult
'meta:register': (metaRenderers: Array<(nuxt: NuxtApp) => NuxtMeta | Promise<NuxtMeta>>) => HookResult

// Vue instance related runtime hooks
'vue:setup': () => void
'vue:error': (...args: Parameters<Parameters<typeof onErrorCaptured>[0]>) => HookResult
```

### hook - `type: function`

`hook` function is useful for adding custom logic by hooking into the rendering lifecycle at a specific point. `hook` function is mostly used in creating Nuxt plugins.
Comment thread
pi0 marked this conversation as resolved.
Outdated

```jsx [plugins/test.ts]
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('page:start', () => {
/* your code goes here */
})
nuxtApp.hook('vue:error', (..._args) => {
console.log('vue:error')
Comment thread
pi0 marked this conversation as resolved.
Outdated
// if (process.client) {
// console.log(..._args)
// }
})
})
```

### callhook - `type: function`

`callHook` returns promise when called with any of the existing hooks.
Comment thread
pi0 marked this conversation as resolved.
Outdated

```jsx
await nuxtApp.callHook('app:created', vueApp)
```