strapi icon indicating copy to clipboard operation
strapi copied to clipboard

Generating static output gives localhost adress

Open patandrick opened this issue 2 years ago • 0 comments

Hello dear devs!

I found a confusing behaviour of the strapi nuxt module.

I'v got this in my nuxt.config.ts :

...
  runtimeConfig: {
    strapi: {
      url: 'http://localhost:1337',
      prefix: '/api',
      version: 'v4'
    },
    public: {
      strapi: {
        url: 'https://api.domain.de'
      }
    }
  },
...

When I did:

npm run generate

> generate
> nuxt generate

Nuxt 3.8.1 with Nitro 2.7.2                                                                                                          10:29:42 AM
ℹ Strapi Admin URL: https://api.domain.de/admin                                                                          10:29:42 AM
ℹ Building client...                                                                                                                10:29:43 AM
ℹ vite v4.5.0 building for production...

the generated URL for strapi media was http://localhost:1337 but the generated static client does not have access to localhost obviously

I found that in useStrapiMedia.ts there is this line

https://github.com/nuxt-modules/strapi/blob/5d3994d4a6754ce0c7350513d9cc6b8bf541dbbb/src/runtime/composables/useStrapiMedia.ts#L5

I already changed the line to always use the public strapi config and it worked. Would it be possible to get the public URL for media in the generate task even if process.server returns true? Otherwise I must open the /api endpoint of strapi and close it afterwards everytime I generate the static client with the prerendered content.

Maybe one way to solve this could be to loop trough process.argv and lookup for 'generate' parameter. Another way may be to add extra configuration in runtime config and implement a mechanism that checks the parameter, e.g. "generated: true"

Any other suggestions?

patandrick avatar Jan 24 '24 09:01 patandrick