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.

g-images with dynamic paths not correctly generating full feature set

See original GitHub issue

Summary

We have some components that we want to load images in. I’ve tried many variations on making this work, and in most cases, gridsome simply outputs the literal property path without actually transforming it. Using require() will correctly output the path, but then it doesn’t output an actual g-image feature, just a generic unhashed img tag.

I’m quite surprised at this as I can’t imagine the tool would be very useful without dynamic images in components.

There is an existing bug for this with a proposed solution, however as you’ll see below, the proposed solution is actually not working.

Example code

<template>
  <div> <!-- ... some other html ... -->

    <!-- FAIL: with computed, it outputs the literal imagePath, broken image -->
    <g-image :src="imagePath" />

    <!-- FAIL: with direct prop, it outputs the literal prop, broken image -->
    <g-image :src="imageSrc" />

    <!-- FAIL: kind of works, but just directly outputs the direct image tag without any g-image features -->
    <g-image :src="webpackImagePath" />

    <!-- FAIL: works, but it's not dynamic! -->
    <g-image src="@/assets/images/literal-image-path.png" /> 
  </div>
</template>

<script>
export default {
  props: {
    imageSrc: {
      type: String,
      required: true
    }
  },
  computed: {
    imagePath () {
      return `@/assets/images/${this.imageSrc}`
    },
    webpackImagePath () {
      return require(`@/assets/images/${this.imageSrc}`)
    }
  }
}

Current behavior

Literal image path

Works as expected, but we need dynamic paths.

<img src="/assets/static/src/assets/images/literal-image-path.png?width=800" width="800" data-src="/assets/static/src/assets/images/literal-image-path.png?width=800" data-srcset="/assets/static/src/assets/images/literal-image-path.png?width=480 480w, /assets/static/src/assets/images/literal-image-path.png?width=800 800w" data-sizes="(max-width: 800px) 100vw, 800px" class="g-image g-image--lazy g-image--loaded" sizes="(max-width: 800px) 100vw, 800px" srcset="/assets/static/src/assets/images/literal-image-path.png?width=480 480w, /assets/static/src/assets/images/literal-image-path.png?width=800 800w">

[FAIL] Dynamic with property (no require):

It’s outputting the literal image path passed in by the prop or computed.

<img src="@/assets/images/literal-image-path.png" class="g-image">

[FAIL] Dynamically using webpack require():

As you can see, it is outputting a transformed path, but it’s not hashed, and it’s missing all of the g-image features.

<img src="/assets/img/literal-image-path.png" class="g-image">

Expected behavior

Either one of:

  • dynamic prop image paths are correctly loaded as a g-image
  • webpack require() is correctly loaded as a g-image

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:60
  • Comments:77 (20 by maintainers)

github_iconTop GitHub Comments

50reactions
jeremyjackson89commented, Feb 8, 2020

This has been working for me:

Go to gridsome.config.js and add an alias for your images folder inside of the chainWebpack function (you may need to add it):

module.exports = {
  siteName: 'Jeremy Jackson',
  plugins: [],
  chainWebpack: config => {
    config.resolve.alias.set('@images', '@/assets/images')
  },
  templates: {}
}

Save the file and restart the server.

Then in your code where you want to use the image you can do:

<g-image :src="require(`!!assets-loader!@images/${imageUrl}`)"/>

And the dynamic image shows up. This works inside of v-for as well.

For reference, here’s the code I’m using inside of a v-for in my site:

<div id="project-images">
  <div class="section-header">Screenshots</div>
  <div class="row">
    <div 
      class="col-xs-12 col-sm-3 project-image"
      v-for="(projectImage, index) in $page.project.images"
      :key="projectImage"
      @click="openLightBox(index)"
    >
      <g-image class="responsive-image" :src="require(`!!assets-loader!@images/${projectImage}`)"/>
    </div>
  </div>
</div>

and the result:

Screen Shot 2020-02-07 at 9 13 45 PM

This has worked for me. If there’s a better solution to this, let me know so I can fix my stuff 😉

21reactions
vimtorcommented, May 12, 2020

Is this planned to be solved? Seems like a very important core feature 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js dynamic images not working - Stack Overflow
The problem was that the path doesn't exist. The whole vue app get compiled by webpack into a single script (and images are...
Read more >
Use dynamic report elements—ArcGIS Pro | Documentation
Dynamic pictures allow you to reference pictures in the index layer included as attachments, raster fields, URLs, or file paths. If the value...
Read more >
Manage paths in Photoshop - Adobe Support
In Adobe Photoshop, learn how to create and manage paths using the Paths ... To create and name a path, make sure no...
Read more >
Dynamically resize images - Akamai TechDocs
Extract the image dimensions from your URL and assign them to the PMUSER_WIDTH and PMUSER_HEIGHT variables you just created using the Set Variable...
Read more >
Router tutorial: tour of heroes - Angular
You've created two routes in the application so far, one to /crisis-center and ... template and set the link to a non-existant route...
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