golden-retriever.mdx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ---
  2. sidebar_position: 11
  3. ---
  4. import Tabs from '@theme/Tabs';
  5. import TabItem from '@theme/TabItem';
  6. import UppyCdnExample from '/src/components/UppyCdnExample';
  7. # Golden Retriever
  8. The `@uppy/golden-retriever` plugin saves selected files in your browser cache,
  9. so that if the browser crashes, or the user accidentally closes the tab, Uppy
  10. can restore everything and continue uploading as if nothing happened. You can
  11. read more about it
  12. [on our blog](https://uppy.io/blog/2017/07/golden-retriever/).
  13. The Golden Retriever uses three methods of browser data storage:
  14. - `LocalStorage` to store file metadata and Uppy state only.
  15. - `IndexedDB` for small files, usually under 5MiB.
  16. - `Service Worker` (_optional_) for _all_ files because, unlike `IndexedDB`,
  17. `Service Worker` can keep references to large files. Service Worker storage is
  18. _quite_ temporary though, and doesn’t persist across browser crashes or
  19. restarts. It works well, however, for accidental refreshes or closed tabs.
  20. Upon restore, the plugin first restores state from `LocalStorage` and then
  21. checks both file storages — `IndexedDB` and `ServiceWorker` — merging the
  22. results.
  23. If restore is unsuccessful for certain files, they will be marked as “ghosts” in
  24. the Dashboard UI, and a message + button offering to re-select those files will
  25. be displayed.
  26. Checkout the
  27. [storage quotas](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria#other_web_technologies)
  28. on MDN to see how much data can be stored depending on the device and browser.
  29. ## When should I use this?
  30. When you want extra insurance for the user-selected files. If you feel like
  31. users might spend some time picking files, tweaking descriptions etc, and will
  32. not appreciate having to do it over again if something crashes. Another use case
  33. might be when you think user might want to pick a few files, navigate away to do
  34. something else in your app or otherwise, and then come back to the same
  35. selection.
  36. ## Install
  37. <Tabs>
  38. <TabItem value="npm" label="NPM" default>
  39. ```shell
  40. npm install @uppy/golden-retriever
  41. ```
  42. </TabItem>
  43. <TabItem value="yarn" label="Yarn">
  44. ```shell
  45. yarn add @uppy/golden-retriever
  46. ```
  47. </TabItem>
  48. <TabItem value="cdn" label="CDN">
  49. <UppyCdnExample>
  50. {`
  51. import { Uppy, GoldenRetriever } from "{{UPPY_JS_URL}}"
  52. new Uppy().use(GoldenRetriever)
  53. `}
  54. </UppyCdnExample>
  55. </TabItem>
  56. </Tabs>
  57. ## Use
  58. ```js {7} showLineNumbers
  59. import Uppy from '@uppy/core';
  60. import Dashboard from '@uppy/dashboard';
  61. import GoldenRetriever from '@uppy/golden-retriever';
  62. new Uppy()
  63. .use(Dashboard, {inline:true, target: '#dashboard')
  64. .use(GoldenRetriever);
  65. ```
  66. By default, Golden Retriever will only use the `IndexedDB` storage, which is
  67. good enough for files up to 5 MiB. `Service Worker` is optional and requires
  68. setup.
  69. ### Enabling Service Worker
  70. With the Service Worker storage, Golden Retriever will be able to temporary
  71. store references to large files.
  72. 1. Bundle your own service worker `sw.js` file with Uppy GoldenRetriever’s
  73. service worker.
  74. :::tip
  75. For Webpack, check out
  76. [serviceworker-webpack-plugin](https://github.com/oliviertassinari/serviceworker-webpack-plugin).
  77. :::
  78. ```js title="sw.js"
  79. import('@uppy/golden-retriever/lib/ServiceWorker');
  80. ```
  81. 2. Register it in your app’s entry point:
  82. ```js
  83. // you app.js entry point
  84. uppy.use(GoldenRetriever, { serviceWorker: true });
  85. if ('serviceWorker' in navigator) {
  86. navigator.serviceWorker
  87. .register('/sw.js') // path to your bundled service worker with GoldenRetriever service worker
  88. .then((registration) => {
  89. console.log(
  90. 'ServiceWorker registration successful with scope: ',
  91. registration.scope,
  92. );
  93. })
  94. .catch((error) => {
  95. console.log(`Registration failed with ${error}`);
  96. });
  97. }
  98. ```
  99. Voilà, that’s it. Happy retrieving!
  100. ## API
  101. ### Options
  102. #### `id`
  103. A unique identifier for this plugin (`string`, default: `'GoldenRetriever'`).
  104. #### `expires`
  105. How long to store metadata and files for. Used for `LocalStorage` and
  106. `IndexedDB` (`number`, default: `24 * 60 * 60 * 1000` // 24 hours).
  107. #### `serviceWorker`
  108. Whether to enable `Service Worker` storage (`boolean`, default: `false`).