Skip to content

Commit

Permalink
add optional suspense option for metadata loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Aug 29, 2024
1 parent 7a9c150 commit 81f3247
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/address/[address]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { useAnchorProgram } from '@providers/anchor';
import { CacheEntry, FetchStatus } from '@providers/cache';
import { useCluster } from '@providers/cluster';
import { PROGRAM_ID as ACCOUNT_COMPRESSION_ID } from '@solana/spl-account-compression';
import { PublicKey } from '@solana/web3.js';
import { Ed25519Program, PublicKey } from '@solana/web3.js';
import { Cluster, ClusterStatus } from '@utils/cluster';
import { FEATURE_PROGRAM_ID } from '@utils/parseFeatureAccount';
import { useClusterPath } from '@utils/url';
Expand Down Expand Up @@ -364,10 +364,10 @@ function Token22MintHeader({
}) {
const tokenMetadata = create(metadataExtension.state, TokenMetadata);
const { metadataAddress } = create(metadataPointerExtension.state, MetadataPointer);
const metadata = useMetadataJsonLink(tokenMetadata.uri);
const metadata = useMetadataJsonLink(tokenMetadata.uri, { suspense: true });

if (!metadata) {
throw new Error('Could not load metadata from given URI');
throw new Error(`Could not load metadata from given URI: ${tokenMetadata.uri}`);
}

// Handles the basic case where MetadataPointer is referencing the Token Metadata extension directly
Expand Down
12 changes: 8 additions & 4 deletions app/providers/compressed-nft.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import useSWRImmutable from 'swr/immutable';

export function useMetadataJsonLink(url: string) {
const { data, error } = useSWRImmutable(url, async (url: string) => {
return fetch(url).then(response => response.json());
});
export function useMetadataJsonLink(url: string, options?: { suspense?: boolean }) {
const { data, error } = useSWRImmutable(
url,
async (url: string) => {
return fetch(url).then(response => response.json());
},
{ suspense: options?.suspense }
);
return error ? null : data;
}

Expand Down

0 comments on commit 81f3247

Please sign in to comment.