You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
fix: remove non default ipld formats in the browser (#1980)
BREAKING CHANGE: Browser application bundles now include only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` IPLD codecs. Other codecs should be added manually, see https://github.com/ipfs/js-ipfs/blob/master/README.md#optionsipld for details.
* In Node.js `require('ipfs')`
* all IPLD formats included
* In browser application bundle `require('ipfs')` bundled with webpack/browserify/etc.
* only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` included
* CDN bundle `<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>`
* all IPLD formats included
Co-Authored-By: hugomrdias <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+140Lines changed: 140 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -326,6 +326,146 @@ Enable and configure experimental features.
326
326
327
327
Modify the default IPFS node config. This object will be *merged* with the default config; it will not replace it.
328
328
329
+
##### `options.ipld`
330
+
331
+
| Type | Default |
332
+
|------|---------|
333
+
| object | [`ipld-nodejs.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-nodejs.js) in Node.js, [`ipld-browser.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-browser.js) in browsers |
334
+
335
+
Modify the default IPLD config. This object will be *merged* with the default config; it will not replace it. Check IPLD [docs](https://github.com/ipld/js-ipld#ipld-constructor) for more information on the available options.
336
+
337
+
> Browser config does **NOT** include by default all the IPLD formats. Only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` are included.
338
+
339
+
To add support for other formats we provide two options, one sync and another async.
return import('ipld-git') // This is a dynamic import
400
+
} else {
401
+
throw new Error('unable to load format ' + multicodec.print[codec])
402
+
}
403
+
}
404
+
}
405
+
}
406
+
)
407
+
```
408
+
> For more information about dynamic imports please check [webpack docs](https://webpack.js.org/guides/code-splitting/#dynamic-imports) or search your bundler documention.
409
+
410
+
Using dynamic imports will tell your bundler to create a separate file (normally called *chunk*) that will **only** be requested by the browser if it's really needed. This strategy will reduce your bundle size and load times without removing any functionality.
411
+
412
+
With Webpack IPLD formats can even be grouped together using magic comments `import(/* webpackChunkName: "ipld-formats" */ 'ipld-git')` to produce a single file with all of them.
413
+
414
+
</details>
415
+
<details><summary>Commonjs Environments</summary>
416
+
417
+
```js
418
+
const node = new IPFS(
419
+
{
420
+
ipld: {
421
+
async loadFormat (codec) {
422
+
if (codec === 'git-raw') {
423
+
return require('ipld-git')
424
+
} else {
425
+
throw new Error('unable to load format ' + multicodec.print[codec])
426
+
}
427
+
}
428
+
}
429
+
}
430
+
)
431
+
```
432
+
</details>
433
+
434
+
<details><summary>Using Script tags</summary>
435
+
436
+
```js
437
+
<script src="https://unpkg.com/ipfs"></script>
438
+
<script>
439
+
440
+
const load = (url, cb) => {
441
+
const script = document.createElement('script')
442
+
script.src = url
443
+
script.onload = () => cb()
444
+
script.onerror = () => cb(new Error('Unable to load script'))
445
+
document.body.appendChild(script);
446
+
};
447
+
448
+
const node = new self.IPFS(
449
+
{
450
+
ipld: {
451
+
loadFormat (codec, cb) {
452
+
switch (codec) {
453
+
case 'git-raw':
454
+
return load('https://unpkg.com/ipld-git', cb)
455
+
case 'bitcoin-block':
456
+
return load('https://unpkg.com/ipld-bitcoin', cb)
457
+
default:
458
+
throw new Error('unable to load format ' + multicodec.print[codec])
0 commit comments