Nuxt/examples/routing/universal-router/plugins/add.ts
Clément Ollivier 1a7b570c82
docs(examples): add examples to docs (#3966)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
2022-03-30 17:59:28 +02:00

34 lines
796 B
TypeScript

export default defineNuxtPlugin(() => {
const timer = useState('timer', () => 0)
if (process.client) {
addRouteMiddleware(async () => {
console.log('Starting timer...')
timer.value = 5
do {
await new Promise(resolve => setTimeout(resolve, 100))
timer.value--
} while (timer.value)
console.log('...and navigating')
})
}
addRouteMiddleware((to) => {
if (to.path === '/forbidden') {
return false
}
})
addRouteMiddleware((to) => {
const { $config } = useNuxtApp()
if ($config) {
console.log('Accessed runtime config within middleware.')
}
if (to.path !== '/redirect') { return }
console.log('Heading to', to.path, 'but I think we should go somewhere else...')
return '/secret'
})
})