fix: nft image startsWith error#40413
Conversation
Fixes #40412 The error 'Pe.startsWith is not a function' occurred because nftSrcUrl could be a non-string value (e.g., object, array, number) when the NFT metadata returned from token URI contained a non-string image field. Changes: - In useFetchNftDetailsFromTokenURI.ts: validate that data.image and data.name are strings before setting state - In nft-details.tsx: add type guard to ensure nftSrcUrl is a string before calling .startsWith() - Added test case to verify non-string image/name values are ignored Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
|
Cursor Agent can help with this pull request. Just |
✨ Files requiring CODEOWNER review ✨💎 @MetaMask/metamask-assets (1 files, +2 -1)
|
|
startsWith error
Builds ready [e7e5afd]
⚡ Performance Benchmarks (1417 ± 111 ms)
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
| } | ||
| if (typeof data.name === 'string') { | ||
| setName(data.name); | ||
| } |
There was a problem hiding this comment.
I was not able to repro the bug locally for the reported NFT ; but trying to use a malformed data worked (screenshot)
Most NFTs return { "image": "https://..." } (a string). But some return malformed metadata like { "image": { "url": "https://..." } } or { "image": ["img1", "img2"] } ; an object or array instead of a string; the UI wont know what to do with this so better not call setImage;
The component uses the value as a fallback when imageOriginal and image are both missing:
const nftSrcUrl = imageOriginal ?? image ?? imageFromTokenURI;
So in case we fallback to imageFromTokenURI it will be empty



Description
Fixes
TypeError: Pe.startsWith is not a functionwhen loading NFT images by ensuringnftSrcUrlis a string.The error occurred because
nftSrcUrlcould be a non-string value (e.g., an object or number) if the NFT metadata from the token URI contained a non-stringimagefield. The optional chaining (?.) only guards againstnull/undefined, not against calling.startsWith()on a defined non-string value. This PR adds explicit type checks to prevent this.Changelog
CHANGELOG entry: fix: nft image
startsWitherrorRelated issues
Fixes: #40412 https://consensyssoftware.atlassian.net/browse/ASSETS-2793
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Low Risk
Small defensive type-guard changes plus a unit test; behavior only changes for malformed/unsupported tokenURI metadata.
Overview
Prevents a runtime crash in NFT details rendering by only calling
startsWith('ipfs:')when the computednftSrcUrlis actually a string.Hardens
useFetchNftDetailsFromTokenURIto only populateimage/namewhen the tokenURI JSON fields are strings, and adds a test case covering non-string metadata responses.Written by Cursor Bugbot for commit e7e5afd. This will update automatically on new commits. Configure here.