nitro icon indicating copy to clipboard operation
nitro copied to clipboard

hook "prerender:config" does not add routes for prerender

Open wokalek opened this issue 9 months ago • 2 comments

Environment

Nitro Version: 2.11.7 Node Version: v22.14.0 Package Manager: [email protected]

Reproduction

Have this setting in Nuxt:

import { prerenderConfigHook } from './app/config/nitro/hooks/prerender'
  nitro: {
    hooks: {
      'prerender:config': nitroConfig => prerenderConfigHook(nitroConfig),
    },
  },

hook:

import { $fetch } from 'ofetch'

import { mergeWith as useMergeWith, pick as usePick } from 'lodash-es'

import type { NitroConfig } from 'nitropack'

export async function prerenderConfigHook(nitroConfig: NitroConfig) {
  const config = await $fetch<{ routes: string[], ignore: string[] }>(`${process.env.API_URL}/api/app/nuxt/config/generate`)

  useMergeWith(nitroConfig.prerender, usePick(config, ['routes', 'ignore']), (objValue, srcValue) => {
    if (Array.isArray(objValue)) {
      return objValue.concat(srcValue)
    }
  })
}

But prerender for added routes doesn't work. Ignoring seems to work, but routes don't work at all...

Describe the bug

Changing nitroConfig.prerender.routes in hook prerender:config does not cause prerendering to work on added routes.

Additional context

No response

Logs


wokalek avatar Mar 28 '25 07:03 wokalek

You should use prerender:routes hook in order to extend pretender routes. config is a pre-initialization step.

pi0 avatar Mar 28 '25 09:03 pi0

@pi0 hmmm. How can I make it so that I can modify both adding routes and modifying the ignore config?

In the end, I decided to do it like this:

nuxt.config:

{
  hooks: {
    'nitro:config': nitroConfig => nitroConfigHook(nitroConfig),
  },
}
export function nitroConfigHook(nitroConfig: NitroConfig) {
  if (process.env.npm_lifecycle_event === 'generate') {
    return nitroConfigPrerenderHook(nitroConfig)
  }
}

nitroConfigPrerenderHook now is previous prerenderConfigHook.

And it works now!

But I'm not sure about this part: process.env.npm_lifecycle_event === 'generate'.

It looks strange. How can you determine in hooks that it is the generation process that is running? Or did I do everything correctly?

wokalek avatar Mar 28 '25 11:03 wokalek