forked from nuxt/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt3): add middleware via route meta (nuxt#2858)
- Loading branch information
Showing
32 changed files
with
399 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
**/node_modules | ||
|
||
docs/content/index.md | ||
docs/content/**/15.nuxt.config.md | ||
docs/content/**/*.nuxt.config.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
schema | ||
15.nuxt.config.md | ||
*.nuxt.config.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
icon: IconDirectory | ||
title: 'middleware' | ||
head.title: Middleware directory | ||
--- | ||
|
||
# Middleware directory | ||
|
||
Nuxt provides a customizable **route middleware** framework you can use throughout your application, ideal for extracting code that you want to run before navigating to a particular route. | ||
|
||
::alert{type=info} | ||
Route middleware run within the Vue part of your Nuxt app. Despite the similar name, they are completely different from server middleware, which are run in the Nitro server part of your app. | ||
:: | ||
|
||
There are three kinds of route middleware: | ||
|
||
1. Anonymous (or inline) route middleware, which are defined directly in the pages where they are used. | ||
2. Named route middleware, which are placed in the `middleware/` directory and will be automatically loaded via asynchronous import when used on a page. | ||
3. Global route middleware, which are placed in the `middleware/` directory (with a `.global` suffix) and will be automatically run on every route change. | ||
|
||
The first two kinds of route middleware can be [defined in `definePageMeta`](/docs/directory-structure/pages). | ||
|
||
## Format | ||
|
||
Route middleware are navigation guards that receive the current route and the next route as arguments. | ||
|
||
```js | ||
export default defineNuxtRouteMiddleware((to, from) => { | ||
if (to.params.id === '1') { | ||
return abortNavigation() | ||
} | ||
return navigateTo('/') | ||
}) | ||
``` | ||
|
||
Nuxt provides two globally available helpers that can be returned directly from the middleware: | ||
|
||
1. `navigateTo (route: string | Route)` - Redirects to the given route, within plugins or middleware. It can also be called directly on client side to perform a page navigation. | ||
2. `abortNavigation (err?: string | Error)` - Aborts the navigation, with an optional error message. | ||
|
||
Unlike, navigation guards in [the vue-router docs](https://next.router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards), a third `next()` argument is not passed, and redirects or route cancellation is handled by returning a value from the middleware. Possible return values are: | ||
|
||
* nothing - does not block navigation and will move to the next middleware function, if any, or complete the route navigation | ||
* `navigateTo('/')` or `navigateTo({ path: '/' })` - redirects to the given path | ||
* `abortNavigation()` - stops the current navigation | ||
* `abortNavigation(error)` - rejects the current navigation with an error | ||
|
||
::alert{type=warning} | ||
It is advised to use the helper functions above for performing redirects or stopping navigation. Other possible return values described in [the vue-router docs](https://next.router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) may work but there may be breaking changes in future. | ||
:: | ||
|
||
## Adding middleware dynamically | ||
|
||
It is possible to add global or named route middleware manually using the `addRouteMiddleware()` helper function, such as from within a plugin. | ||
|
||
```ts | ||
export default defineNuxtPlugin(() => { | ||
addRouteMiddleware('global-test', () => { | ||
console.log('this global middleware was added in a plugin and will be run on every route change') | ||
}, { global: true }) | ||
|
||
addRouteMiddleware('named-test', () => { | ||
console.log('this named middleware was added in a plugin and would override any existing middleware of the same name') | ||
}) | ||
}) | ||
``` | ||
|
||
## Example: a named route middleware | ||
|
||
```bash | ||
-| middleware/ | ||
---| auth.ts | ||
``` | ||
|
||
In your page file, you can reference this route middleware | ||
|
||
```vue | ||
<script setup> | ||
definePageMeta({ | ||
middleware: ["auth"] | ||
// or middleware: 'auth' | ||
}) | ||
</script> | ||
``` | ||
|
||
Now, before navigation to that page can complete, the `auth` route middleware will be run. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<script setup lang="ts"> | ||
const route = useRoute() | ||
</script> | ||
|
||
<template> | ||
<NuxtExampleLayout example="with-middleware"> | ||
<NuxtPage /> | ||
|
||
<template #nav> | ||
<nav class="flex align-center gap-4 p-4"> | ||
<NuxtLink to="/" class="n-link-base"> | ||
Home | ||
</NuxtLink> | ||
<NuxtLink to="/forbidden" class="n-link-base"> | ||
Forbidden | ||
</NuxtLink> | ||
<NuxtLink to="/redirect" class="n-link-base"> | ||
Redirect | ||
</NuxtLink> | ||
</nav> | ||
</template> | ||
|
||
<template #footer> | ||
<div class="text-center p-4 op-50"> | ||
Current route: <code>{{ route.path }}</code> | ||
</div> | ||
</template> | ||
</NuxtExampleLayout> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default defineNuxtRouteMiddleware(() => { | ||
console.log('running global middleware') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default defineNuxtRouteMiddleware((to) => { | ||
const { $config } = useNuxtApp() | ||
if ($config) { | ||
console.log('Accessed runtime config within middleware.') | ||
} | ||
console.log('Heading to', to.path, 'but I think we should go somewhere else...') | ||
return '/secret' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineNuxtConfig } from 'nuxt3' | ||
|
||
export default defineNuxtConfig({ | ||
modules: [ | ||
'@nuxt/ui' | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "example-with-middleware", | ||
"private": true, | ||
"scripts": { | ||
"build": "nuxi build", | ||
"dev": "nuxi dev", | ||
"start": "nuxi preview" | ||
}, | ||
"devDependencies": { | ||
"@nuxt/ui": "npm:@nuxt/ui-edge@latest", | ||
"nuxt3": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div> | ||
Forbidden | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
definePageMeta({ | ||
// This is an example of inline middleware | ||
middleware: () => { | ||
console.log('Strictly forbidden.') | ||
return false | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<div> | ||
Home | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<template> | ||
<div> | ||
You should never see this page | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
definePageMeta({ | ||
middleware: 'redirect-me' | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<template> | ||
<div> | ||
You've landed on a page that wasn't in the menu! | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
definePageMeta({ | ||
middleware: 'named-test' | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default defineNuxtPlugin(() => { | ||
addRouteMiddleware('global-test', () => { | ||
console.log('this global middleware was added in a plugin') | ||
}, { global: true }) | ||
|
||
addRouteMiddleware('named-test', () => { | ||
console.log('this named middleware was added in a plugin') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./.nuxt/tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.