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

TypeError: Cannot read property '_img' of undefined #258

Closed
lucadalli opened this issue May 11, 2021 · 4 comments
Closed

TypeError: Cannot read property '_img' of undefined #258

lucadalli opened this issue May 11, 2021 · 4 comments

Comments

@lucadalli
Copy link

I am attempting to resize images during nuxt.generate by calling context.$img from a server plugin.
The plugin is intentionally naïve and runs on every request to keep the demo minimal.

I am facing the error below:

TypeError: Cannot read property '_img' of undefined
    at handleStaticImage (node_modules/@nuxt/image/dist/runtime/image.js:38:0)
    at getImage (node_modules/@nuxt/image/dist/runtime/image.js:14:0)
    at $img2 (node_modules/@nuxt/image/dist/runtime/image.js:19:0)
    at image_server (plugins/image.server.js:3:0)
    at createApp (node_modules/.cache/nuxt/index.js:185:0)
    at async module.exports.__webpack_exports__.default (node_modules/.cache/nuxt/server.js:85:0)

Reproduction steps:

  1. Visit https://codesandbox.io/s/icy-lake-w3wzh?file=/plugins/image.server.js
  2. Run yarn run generate in terminal
@shadow81627
Copy link
Contributor

shadow81627 commented May 12, 2021

I had a similar issue with my 404 page.

Duplicate of #249

@pi0
Copy link
Member

pi0 commented May 12, 2021

Hi @lucadalli. In fact that stage (plugin entry) server context is not available that's why you are getting this error. Proper method is using beforeNuxtRender hook:

export default function ({ $img, beforeNuxtRender }) {
  if (process.static) {
    beforeNuxtRender(() => {
      $img("https://via.placeholder.com/350x150", {
        width: 100
      });
    });
  }
}

See my fork: https://codesandbox.io/s/lively-feather-uwhmz?file=/plugins/image.server.js (link outdated)

Regardless I will try to add a guard when ssrContext is not available (it won't work but also don't crash and possibly fixing #249)

pi0 added a commit that referenced this issue May 12, 2021
@pi0
Copy link
Member

pi0 commented May 13, 2021

Guard also added in @nuxt/image@0.4.9.

@pi0 pi0 closed this as completed May 13, 2021
@lucadalli
Copy link
Author

@pi0, it's working as intended with beforeNuxtRender. Thank you 🙏

procrates pushed a commit to procrates/nuxt-image that referenced this issue Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants