Skip to content

allow providing routes to generate asynchronously #13939

@TotomInc

Description

@TotomInc

Environment


  • Operating System: Darwin
  • Node Version: v16.15.0
  • Nuxt Version: 3.0.0-rc.1
  • Package Manager: yarn@1.22.18
  • Builder: vite
  • User Config: target, generate
  • Runtime Modules: -
  • Build Modules: -

Reproduction

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  target: "static",

  generate: {
    // TypeScript complain about `any[]` not assignable to `Promise<any[]>` because of await.
    // @ts-ignore
    async routes() {
      const productsRoutes = await fetch("http://localhost:1337/api/products")
        .then((response) => response.json() as Promise<StrapiResponse<ProductResponse[]>>)
        .then(({ data }) => data.map((product) => /products/${product.attributes.slug}`));

      return [productsRoutes];
    }
  }
})

Describe the bug

I am unable to use async/await for generate.routes in nuxt.config.ts, like provided in the developer documentation of the Nuxt framework: https://github.com/nuxt/framework/blob/main/packages/schema/src/config/generate.ts#L15-L70

As explained with the code above, I am trying to add dynamic routes for the SSG (npx nuxi generate).

I want to create a route for each product that is referenced in my back-end, so I used the generate.route function with an async/await, as explained in the documentation.

However, when I am trying to run npx nuxi generate, the following error is happening:

$ nuxt generate
Nuxt CLI v3.0.0-rc.2

 ERROR  nuxt.options.generate.routes is not iterable

  at initNitro (node_modules/nuxt/dist/index.mjs:1177:81)
  at async initNuxt (node_modules/nuxt/dist/index.mjs:1367:3)
  at async loadNuxt (node_modules/nuxt/dist/index.mjs:1384:5)
  at async loadNuxt (node_modules/@nuxt/kit/dist/index.mjs:657:19)
  at async Object.invoke (node_modules/nuxi/dist/chunks/build.mjs:28:18)
  at async Object.invoke (node_modules/nuxi/dist/chunks/generate.mjs:27:5)
  at async _main (node_modules/nuxi/dist/cli.mjs:46:20)

error Command failed with exit code 1.
  • Is the documentation incorrect about the usage of async/await as a generate.route function?
  • What are the possible alternatives?
  • The return type of generate.routes is also incorrect if the usage of async/await is allowed. It should be Promise<any[]> | any[] instead of any[].

Please, let me know if you need additional context.

Additional context

The error seems to come from this line: https://github.com/nuxt/framework/blob/main/packages/nuxt/src/core/nitro.ts#L55

Where nuxt.options.generate.routes can potentially be a function instead of an array.

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xnitroNitro server engine integration✨ enhancementNew feature or improvement to existing functionality🍰 p2-nice-to-haveNice to have, not blocking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions