question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complex runtimeConfig values become `{}` inside the app

See original GitHub issue

Environment

Nuxi 3.0.0-rc.12                                                                                     14:16:13
RootDir: /home/projects/nuxt-runtimeconfig-complex-objects                                           14:16:14
Nuxt project info:                                                                                   14:16:14

------------------------------
- Operating System: `Linux`
- Node Version:     `v16.14.2`
- Nuxt Version:     `3.0.0-rc.12`
- Nitro Version:    `0.6.0`
- Package Manager:  `npm@7.17.0`
- Builder:          `vite`
- User Config:      `runtimeConfig`
- Runtime Modules:  `-`
- Build Modules:    `-`
------------------------------

Reproduction

https://stackblitz.com/edit/nuxt-runtimeconfig-complex-objects?file=nuxt.config.ts,app.vue

Describe the bug

When passing a complex object into the runtime config it comes out as an empty object ({}) on the other side.

  1. When passing in:
import redisDriver from 'unstorage/drivers/redis';

const driver = redisDriver();
console.log('driver should be: ', driver);

export default defineNuxtConfig({
  runtimeConfig: {
    driver,
  },
});

we get:

driver should be:  {                                                                                 14:18:56
  hasItem: [Function: hasItem],
  getItem: [Function: getItem],
  setItem: [Function: setItem],
  removeItem: [Function: removeItem],
  getKeys: [Function: getKeys],
  clear: [AsyncFunction: clear],
  dispose: [Function: dispose]
}
   
  1. And then inspecting driver inside the app.vue:
<template>
  <div>Checkout the console.logs</div>
</template>

<script setup lang="ts">
if (process.server) {
  const config = useRuntimeConfig();
  console.log('driver is: ', config.driver);
}
</script>

we get:

driver is:  {}

Here, I’d expect driver is: ... to say the same thing as driver should be: ...

Additional context

This first arose in this nuxt-session PR by @Twitch0125. I could imagine that this is due to some de-/serialization happening. I checked the current runtime config docs and couldn’t find anything about this happening. If some de-/serialization prevents us from using non-primitive config values, I have some ideas for improvements:

  1. document it in the runtime config docs
  2. use typescript to disallow config values that will not make it through “whole”

Thanks ❤️

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Oct 24, 2022

We don’t really want users to pass non-serialisable data from nuxt.config to the runtime part of the app as it blurs the boundary between configuration and runtime code.

As a module author, you can inject the nitro plugin yourself based on the user’s configuration of your module. (Or do various other things depending on your use case.) It’s difficult to say in the abstract, but feel free to drop me a line and I’ll happily assist.

1reaction
wobsorianocommented, Oct 31, 2022

@BracketJohn we also have a Nuxt integration for next still in private because of some upcoming Request/Response changes. And yes, we did not go with passing config via nuxt.config because of serialization.

Screen Shot 2022-10-30 at 11 19 34 PM

If you are interested in brining this into our monorepo, you can message https://twitter.com/balazsorban44

@danielroe is there any nitro plugin example related to this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deep-dive into .NET Core primitives, part 3: runtimeconfig.json ...
The file can be used to control settings which are not surfaced in Visual Studio, such as automatically running your app on higher...
Read more >
asp.net - Appropriate content for Myapp..runtimeconfig.json ...
1 (according nuget) with no problem. I am unable to figure the good values to set in this file to host a MVC...
Read more >
runtimeconfig.json contains incorrect framework version #686
Currently, a package reference to Microsoft.NETCore.App 1.0.3 produces runtimeconfig with 1.0.0, but should be 1.0.3. visibility in NuGet GUI in ...
Read more >
.NET Runtime config options - .NET | Microsoft Learn
Configuring an option in the runtimeconfig.json or project file applies the setting to that application only. Some configuration values can ...
Read more >
Providing Runtime configuration to an Angular Application
First of all, you will need to create a config file in JSON format under assets/ folder (we don't want this file to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found