I’m running into a limitation with IPFS regarding directory listing.
Unlike traditional web servers such as Nginx or Apache (with autoindex on), IPFS does not provide a built-in way to list directory contents on the server side.
In my case, the “Media” section of my index.html is designed to:
First attempt to load a media.json file (which would explicitly define the content),
If media.json is not present, fall back to parsing the HTML response of the directory in order to infer the file listing.
However, this fallback approach feels unreliable and somewhat hacky, since IPFS gateways are not guaranteed to return a consistent or parseable directory HTML format.
So my questions are:
Is there a recommended or standard way to handle directory listings in IPFS-backed websites?
Are there best practices for dynamically discovering files in a directory without relying on a pre-generated JSON file?
Do any gateways or tools provide a stable API for listing directory contents?
I’d appreciate any guidance or suggestions from people who have dealt with similar use cases.
Your approach is fine. Static index.html listings at publish time, or a sibling JSON rendered client-side, are both reasonable.
Yes. The dir listing data is already in IPFS DAG. A UnixFS directory is content-addressed protobuf manifest, and every gateway returns those bytes via ?format=raw as application/vnd.ipld.raw. So you could have single index that works without media.json, and can render the listing from the directory’s own block(s) at view time.
To use it on your own content, drop a similar index.html into any directory you publish (download). Note: the demo loads JS from esm.sh for brevity. For production, bundle the dependency into index.html so the page is fully content-addressed with the data.
Note that the demo above uses very naive “blockstore” stub which just fetches blocks via ?format=raw. For advanced path resolution, content-type negotiation, caching, ranges, or full client-side gateway behavior, use @helia/verified-fetch or see even more advanced pre-built UI in service-worker-gateway.