Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting dynamic loading working in Nuxt 3 #95

Open
mklueh opened this issue Oct 30, 2022 · 6 comments
Open

Not getting dynamic loading working in Nuxt 3 #95

mklueh opened this issue Oct 30, 2022 · 6 comments
Labels

Comments

@mklueh
Copy link

mklueh commented Oct 30, 2022

I'm confused. What worked with Nuxt 2 does not work (or sometimes only when hot reload triggers the page refresh)

What I've tried so far:

1. Direct require in v-html

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="require(`~/assets/${icon}?raw`)"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
</script>

result:

404 Cannot find any route matching /assets/upload.svg?raw. (http://[::]:3000/nuxt_vite_node/module/../assets/upload.svg?raw)

2. Via await import (not according to the docs)

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = (await import("../assets/" + props.icon + "?raw")).default;

</script>

result:

404 Cannot find any route matching /assets/upload.svg?raw. (http://[::]:3000/nuxt_vite_node/module/../assets/upload.svg?raw)

However, the odd thing is, if I comment that line out let image = (await import("../assets/" + props.icon + "?raw")).default;
and let the page load, and then uncomment it, it will load the icon

3. Via import but not with await

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = import("../assets/" + props.icon + "?raw");

</script>

result: shows [OBJECT PROMISE]

4. With require in script tag

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = require("../assets/" + props.icon + "?raw");

</script>

result:

Cannot find module '../assets/upload.svg?raw' Require stack: - /my-project/src/atoms/BaseImage.vue

**5. With compute and require

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")


let image = computed(() => {
  return require("../assets/" + props.icon + "?raw");
})

</script>

result:

Cannot find module '../assets/upload.svg?raw' Require stack: - /my-project/src/atoms/BaseImage.vue

6. With compute and import and async

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")


let image = computed(async () => {
    return (await import("../assets/" + props.icon + "?raw")).default;
  }
)

</script>

result: shows [OBJECT PROMISE]

I'm not sure what else I could try and I don't see a reason why this happens.

Maybe I should add, that the component is not located in the actual Nuxt application, but a Nuxt library module, and the assets are located in the library as well.

I'd appreciate any help

@manuelodelain
Copy link
Collaborator

Duplicate of #86

@manuelodelain manuelodelain marked this as a duplicate of #86 Nov 8, 2022
@johannschopplich
Copy link

In Nuxt 3, you don't need svg-module, but can instead create a custom component utilizing Vite's glob import:

<template>
  <span v-if="icon" class="h-[1em] w-[1em]" v-html="icon" />
</template>

<script setup lang="ts">
const props = defineProps<{
  name?: string
}>()

// Auto-load icons
const icons = Object.fromEntries(
  Object.entries(import.meta.glob('~/assets/images/*.svg', { as: 'raw' })).map(
    ([key, value]) => {
      const filename = key.split('/').pop()!.split('.').shift()
      return [filename, value]
    },
  ),
)

// Lazily load the icon
const icon = props.name && (await icons?.[props.name]?.())
</script>

@rchl
Copy link
Member

rchl commented Dec 9, 2022

Isn't Vite only used during development?
Unless this feature is not Vite specific...

@johannschopplich
Copy link

johannschopplich commented Dec 9, 2022

@rchl No, Vite is used during development as the dev server and also as the bundler for production builds. Nuxt 3 is built upon Vite. As linked before, import.meta.glob is a Vite-specific feature.

@rchl
Copy link
Member

rchl commented Dec 9, 2022

It says to support both webpack and vite as bundlers. I'm not sure which one in which cases or whether it's user's choice but I guess that in some cases the suggested code wouldn't work.

@johannschopplich
Copy link

johannschopplich commented Dec 9, 2022

Good point. If you opt in for webpack in Nuxt 3, the component would not work, since the glob import is a Vite-specific feature. Honestly, the use-cases for webpack are probably little. The overall experience is just lacking behind Vite. That's why Vite is the default in Nuxt 3 as well as optimized for it. Some modules don't even support webpack in the first place (not saying that's a good thing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants