0

I'm using Nuxt v2.15.8 to create a static website using nuxt generate command. I expect the requests to the server for fetching the main js and CSS files to be made to / path, as I specified in my nuxt.cofig:

export default {
  ssr: false,
  target: 'static',
  // ...
  build: {
    publicPath: process.env.NODE_ENV === 'development' ? '/_nuxt/' : '/',
  },
  // ...
}

When I visit the generated static website, despite what I specified in the build config, at first it tries to load data from /_nuxt/ path which does not exist. As you can see in the attached pictures, after getting a 404 error for this route, it loads data successfully from the correct path which is /, but it should not send those requests to /_nuxt path to begin with.

Request to the wrong path enter image description here

Request to the correct path enter image description here

Any idea about how can I eliminate those initial requests to /_nuxt/?

mo3n
  • 1,522
  • 2
  • 10
  • 34
  • Hm, I never faced this issue before. What kind of data are you trying to load here? Also, what happens if you `yarn generate && yarn start` the project, is it still buggy with an untouched `publicPath`? – kissu Aug 08 '22 at 13:12
  • @kissu I'm trying to load the main css and js files generated by nuxt when I run `yarn generate` command. The result is the same if I `yarn generate && yarn start` the project. And when publicPath is untouched, these js and css files are placed inside **_nuxt** directory, which I do not want. – mo3n Aug 08 '22 at 14:35
  • 1
    What do you want to do with those files **after** the build? Seems quite a strange usage overall. – kissu Aug 08 '22 at 16:42
  • I just use the files generated in the 'dist' directory and place them on my servers root directory – mo3n Aug 08 '22 at 17:30
  • You should use git for that. And I'm not sure why this wouldn't work tbh. Why the need to change the path? – kissu Aug 08 '22 at 19:03
  • My client insist on it – mo3n Aug 09 '22 at 08:15

1 Answers1

0

So I fixed the issue by adding the following lines to nuxt.config.js:

export default {
  ssr: false,
  target: 'static',
  // ...
  build: {
    publicPath: process.env.NODE_ENV === 'development' ? '/_nuxt/' : '/',
  },
  // added the following lines
  hooks: {
    render: {
      resourcesLoaded(resources) {
        const path = `/`
        resources.clientManifest && (resources.clientManifest.publicPath = path)
        resources.modernManifest && (resources.modernManifest.publicPath = path)
        resources.serverManifest && (resources.serverManifest.publicPath = path)
      }
    }
  },
  // ...

}
mo3n
  • 1,522
  • 2
  • 10
  • 34