Static site generation with Nuxt.JS using relative paths

Viewed 397

I'm using Nuxt 2.15.8 to generate static pages (migrating to Nuxt 3 is also an option for me if it solves the problem).

It works great when deployed in the root folder of the server but I need it to be served in a subdirectory, like:

https://my.domain.com/folder/subfolder

The problem is that the compiled HTML includes nuxt related assets like:

/_nuxt/123456789.js

which translates to:

https://my.domain.com/_nuxt/123456789.js which obviously fails as the file is in a subfolder, not in the root.

I tried using publicPath config and absolute paths but it is not an option for me as I have several environments with different URLs.

I need to generate static HTML files with relative paths in order to make sure my site works as expected in all the environments, agnostically from the server URL.

I was able to achieve it using Vite + Vue 3 but migrating to a new implementation is not an option, I need to achieve it using the current Nuxt implementation.

I tried using nuxt-vite https://vite.nuxtjs.org/ but was not able to achieve relative paths, I still get /_nuxt/123456789.js instead of

./_nuxt/123456789.js

../_nuxt/123456789.js

../../_nuxt/123456789.js , etc

2 Answers

It seems like it's not supported in plain nuxt 2 but if you use nuxt-vite you can set vite.base to '' or './' in nuxt.config to make the paths relative.

Or just edit index.html manually...

Related