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

generation works only once: [Error] TypeError: Cannot read properties of undefined (reading '0') #650

Closed
SwapnilSoni1999 opened this issue Nov 27, 2024 · 4 comments · Fixed by #651
Labels

Comments

@SwapnilSoni1999
Copy link

SwapnilSoni1999 commented Nov 27, 2024

Bug report

Description / Observed Behavior

I'm using satori in nodejs environment and the svg generation only works once then it gives an error

Expected Behavior

It should generate svg

Here's the code snippet to regenerate the issue

const poppinsExtraBoldPath = path.join(ASSETS_DIR, 'Poppins-ExtraBold.ttf')
const boldFont = readFileSync(poppinsExtraBoldPath)

const generateHexagonProfilePictureSvg = async (
  imageUrl: string,
  dimensions: {
    width: number
    height: number
  } = {
    width: 130,
    height: 120
  }
) => {
  const JSX = {
    type: 'div',
    props: {
      style: {
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
        width: `${dimensions.width}px`,
        height: `${dimensions.height}px`,
        backgroundColor: 'transparent'
      },
      children: [
        {
          type: 'svg',
          props: {
            width: `${dimensions.width}`,
            height: `${dimensions.height}`,
            viewBox: '0 0 135.99488 126.66667',
            xmlns: 'http://www.w3.org/2000/svg',
            children: [
              {
                type: 'defs',
                props: {
                  children: [
                    {
                      type: 'clipPath',
                      props: {
                        id: 'svgPath',
                        children: [
                          {
                            type: 'path',
                            props: {
                              d: 'm 0,0 h -26.12 c -8.174,0 -15.727,-4.361 -19.814,-11.44 l -13.06,-22.62 c -4.087,-7.079 -4.087,-15.801 0,-22.88 l 13.06,-22.62 C -41.847,-86.639 -34.294,-91 -26.12,-91 H 0 c 8.174,0 15.727,4.361 19.814,11.44 l 13.06,22.62 c 4.087,7.079 4.087,15.801 0,22.88 l -13.06,22.62 C 15.727,-4.361 8.174,0 0,0',
                              transform:
                                'matrix(1.3333333,0,0,-1.3333333,85.410866,2.6666667)'
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                type: 'image',
                props: {
                  href: imageUrl,
                  width: '100%',
                  height: '100%',
                  preserveAspectRatio: 'xMidYMid slice',
                  clipPath: 'url(#svgPath)'
                }
              },
              {
                type: 'path',
                props: {
                  d: 'm 0,0 h -26.12 c -8.174,0 -15.727,-4.361 -19.814,-11.44 l -13.06,-22.62 c -4.087,-7.079 -4.087,-15.801 0,-22.88 l 13.06,-22.62 C -41.847,-86.639 -34.294,-91 -26.12,-91 H 0 c 8.174,0 15.727,4.361 19.814,11.44 l 13.06,22.62 c 4.087,7.079 4.087,15.801 0,22.88 l -13.06,22.62 C 15.727,-4.361 8.174,0 0,0',
                  transform:
                    'matrix(1.3333333,0,0,-1.3333333,85.410866,2.6666667)',
                  fill: 'none',
                  stroke: 'white',
                  strokeWidth: '5'
                }
              }
            ]
          }
        }
      ]
    }
  }

  console.log({
    JSX
  })

  const svg = await satori(JSX, {
    width: dimensions.width,
    height: dimensions.height,
    fonts: [
      {
        name: 'Poppins',
        style: 'normal',
        data: boldFont
      }
    ],
    debug: true
  })

  return svg
}

// FIRST CALL WORKS!!
const profilePictureSvg = await generateHexagonProfilePictureSvg(
    data.profileUrl,
    {
      width: 140,
      height: 130
    }
  )

// 2nd CALL FAILS!
const profilePictureSvg2 = await generateHexagonProfilePictureSvg(
    data.profileUrl,
    {
      width: 140,
      height: 130
    }
  )

Additional Context

"satori": "^0.12.0"

@SwapnilSoni1999
Copy link
Author

I managed to fix by setting base64 encoded image data in href of image tag inside svg
However setting a direct url isn't working for the 2nd time
I suspect the LRU caching is the culprit

@erxclau
Copy link
Contributor

erxclau commented Nov 28, 2024

Here's a reproduction in the playground.

Making an update in the code (e.g. adding a space somewhere) will trigger this error:

undefined is not an object (evaluating 'Pt.get(t)[0]')

The browser console traces back to here:

Screenshot 2024-11-28 at 3 36 24 PM

The relevant lines are here:

const attrs = `${Object.entries(restProps)
.map(([k, _v]) => {
if (typeof _v === 'string' && _v.toLowerCase() === 'currentcolor') {
_v = currentColor
}
if (k === 'href' && type === 'image') {
return ` ${ATTRIBUTE_MAPPING[k] || k}="${cache.get(_v as string)[0]}"`
}
return ` ${ATTRIBUTE_MAPPING[k] || k}="${_v}"`
})
.join('')}`

@erxclau
Copy link
Contributor

erxclau commented Nov 28, 2024

This mainly has to do with how resolveImageData is implemented in the image handler.

  1. Start by preprocessing, which handles image data resolution.
  2. On the first render, the function will fetch the image URL. While the function fetches, we also mark it down in a map of inflight requests (that maps the URL to the fetch promise).
  3. When the image is fetched, we generate a base64 version of the image and add it to the cache. If we need to resolve the same URL, we simply return the promise in the inflight requests map here.
  4. At this point, we properly generate the SVG element attributes.
  5. At the second render, we start by clearing the cache. However, we do not clear the inflight requests map. This results in an early return before we add the image data back to the cache.

I think the most straightforward solution would be to clear the map of inflight requests along with the cache clear.

shuding pushed a commit that referenced this issue Jan 6, 2025
Closes #650

## Current behavior

Try changing the background color in the `main` [playground
link](https://og-playground.vercel.app/?share=7VVdb9owFP0rlvfSScH5BEIEnVZUqZ3UrZ2qTZN4CYmTmDpxZhsCQ_z33XxBqvVp2uOMUHyvj-899_o4OeJIxBQHeB6z3apASOkDp4vjsZ4jFDNV8vAQoBVOON2vsNH6N1ulWXJYikLTQtfLETypPANCztLiXtNcvVpE7WrFYp0FyLasDp5RlmYQ5-JZh9FLKsW2iJeCC1lH0TIsVBlKCNblOZ1WxXU9matdOgi9OEIgWLuEHnp2jFY3Yr9YYQtZyHbHZDbzfB_ZzoRMYExXuAXuc14ogGVal4FpVlVFKpcImZqOZVlmnbNBNhSAREwT1c3BijgrH0OdIRZDDADXxgqfAQApa8_ZRKipMBEyhw15qCXbX9nEbYdhwW90Mf0x8WzLn0wMp2ENvN_3xNtRp80RbEMZGkFttoMiNPKJPfXAN7LHZOpMjZFH3IkN5oz4tgcZbOJ5iIPDJdbEGDkQvtnoEcsH-JRY09nZgiC-ZUOSGgc95NDPwbYlAG3ie4D0obkubHQ94swgzwxyNqSa6R2cRIR6bh21lllHrOfVxu9ZtTRaTu38zKgn1BXSE3pddp8RujRs3vXcrM_mcphmf5r9WZuDw56zPExpj80kTTrRKFBNEirND6RkkdrmpMyEFspksemM_VpGpmtZZFOmH7I8jBaP019bb3bzqXq6u9t8vp08OvffHKofnp65-LJ2ntY8_-mz_XL00EnvTcm_JXqESkkVlTv6UZU00l9DzQTQ3D-w-Af8keIsopegfb0A2Up-9a7T71li0KGm6r4FQyknjHPYV4hiEFBpKV4ouKuM6T_839saxme2_-4q_L8Gf3UNhldgXr_rYAaqZ7trbGBRgnoKhYMjbtSHAx_e3LiVHQ682ojpepviIAm5ogamudiw50NZf2t01VgQJ4EPyG2-pjEOtNzSk4F1uAZERjkXlZA8xqff).
See this comment for
[explanation](#650 (comment))
of why this is happening.

## Proposed behavior

Try changing the background color in this branch's [playground
link](https://satori-playground-git-fork-erxclau-clear-image-inflight-6f2758.vercel.sh/?share=7VVdb9owFP0rlvfSScH5BEIEnVZUqZ3UrZ2qTZN4CYmTmDpxZhsCQ_z33XxBqvVp2uOMUHyvj-899_o4OeJIxBQHeB6z3apASOkDp4vjsZ4jFDNV8vAQoBVOON2vsNH6N1ulWXJYikLTQtfLETypPANCztLiXtNcvVpE7WrFYp0FyLasDp5RlmYQ5-JZh9FLKsW2iJeCC1lH0TIsVBlKCNblOZ1WxXU9matdOgi9OEIgWLuEHnp2jFY3Yr9YYQtZyHbHZDbzfB_ZzoRMYExXuAXuc14ogGVal4FpVlVFKpcImZqOZVlmnbNBNhSAREwT1c3BijgrH0OdIRZDDADXxgqfAQApa8_ZRKipMBEyhw15qCXbX9nEbYdhwW90Mf0x8WzLn0wMp2ENvN_3xNtRp80RbEMZGkFttoMiNPKJPfXAN7LHZOpMjZFH3IkN5oz4tgcZbOJ5iIPDJdbEGDkQvtnoEcsH-JRY09nZgiC-ZUOSGgc95NDPwbYlAG3ie4D0obkubHQ94swgzwxyNqSa6R2cRIR6bh21lllHrOfVxu9ZtTRaTu38zKgn1BXSE3pddp8RujRs3vXcrM_mcphmf5r9WZuDw56zPExpj80kTTrRKFBNEirND6RkkdrmpMyEFspksemM_VpGpmtZZFOmH7I8jBaP019bb3bzqXq6u9t8vp08OvffHKofnp65-LJ2ntY8_-mz_XL00EnvTcm_JXqESkkVlTv6UZU00l9DzQTQ3D-w-Af8keIsopegfb0A2Up-9a7T71li0KGm6r4FQyknjHPYV4hiEFBpKV4ouKuM6T_839saxme2_-4q_L8Gf3UNhldgXr_rYAaqZ7trbGBRgnoKhYMjbtSHAx_e3LiVHQ682ojpepviIAm5ogamudiw50NZf2t01VgQJ4EPyG2-pjEOtNzSk4F1uAZERjkXlZA8xqff).

## Related

This is related to #593 and #592
(#592 (comment) and
#592 (comment)).

The error in #592 is no longer thrown, though the image still does not
render in PNG mode
([link](https://satori-playground-git-fork-erxclau-clear-image-inflight-6f2758.vercel.sh/?share=7VVdb9owFP0rlvfSScH5BEIEnVZUqZ3UrZ2qTZN4CYmTmDpxZhsCQ_z33XxBqvVp2uOMUHyvj-899_o4OeJIxBQHeB6z3apASOkDp4vjsZ4jFDNV8vAQoBVOON2vsNH6N1ulWXJYikLTQtfLETypPANCztLiXtNcvVpE7WrFYp0FyLasDp5RlmYQ5-JZh9FLKsW2iJeCC1lH0TIsVBlKCNblOZ1WxXU9matdOgi9OEIgWLuEHnp2jFY3Yr9YYQtZyHbHZDbzfB_ZzoRMYExXuAXuc14ogGVal4FpVlVFKpcImZqOZVlmnbNBNhSAREwT1c3BijgrH0OdIRZDDADXxgqfAQApa8_ZRKipMBEyhw15qCXbX9nEbYdhwW90Mf0x8WzLn0wMp2ENvN_3xNtRp80RbEMZGkFttoMiNPKJPfXAN7LHZOpMjZFH3IkN5oz4tgcZbOJ5iIPDJdbEGDkQvtnoEcsH-JRY09nZgiC-ZUOSGgc95NDPwbYlAG3ie4D0obkubHQ94swgzwxyNqSa6R2cRIR6bh21lllHrOfVxu9ZtTRaTu38zKgn1BXSE3pddp8RujRs3vXcrM_mcphmf5r9WZuDw56zPExpj80kTTrRKFBNEirND6RkkdrmpMyEFspksemM_VpGpmtZZFOmH7I8jBaP019bb3bzqXq6u9t8vp08OvffHKofnp65-LJ2ntY8_-mz_XL00EnvTcm_JXqESkkVlTv6UZU00l9DzQTQ3D-w-Af8keIsopegfb0A2Up-9a7T71li0KGm6r4FQyknjHPYV4hiEFBpKV4ouKuM6T_839saxme2_-4q_L8Gf3UNhldgXr_rYAaqZ7trbGBRgnoKhYMjbtSHAx_e3LiVHQ682ojpepviIAm5ogamudiw50NZf2t01VgQJ4EPyG2-pjEOtNzSk4F1uAZERjkXlZA8xqff)).
Copy link

github-actions bot commented Jan 6, 2025

🎉 This issue has been resolved in version 0.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

kodiakhq bot pushed a commit to X-oss-byte/Nextjs that referenced this issue Jan 6, 2025
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [satori](https://redirect.github.com/vercel/satori) | [`0.12.0` -> `0.12.1`](https://renovatebot.com/diffs/npm/satori/0.10.13/0.12.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/satori/0.12.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/satori/0.12.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/satori/0.10.13/0.12.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/satori/0.10.13/0.12.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>vercel/satori (satori)</summary>

### [`v0.12.1`](https://redirect.github.com/vercel/satori/releases/tag/0.12.1)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.12.0...0.12.1)

##### Bug Fixes

-   clear inflight request map for image resolution ([#&#8203;651](https://redirect.github.com/vercel/satori/issues/651)) ([57a89ea](https://redirect.github.com/vercel/satori/commit/57a89ea6b1a4fdf3c273b4f6d4f384fa02cacc5c)), closes [#&#8203;650](https://redirect.github.com/vercel/satori/issues/650) [/github.com/vercel/satori/issues/650#issuecomment-2506776490](https://redirect.github.com//github.com/vercel/satori/issues/650/issues/issuecomment-2506776490) [#&#8203;593](https://redirect.github.com/vercel/satori/issues/593) [#&#8203;592](https://redirect.github.com/vercel/satori/issues/592) [/github.com/vercel/satori/issues/592#issuecomment-1979586066](https://redirect.github.com//github.com/vercel/satori/issues/592/issues/issuecomment-1979586066) [/github.com/vercel/satori/issues/592#issuecomment-1979590654](https://redirect.github.com//github.com/vercel/satori/issues/592/issues/issuecomment-1979590654) [#&#8203;592](https://redirect.github.com/vercel/satori/issues/592)

### [`v0.12.0`](https://redirect.github.com/vercel/satori/releases/tag/0.12.0)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.11.3...0.12.0)

##### Features

-   add ability to add text stroke  ([#&#8203;645](https://redirect.github.com/vercel/satori/issues/645)) ([1481902](https://redirect.github.com/vercel/satori/commit/14819024d4d9a38509656329877f200a753d504a)), closes [#&#8203;578](https://redirect.github.com/vercel/satori/issues/578)

### [`v0.11.3`](https://redirect.github.com/vercel/satori/releases/tag/0.11.3)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.11.2...0.11.3)

##### Bug Fixes

-   background-clip:text should compatible with transform and mask ([#&#8203;639](https://redirect.github.com/vercel/satori/issues/639)) ([11575c9](https://redirect.github.com/vercel/satori/commit/11575c9811e396bf88a83d8e158c639a0944b1c8))

### [`v0.11.2`](https://redirect.github.com/vercel/satori/releases/tag/0.11.2)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.11.1...0.11.2)

##### Bug Fixes

-   textDecoration should compatible with transform ([#&#8203;640](https://redirect.github.com/vercel/satori/issues/640)) ([618d565](https://redirect.github.com/vercel/satori/commit/618d565edb83270d9b829edc430788032e6f2bc6))

### [`v0.11.1`](https://redirect.github.com/vercel/satori/releases/tag/0.11.1)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.11.0...0.11.1)

##### Bug Fixes

-   clipping behavior of children with transform ([#&#8203;635](https://redirect.github.com/vercel/satori/issues/635)) ([c55e4da](https://redirect.github.com/vercel/satori/commit/c55e4da1f5a66edc906f3ab5f901efff4b7cd6f2))

### [`v0.11.0`](https://redirect.github.com/vercel/satori/releases/tag/0.11.0)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.10.14...0.11.0)

##### Features

-   support `repeating-linear-gradient` ([#&#8203;630](https://redirect.github.com/vercel/satori/issues/630)) ([ff80448](https://redirect.github.com/vercel/satori/commit/ff80448924541575bfd72c179a5ceb199912a9cf)), closes [#&#8203;624](https://redirect.github.com/vercel/satori/issues/624)

### [`v0.10.14`](https://redirect.github.com/vercel/satori/releases/tag/0.10.14)

[Compare Source](https://redirect.github.com/vercel/satori/compare/0.10.13...0.10.14)

##### Bug Fixes

-   incorrect data URL parsing ([#&#8203;596](https://redirect.github.com/vercel/satori/issues/596)) ([965b3a1](https://redirect.github.com/vercel/satori/commit/965b3a12a28ab624d55246f17bebc5d9b90f35a2)), closes [#&#8203;597](https://redirect.github.com/vercel/satori/issues/597) [/www.rfc-editor.org/rfc/rfc2397#section-3](https://redirect.github.com//www.rfc-editor.org/rfc/rfc2397/issues/section-3)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/X-oss-byte/Nextjs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants