storybook icon indicating copy to clipboard operation
storybook copied to clipboard

Cannot read properties of undefined (reading 'cdnURL')

Open AlexanderBredun opened this issue 1 year ago • 12 comments

I used this Nuxt Storybook starter, but when I try to add any image from the public directory, I get the following error: image When I checked the console, it pointed me to this part of the code and showed that there is a problem with useRuntimeConfig: image

Node 18.18.2, npm 9.8.1, windows 10

AlexanderBredun avatar Aug 14 '24 08:08 AlexanderBredun

@AlexanderBredun did you manage to find a workaround?

guim4dev avatar Aug 15 '24 20:08 guim4dev

@guim4dev no unfortunately

AlexanderBredun avatar Aug 19 '24 12:08 AlexanderBredun

To replicate, build the showcase app that has /pages in it:

pnpm dev:prepare & pnpm example:showcase:storybook:build

Go to the build folder (storybook/examples/showcase/storybook-static) and run an http server in it: npx http-server

Check the logs in the console.

obulat avatar Aug 23 '24 16:08 obulat

Thanks @obulat for the reproduction. I get the error there now as well. This only occurs in the static build, and not when looking in console for the dev build ( example:showcase:dev > Storybook), right?

That makes debugging this a bit harder...

tobiasdiez avatar Aug 23 '24 21:08 tobiasdiez

Thanks @obulat for the reproduction. I get the error there now as well. This only occurs in the static build, and not when looking in console for the dev build ( example:showcase:dev > Storybook), right?

That makes debugging this a bit harder...

I think that this is related to #761, and the errors there can be masking these errors. router is definitely part of the problem here.

obulat avatar Aug 24 '24 04:08 obulat

I have a similar problems. Occurs with the autogenerated Docs view when I have multiple exports in a stories file.

doppelmutzi avatar Aug 26 '24 15:08 doppelmutzi

adding some extra info here

@doppelmutzi I had the same problem. It only occurs in autogenerated docs with >1 stories. Single Story autogenerated docs work fine, its probably something related to routing as pointed out by @obulat

I disabled autodocs for now as multi stories are important for our use case and it works fine locally.

Sadly now im getting blocked by chromatic, as the built version of storybook also throws the cdnURL error :/

guim4dev avatar Aug 29 '24 18:08 guim4dev

Could this be a race condition? I set a breakpoint on line 2 in paths.mjs and then stepped into useRuntimeConfig():

// /.nuxt/nitro.client.mjs
export const useRuntimeConfig = () => 
window?.__NUXT__?.config || {}

In useRuntimeConfig() window.__NUXT__ was undefined.

However, when I later check whether this global variable is defined in the console I can see that it is:

Screenshot 2024-08-29 at 17 23 52

d-low avatar Aug 29 '24 21:08 d-low

@tobiasdiez Thank you for your work on this module 💚

It would be great to prioritise this issue. In my case I can't upgrade to Nuxt 3.13.x because of this error.

I will also try to investigate.

enkot avatar Sep 30 '24 10:09 enkot

Encountered this error with latest versions of nuxt and storybook. This helped resolve the issue (only for stories themselves, not docs):

vite: {
    resolve: {
      alias: {
        vue: 'vue/dist/vue.esm-bundler.js',
      },
    },
}

Come to think of it, this is probably related to other issue, but still might be useful to someone

Limonische avatar Oct 01 '24 11:10 Limonische

Thanks for looking into this. If it's indeed a problem with the runtime bundler of vue, then this should be fixed as part of https://github.com/nuxt-modules/storybook/pull/740 (which sadly is stuck in another issue).

tobiasdiez avatar Oct 01 '24 14:10 tobiasdiez

As a workaround, because we are not using cdn or changing the base URL, pnpm patch nuxt did the trick, for now:

diff --git a/dist/index.mjs b/dist/index.mjs
index 9fe137cf72916a0ceb3edbae12c9deacd960faed..625bb157338fbf10083760166600e518d81065c5 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -5119,7 +5119,8 @@ const publicPathTemplate = {
       "export const buildAssetsDir = () => appConfig.buildAssetsDir",
       "export const buildAssetsURL = (...path) => joinRelativeURL(publicAssetsURL(), buildAssetsDir(), ...path)",
       "export const publicAssetsURL = (...path) => {",
-      "  const publicBase = appConfig.cdnURL || appConfig.baseURL",
+      "  const publicBase = ''",
       "  return path.length ? joinRelativeURL(publicBase, ...path) : publicBase",
       "}",
       // On server these are registered directly in packages/nuxt/src/core/runtime/nitro/renderer.ts

bojanrajh avatar Oct 18 '24 07:10 bojanrajh

When you use Storybook's render function to render out a component (or components) this issue arises as well.

losbeekos avatar Nov 06 '24 15:11 losbeekos

@d-low for me window.__NUXT__ is always undefined, even after some time.

I've been doing some tests and the function passed to the setup function inside preview.ts is never called, which explains why window.__NUXT__ is always undefined. I see that the setup function is provided by @storybook/vue3, which, according to their code, adds the passed function into globalThis.PLUGINS_SETUP_FUNCTIONS and it's later executed in runSetupFunctions.

I'm having trouble understanding how the 2 packages work together. I know that the Nuxt renderer is a wrapper for @storybook/vue3, but things get complicated inside @storybook/vue3, where it doesn't seem to call the provided setup functions.

Does anyone have an idea of what's happening?

andresilva-cc avatar Dec 06 '24 17:12 andresilva-cc

Reverting commit bcf68df stops the error, but images are broken, maybe it's just a matter of adding staticDirs to .storybook/main.ts.

Ok, so temporary workaround for anyone struggling with this. Add this snippet to your .storybook/preview.ts:

// @ts-expect-error internal Nuxt property
window.__NUXT__ = {
  config: {
    app: {
      baseURL: '/',
      cdnURL: ''
    }
  }
}

And to make images work, add this line to your config in .storybook/main.ts:

staticDirs: ['../public'],

andresilva-cc avatar Dec 06 '24 18:12 andresilva-cc

Reverting commit bcf68df stops the error, but images are broken, maybe it's just a matter of adding staticDirs to .storybook/main.ts.

Ok, so temporary workaround for anyone struggling with this. Add this snippet to your .storybook/preview.ts:

// @ts-expect-error internal Nuxt property window.NUXT = { config: { app: { baseURL: '/', cdnURL: '' } } } And to make images work, add this line to your config in .storybook/main.ts:

staticDirs: ['../public'],

Looks like this error is not showing up when I remove router.options.ts file so yeah definitely something connected to the router. When I include routes and I apply window.__NUXT__ patch from above, then it solves the issue, however, another one arises:

Image

JoJk0 avatar Dec 13 '24 13:12 JoJk0

Any new on this? I also have what JoJk0 says

danieelbog avatar Jan 16 '25 11:01 danieelbog

@AlexanderBredun Is the issue fixed ?

etsubeta132 avatar Feb 11 '25 14:02 etsubeta132

Same issue, interested in solution!

yanniznik avatar May 08 '25 14:05 yanniznik

I have same issue, with just fresh installation. Also, if I create a simple app-button.stories.ts, it throws errors too

Image

GonzaloGPF avatar Jul 02 '25 08:07 GonzaloGPF

This is our current pnpm patch nuxt.

diff --git a/dist/index.mjs b/dist/index.mjs
index 7fa92a5cd8f111907c6d991a367d1442eb5c920f..ed13c46841bbaeff30947d4960dc55eddb240b26 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -6452,13 +6452,13 @@ const publicPathTemplate = {
     return [
       "import { joinRelativeURL } from 'ufo'",
       !nuxt.options.dev && "import { useRuntimeConfig } from 'nitropack/runtime'",
-      nuxt.options.dev ? `const getAppConfig = () => (${JSON.stringify(nuxt.options.app)})` : "const getAppConfig = () => useRuntimeConfig().app",
-      "export const baseURL = () => getAppConfig().baseURL",
-      "export const buildAssetsDir = () => getAppConfig().buildAssetsDir",
+      nuxt.options.dev ? `const getAppConfig = () => (${JSON.stringify(nuxt.options.app)})` : "const getAppConfig = () => useRuntimeConfig()?.app",
+      "export const baseURL = () => getAppConfig()?.baseURL || '/'",
+      "export const buildAssetsDir = () => getAppConfig()?.buildAssetsDir",
       "export const buildAssetsURL = (...path) => joinRelativeURL(publicAssetsURL(), buildAssetsDir(), ...path)",
       "export const publicAssetsURL = (...path) => {",
       "  const appConfig = getAppConfig()",
-      "  const publicBase = appConfig.cdnURL || appConfig.baseURL",
+      "  const publicBase = appConfig?.cdnURL || appConfig?.baseURL || '/'",
       "  return path.length ? joinRelativeURL(publicBase, ...path) : publicBase",
       "}",
       // On server these are registered directly in packages/nuxt/src/core/runtime/nitro/handlers/renderer.ts

bojanrajh avatar Aug 25 '25 07:08 bojanrajh

@bojanrajh, the patch worked for me, but do you know if there is any issue inside Nuxt that would address it in the Core, so we won't need to support the patch internally?

AndrewBogdanovTSS avatar Aug 29 '25 15:08 AndrewBogdanovTSS