Nuxt module to run async initialization steps at build-time.
defineNuxtPrepareHandlernpx nuxi@latest module add prepare
Add the nuxt-prepare module to your nuxt.config.ts:
// `nuxt.config.ts`
export default defineNuxtConfig({
modules: ['nuxt-prepare']
})
By default, Nuxt Prepare will look for a server.prepare.ts file in your project root. To run synchronous or asynchronous code when Nuxt builds your app, define a handler in your project root and export a default function:
// `server.prepare.ts`
import { defineNuxtPrepareHandler } from 'nuxt-prepare/config'
export default defineNuxtPrepareHandler(async () => {
// Do some async magic here, e.g. fetch data from an API
return {
// Overwrite the runtime config variable `foo`
runtimeConfig: {
public: {
foo: 'Overwritten by "server.prepare" script'
}
},
// Pass custom state to Nuxt and import it
// anywhere from `#nuxt-prepare`
state: {
foo: 'bar'
}
}
})
!TIP You can run as many prepare scripts as you want – add them to the
prepare.scriptsmodule configuration.
corepack enablepnpm installpnpm run dev:preparepnpm run devnuxt-server-init module.MIT License © 2023-PRESENT Johann Schopplich