Skip to content

navigateTo doesn't return promise, thus cannot be awaited #1226

@evgenii-code

Description

@evgenii-code

Environment

  • Operating System: Linux
  • Node Version: v18.20.3
  • Nuxt Version: 2.17.3
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: npm@10.2.3
  • Builder: webpack
  • User Config: head, components, bridge, serverHandlers, devServerHandlers, devServer, typescript, nitro, buildModules
  • Runtime Modules: -
  • Build Modules: (), @nuxt/bridge@3.2.1

Reproduction

Nuxt Bridge reproduction
https://stackblitz.com/edit/nuxt-bridge-starter-nbqmvf?file=pages%2Findex.vue

Nuxt 3 reproduction
https://stackblitz.com/edit/nuxt-starter-me3k42?file=pages%2Findex.vue

Describe the bug

Utility helper navigateTo does not return a promise, thus cannot be awaited.
The method router.push has the same behaviour. You may notice, that the app crashes on router.push('/').then(() => {}), because there is no .then in undefined

In this Nuxt Bridge reproduction I have tried to add await to the navigateTo, and second alert fires synchronously.

But in Nuxt 3, await makes the difference and second alert fires after navigation complete.

A little bit of code

pages/index.vue

<template>
  <div>
    <button @click="navigateToTodos">navigateTo</button>
  </div>
</template>

<script lang="ts" setup>
const navigateToTodos = async () => {
  alert(1);

  await navigateTo('/todos');

  alert(3)
};
</script>

pages/todos/index.vue

<template>
  <div>
    Todos
  </div>
</template>

<script lang="ts" setup>
if (process.client) {
  alert(2);
}
</script>

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions