nuxt-prepare
nuxt-prepare
Initialization steps at build time, like nuxtServerInit
Nuxt Prepare
Turn API responses into typed imports when Nuxt builds.
Features
- 🦦 Zero configuration
- 🦎 Import build-time data from
#nuxt-prepare, typed with its literal values - ✂️ Conditionally overwrite runtime config or app config values
- 🍡 Run scripts in series or parallel
- 🥢 Ready for Nitro server routes
- 🦾 Typed handlers with
defineNuxtPrepareHandler
Setup
npx nuxt module add prepareBasic Usage
By default, Nuxt Prepare looks for a server.prepare.ts file in your project root. To run synchronous or asynchronous code when Nuxt builds your app, create it and default-export the result of defineNuxtPrepareHandler:
// `server.prepare.ts`
import { defineNuxtPrepareHandler } from 'nuxt-prepare/config'
export default defineNuxtPrepareHandler(async () => {
// Fetch data from an API, read a file, query a database …
return {
// Import `foo` anywhere from `#nuxt-prepare`
data: {
foo: 'bar'
},
// Overwrite the runtime config variable `foo`
runtimeConfig: {
public: {
foo: 'Overwritten by "server.prepare" script'
}
}
}
})The documentation covers the handler API, prepare data and every module option.
💻 Development
- Clone this repository
- Enable Corepack using
corepack enable - Install dependencies using
pnpm install - Run
pnpm run dev:prepare - Start development server using
pnpm run dev
Credits
- Konkon for his logo pixel art.
- OmarMcAdam for his inspirational
nuxt-server-initmodule.
License
MIT License © 2023-PRESENT Johann Schopplich
