Open selfagency opened 3 years ago
@selfagency Thanks for this. I'll have a look.
Ok, I just discovered that, once pushed to Vercel, no async data being fed into nuxt.config.js
is working at all and that the issue is not limited to the sitemap.
Switched from Axios to Ky and it's the same deal.
Ok, I did something crazy: I created an initializer script that fetches my config from my API, appends it to process.env
and then spawns nuxt
in a child process. Again: It works locally, but once you push it to Vercel, nada.
I haven't yet looked into this, but more generally, it's bad practice for serverless to do network requests on function startup. It will dramatically increase your cold start, and even if we identify the underlying issue here I would recommend against it.
So there are a couple of things I noticed as I continued troubleshooting this yesterday:
The first is that @nuxtjs/sitemaps
only works as server middleware when running in dev mode and only works in production when the target is static
and the build method is generate
. This, of course, builds a static sitemap during Nuxt's build step, and does not serve middleware, as the only way server middleware works in Nuxt is when the target is server
and you build
. I do not believe this is how the module is intended to function, but nonetheless, if you use build
with the target as server
, the sitemap middleware simply does not work at all. In the end, I wound up writing my own server middleware to generate a sitemap.
The other issue I noticed is that if you make the nuxt.config.js
export into a function, once pushed to Vercel, server middleware doesn't load at all, even if you're running build
to server
. My other custom server middleware to generate an RSS feed wouldn't load at all until I switched my config back from a function to a standard Javascript object. Every other aspect of my nuxt.config.js
worked just fine, however.
With regards to the network requests on function startup, the initial API request I had in my nuxt.config.js
was only made during the build step, which then — as far as I understand — places the resolved environment variables into the generated code. Ergo, it should not have an impact on lambda startup times.
Whatever the case may be, I wound up having to a) not use functions in my nuxt.config.js
and b) drop the @nuxtjs/sitemap
module all together in order to get my site working properly again. These issues run counter to the documentation for both Nuxt and @nuxtjs/sitemap
.
@selfagency, I have a solution:
"builds": [ { "src": "nuxt.config.js", "use": "@nuxtjs/vercel-builder", "config": { "serverFiles": [".nuxt/dist/sitemap-routes.json"] } ]
Just add .nuxt/dist/sitemap-routes.json
to your vercel.json and it will be work
@VSKut Can confirm that this works here too. Thanks!
Ok, so let's say you have a very rudimentary blog:
Post index Live example
Individual post Live example
If you click the live examples above, you can see Axios is working properly to retrieve data from the
jsonplaceholder.typicode.com
URLs on the live site.Now you want to use
@nuxtjs/sitemap
to generate a sitemap. Everything works great locally with every single one of the following configurations. But when you push to Vercel, no matter what approach you take, the async data always comes back blank and the sitemap won't generate.config 1: async routes
full code
local result: ✅ vercel result: ❌
error:
config 2: async nuxt.config.js
full code
local result: ✅ vercel result: ❌
error:
config 3: module w/ custom server middleware
full code
local result: ✅ vercel result: ❌
error:
config 4: generate static sitemap w/o middleware
full code
local result: ✅ vercel result: ❌
error: