Features
- Add
redirected
key in the result ofnuxt.renderRoute(route, context = {})
, useful for unit tests - Add
redirect
field in thecontext
received indata (context)
andfetch (context)
, useful for authenticated routes:
// Ex: dashboard.vue
<script>
export default {
fetch ({ redirect, route }) {
// Check if user is connected via req.session or JWT...
redirect({
path: '/login',
query: { redirect: route.fullPath }
})
}
}
</script>
You can also use redirect('/login')
if you don't want to add any query parameter.
The redirect also works for nuxt generate
and will create a html page with:
<meta http-equiv="refresh" content="0; url={{ redirectedPath }}" />
<script type="text/javascript">
window.location.href = '{{ redirectedPath }}'
</script>