NuxtN
Nuxt7mo ago
20 replies
Alwe

Add public assets at build time

Hi, i'm creating a local module for my project, and I'm stuck on adding public assets with the nitro:config hook.
I did this:
nuxt.hook('nitro:config', (nitroConfig) => {
  nitroConfig.publicAssets ||= []
  nitroConfig.publicAssets.push({
    baseURL: 'images',
    dir: presetResolve('images'),
    maxAge: 60 * 60 * 24 * 365,
  })
  nitroConfig.publicAssets.push({
    baseURL: 'icons',
    dir: presetResolve('icons'),
    maxAge: 60 * 60 * 24 * 365,
  })
})


and when I start the dev server all works fine. The problem comes when I try to build, as nuxt tries to find the images/icons I use in my page routes but since they are added "after", it throws this error:

[nuxi]  ERROR  Nuxt Build Error: [vite]: Rollup failed to resolve import "/images/logo_brand.png" from "/app/app/pages/<MY_PAGE>/index.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
build.rollupOptions.external
Was this page helpful?