Skip to content

Cannot stringify a function error during pre-rendering with script registry config in v0.12.0+ #702

@luc122c

Description

@luc122c

🐛 The bug

I'm getting a Cannot stringify a function error when building with @nuxt/scripts v0.12.0 or higher. This happens specifically when I have any script registry configured in nuxt.config.ts.
Steps to reproduce:

  1. Create a new Nuxt project
  2. Install @nuxt/scripts@0.12.0
  3. Add a script registry in nuxt.config.ts - for example:
  scripts: {
     registry: {
       cloudflareWebAnalytics: {
         token: "some-token-here",
         scriptOptions: { trigger: "client" }
       }
     }
   }
  1. Enable prerendering in nitro config
  2. Run npm run build

The build fails during prerendering with this error:

Cannot stringify a function
at walk (node_modules/devalue/src/uneval.js:51:11)
at uneval (node_modules/devalue/src/uneval.js:145:2)
at renderPayloadJsonScript (.nuxt/prerender/chunks/_/renderer.mjs:240:17)

This works fine in v0.11.13. I've tested 0.12.0, 0.12.1, 0.12.2 and they all fail. I'm using Nuxt 4.4.2 with cloudflare_module preset.

🛠️ To reproduce

https://stackblitz.com/edit/github-hm1injqv?file=nuxt.config.ts

🌈 Expected behavior

The build should complete successfully and prerender the routes, just like it does in v0.11.13. The script registry config should be supported in v0.12.0+ without causing serialization errors.
I couldn't find any documentation suggesting this config shouldn't work - the cloudflareWebAnalytics example in the docs shows exactly this setup with a token and scriptOptions. The config is valid and gets merged into runtimeConfig correctly (I can see it in the debug logs), but something in how it's processed later in the build pipeline ends up with a function reference that devalue can't serialize for SSR.

ℹ️ Additional context

I did some debugging to trace the issue. Here's what I found:

  1. What changed in v0.12.0: New defu config merging was added that merges the user's scripts.registry config into runtimeConfig.public.scripts, then passes it to the bundler as registryConfig
  2. The config flows through multiple merge steps:
    • User config → merged via defu → passed to bundler → merged again with function arguments
    • Somewhere in this chain, a function reference gets into the object
  3. Debug logs showed: The config itself only contains strings (token, scriptOptions.trigger), but the error happens in Nuxt's internal SSR payload generation - the function isn't visible at the config level I was debugging
  4. Why it's hard to trace: The error occurs in renderPayloadJsonScript during prerendering, which is Nuxt internals. My debug logging of the config didn't catch the function because it's added internally during the build.
    Key changes in 0.12.0 that likely cause this:
  • module.ts: defu merging of registry config into runtimeConfig.public.scripts
  • module.ts: Passing registryConfig to bundler transformer
  • composables/useScript.ts: Added _validate function and modified trigger handling that can assign onNuxtReady to options

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions