|
| 1 | +--- |
| 2 | +order: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# ⬆️ Migrating from Version 1 |
| 6 | + |
| 7 | +Miniflare 2 includes [breaking changes](/get-started/changelog#_2-0-0). This |
| 8 | +guide walks you through how to upgrade your app. |
| 9 | + |
| 10 | +## CLI & API Changes |
| 11 | + |
| 12 | +### Upgrade Node.js |
| 13 | + |
| 14 | +**Node.js 16.13.0 is now the minimum required version**. You should use the |
| 15 | +latest Node.js version if possible, as Cloudflare Workers use a very up-to-date |
| 16 | +version of V8. Consider using a Node.js version manager such as |
| 17 | +<https://volta.sh/> or <https://github.com/nvm-sh/nvm>. |
| 18 | + |
| 19 | +### Delete persisted Durable Object and cached data |
| 20 | + |
| 21 | +The storage format for Durable Objects and cached responses has changed in |
| 22 | +Miniflare 2. If you were persisting to the file-system or Redis, you'll need to |
| 23 | +delete these directories/namespaces. |
| 24 | + |
| 25 | +### Delete references to Durable Object IDs |
| 26 | + |
| 27 | +The format for Durable Object IDs has changed in Miniflare 2 to include a hash |
| 28 | +of the object name. If you have any these stored in persisted KV data or |
| 29 | +constants, you'll need to delete them. |
| 30 | + |
| 31 | +### Replace `--disable-updater` with `--no-update-check` |
| 32 | + |
| 33 | +The `--disable-updater` flag has been renamed to `--no-update-check`. |
| 34 | + |
| 35 | +### Replace `--disable-cache` with `--no-cache` |
| 36 | + |
| 37 | +The `--disable-cache` flag has been renamed to `--no-cache`. The `disableCache` |
| 38 | +API option has also been replaced with `cache`. Replace... |
| 39 | + |
| 40 | +```js |
| 41 | +const mf = new Miniflare({ disableCache: true }); // ❌ |
| 42 | +``` |
| 43 | + |
| 44 | +...with... |
| 45 | + |
| 46 | +```js |
| 47 | +const mf = new Miniflare({ cache: false }); // ✅ |
| 48 | +``` |
| 49 | + |
| 50 | +### Replace `miniflare.wasm_bindings` with `wasm_modules` |
| 51 | + |
| 52 | +The `miniflare.wasm_bindings` key was non-standard. It has been replaced with |
| 53 | +the standard `wasm_modules` key. Replace... |
| 54 | + |
| 55 | +```toml |
| 56 | +--- |
| 57 | +filename: wrangler.toml |
| 58 | +--- |
| 59 | +[miniflare] |
| 60 | +wasm_bindings = [ # ❌ |
| 61 | + { name = "MODULE1", path="module1.wasm" }, |
| 62 | + { name = "MODULE2", path="module2.wasm" } |
| 63 | +] |
| 64 | +``` |
| 65 | + |
| 66 | +...with... |
| 67 | + |
| 68 | +```toml |
| 69 | +--- |
| 70 | +filename: wrangler.toml |
| 71 | +--- |
| 72 | +[wasm_modules] # ✅ |
| 73 | +MODULE1 = "module1.wasm" |
| 74 | +MODULE2 = "module2.wasm" |
| 75 | +``` |
| 76 | + |
| 77 | +### Update the `script_name` option |
| 78 | + |
| 79 | +The Durable Object `script_name` option was implemented incorrectly in |
| 80 | +Miniflare 1. It should've been the name of a worker, not a path to a script. |
| 81 | +Replace... |
| 82 | + |
| 83 | +```toml |
| 84 | +--- |
| 85 | +filename: wrangler.toml |
| 86 | +--- |
| 87 | +[durable_objects] |
| 88 | +bindings = [ |
| 89 | + { name = "TEST", class_name = "Test", script_name = "./api/index.mjs" }, # ❌ |
| 90 | +] |
| 91 | +``` |
| 92 | + |
| 93 | +```js |
| 94 | +const mf = new Miniflare({ |
| 95 | + durableObjects: { |
| 96 | + TEST: { className: "Test", scriptPath: "./api/index.mjs" }, // ❌ |
| 97 | + }, |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +...with... |
| 102 | + |
| 103 | +```toml |
| 104 | +--- |
| 105 | +filename: wrangler.toml |
| 106 | +--- |
| 107 | +[durable_objects] |
| 108 | +bindings = [ |
| 109 | + { name = "TEST", class_name = "Test", script_name = "api" }, # ✅ |
| 110 | +] |
| 111 | +[miniflare.mounts] |
| 112 | +api = "./api" |
| 113 | +``` |
| 114 | + |
| 115 | +```js |
| 116 | +const mf = new Miniflare({ |
| 117 | + durableObjects: { |
| 118 | + TEST: { className: "Test", scriptName: "api" }, // ✅ |
| 119 | + }, |
| 120 | + mounts: { api: "./api" }, |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +See |
| 125 | +[📌 Durable Objects](/storage/durable-objects#using-a-class-exported-by-another-script) |
| 126 | +for more details. |
| 127 | + |
| 128 | +### Install the optional `@miniflare/storage-redis` package |
| 129 | + |
| 130 | +Redis persistence support is no longer included by default. If you're persisting |
| 131 | +KV, Durable Objects or cached data in Redis, you must install the |
| 132 | +`@miniflare/storage-redis` optional peer dependency: |
| 133 | + |
| 134 | +```sh |
| 135 | +$ npm install @miniflare/storage-redis -D |
| 136 | +``` |
| 137 | + |
| 138 | +## API Only Changes |
| 139 | + |
| 140 | +### Automatically load configuration files |
| 141 | + |
| 142 | +When using the API, `wrangler.toml`, `package.json` and `.env` are **no longer |
| 143 | +automatically loaded from their default locations**. To re-enable this |
| 144 | +behaviour, set these options to `true`: |
| 145 | + |
| 146 | +```js |
| 147 | +const mf = new Miniflare({ |
| 148 | + wranglerConfigPath: true, // ✅ |
| 149 | + packagePath: true, |
| 150 | + envPath: true, |
| 151 | +}); |
| 152 | +``` |
| 153 | + |
| 154 | +### Replace `ConsoleLog` with `Log` |
| 155 | + |
| 156 | +The `ConsoleLog` class has been replaced with the `Log` class. You can construct |
| 157 | +this with a `LogLevel` to control how much information is logged to the console. |
| 158 | +Replace... |
| 159 | + |
| 160 | +```js |
| 161 | +import { Miniflare, ConsoleLog } from "miniflare"; |
| 162 | +const mf = new Miniflare({ |
| 163 | + log: new ConsoleLog(true), // ❌ |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +...with... |
| 168 | + |
| 169 | +```js |
| 170 | +import { Miniflare, Log, LogLevel } from "miniflare"; |
| 171 | +const mf = new Miniflare({ |
| 172 | + log: new Log(LogLevel.DEBUG), // ✅ |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +### Replace `storage()` with `getDurableObjectStorage()` |
| 177 | + |
| 178 | +The `DurableObjectStub#storage()` method was non-standard, and was accessible |
| 179 | +inside workers, which was not good. It has been replaced with the |
| 180 | +`Miniflare#getDurableObjectStorage()` method. Replace... |
| 181 | + |
| 182 | +```js |
| 183 | +--- |
| 184 | +highlight: [4,5] |
| 185 | +--- |
| 186 | +const mf = new Miniflare({ ... }); |
| 187 | +const ns = await mf.getDurableObjectNamespace("TEST"); |
| 188 | +const id = ns.newUniqueId(); |
| 189 | +const stub = ns.get(id); |
| 190 | +const storage = await stub.storage(); // ❌ |
| 191 | +``` |
| 192 | + |
| 193 | +...with... |
| 194 | + |
| 195 | +```js |
| 196 | +--- |
| 197 | +highlight: [4] |
| 198 | +--- |
| 199 | +const mf = new Miniflare({ ... }); |
| 200 | +const ns = await mf.getDurableObjectNamespace("TEST"); |
| 201 | +const id = ns.newUniqueId(); |
| 202 | +const storage = await mf.getDurableObjectStorage(id); // ✅ |
| 203 | +``` |
| 204 | + |
| 205 | +### Replace `getCache()` with `getCaches()` |
| 206 | + |
| 207 | +The `Miniflare#getCache()` method has been replaced with |
| 208 | +`Miniflare#getCaches()`. Replace... |
| 209 | + |
| 210 | +```js |
| 211 | +const mf = new Miniflare({ ... }); |
| 212 | +const defaultCache = await mf.getCache(); // ❌ |
| 213 | +const namedCache = await mf.getCache("named"); // ❌ |
| 214 | +``` |
| 215 | + |
| 216 | +...with... |
| 217 | + |
| 218 | +```js |
| 219 | +const mf = new Miniflare({ ... }); |
| 220 | +const caches = await mf.getCaches(); |
| 221 | +const defaultCache = caches.default; // ✅ |
| 222 | +const namedCache = await caches.open("named"); // ✅ |
| 223 | +``` |
| 224 | + |
| 225 | +### Replace `buildWatchPath` with `buildWatchPaths` |
| 226 | + |
| 227 | +Miniflare 2 supports watching multiple paths for changes to rebuild on. |
| 228 | +Replace... |
| 229 | + |
| 230 | +```js |
| 231 | +const mf = new Miniflare({ |
| 232 | + buildWatchPath: "./src", // ❌ |
| 233 | +}); |
| 234 | +``` |
| 235 | + |
| 236 | +...with... |
| 237 | + |
| 238 | +```js |
| 239 | +const mf = new Miniflare({ |
| 240 | + buildWatchPaths: ["./src"], // ✅ |
| 241 | +}); |
| 242 | +``` |
| 243 | + |
| 244 | +### Replace `reloadOptions()` with `reload()` |
| 245 | + |
| 246 | +The `Miniflare#reloadOptions()` method has been replaced with |
| 247 | +`Miniflare#reload()`. Replace... |
| 248 | + |
| 249 | +```js |
| 250 | +const mf = new Miniflare({ ... }); |
| 251 | +await mf.reloadOptions(); // ❌ |
| 252 | +``` |
| 253 | + |
| 254 | +...with... |
| 255 | + |
| 256 | +```js |
| 257 | +const mf = new Miniflare({ ... }); |
| 258 | +await mf.reload(); // ✅ |
| 259 | +``` |
| 260 | + |
| 261 | +Miniflare 2 also adds a new `Miniflare#setOptions()` method which accepts the |
| 262 | +same options object as the `new Miniflare` constructor, applies those options, |
| 263 | +then reloads the worker. |
| 264 | + |
| 265 | +```js |
| 266 | +const mf = new Miniflare({ |
| 267 | + buildCommand: "npm run build", |
| 268 | + kvNamespaces: ["TEST"], |
| 269 | +}); |
| 270 | +await mf.setOptions({ |
| 271 | + kvNamespaces: ["TEST2"], // ✅ |
| 272 | +}); |
| 273 | +``` |
| 274 | + |
| 275 | +### Await `createServer()` |
| 276 | + |
| 277 | +The `Miniflare#createServer()` method now always returns a `Promise`. Replace... |
| 278 | + |
| 279 | +```js |
| 280 | +const mf = new Miniflare({ ... }); |
| 281 | +const server = mf.createServer(); // ❌ |
| 282 | +server.listen(5000, () => { ... }); |
| 283 | +``` |
| 284 | + |
| 285 | +...with... |
| 286 | + |
| 287 | +```js |
| 288 | +const mf = new Miniflare({ ... }); |
| 289 | +const server = await mf.createServer(); // ✅ |
| 290 | +server.listen(5000, () => { ... }); |
| 291 | +``` |
| 292 | + |
| 293 | +Miniflare 2 also adds a new `Miniflare#startServer()` which automatically starts |
| 294 | +a server using the configured `host` and `port`. |
| 295 | + |
| 296 | +```js |
| 297 | +const mf = new Miniflare({ port: 5000 }); |
| 298 | +await mf.startServer(); // ✅ |
| 299 | +``` |
0 commit comments