diff --git a/src/content/changelog/durable-objects/2026-03-15-durable-object-id-name.mdx b/src/content/changelog/durable-objects/2026-03-15-durable-object-id-name.mdx new file mode 100644 index 000000000000000..c3a7dd97977d0d4 --- /dev/null +++ b/src/content/changelog/durable-objects/2026-03-15-durable-object-id-name.mdx @@ -0,0 +1,42 @@ +--- +title: Access Durable Object name via `ctx.id.name` +description: Retrieve the name of a named Durable Object from within the object itself. +products: + - durable-objects + - workers +date: 2026-03-15 +--- + +When your Worker accesses a Durable Object via `idFromName()` or `getByName()`, the same name is now available on `ctx.id.name` inside the object — no need to pass it through method arguments or persist it in storage. This brings the runtime behavior in line with the [Workers runtime types](/workers/languages/typescript/). + +This is especially useful for [alarms](/durable-objects/api/alarms/), where there is no calling client to pass the name as an argument. When an alarm handler runs, `ctx.id.name` will hold the same name the object was originally accessed with. + +```js +import { DurableObject } from "cloudflare:workers"; + +export class ChatRoom extends DurableObject { + async getRoomName() { + // ctx.id.name returns the name passed to getByName() or idFromName() + return this.ctx.id.name; + } +} + +// Worker +export default { + async fetch(request, env) { + const stub = env.CHAT_ROOM.getByName("general"); + const roomName = await stub.getRoomName(); + return new Response(`Welcome to ${roomName}!`); + }, +}; +``` + +`ctx.id.name` is `undefined` in the following cases: + +- For Durable Objects created with `newUniqueId()`. +- When accessed via `idFromString()`, even if the ID was originally created from a name. +- For [names longer than 1,024 bytes](/durable-objects/api/id/#name). + +This works the same way in local development with `wrangler dev` as it does in production. Run `npm update wrangler` to ensure you are on a version with this support. + +For more information, refer to the [Durable Object ID documentation](/durable-objects/api/id/#name). diff --git a/src/content/changelog/durable-objects/2026-03-26-durable-object-id-name.mdx b/src/content/changelog/durable-objects/2026-03-26-durable-object-id-name.mdx deleted file mode 100644 index 7e5261e431ca833..000000000000000 --- a/src/content/changelog/durable-objects/2026-03-26-durable-object-id-name.mdx +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: Access Durable Object name via `ctx.id.name` -description: Retrieve the name of a named Durable Object from within the object itself. -products: - - durable-objects - - workers -date: 2026-03-26 ---- - -`ctx.id.name` inside a Durable Object now matches what you see client-side. If a Worker accesses a Durable Object via `idFromName()` or `getByName()`, the same name is now available on `ctx.id.name` inside the object — no need to pass it through method arguments or persist it in storage. - -Previously, the TypeScript type for `ctx.id` declared `name` as `string | undefined`, which led developers and LLM-based coding assistants to assume the name would be available inside the Durable Object. In reality, it was always `undefined` from inside, forcing manual workarounds. - -```js -export class ChatRoom extends DurableObject { - async fetch(request) { - // ctx.id.name now matches the name passed to getByName() or idFromName() on the client side - const roomName = this.ctx.id.name; - return new Response(`Welcome to ${roomName}!`); - } -} - -// Worker -export default { - async fetch(request, env) { - const stub = env.CHAT_ROOM.getByName("general"); - return stub.fetch(request); - }, -}; -``` - -A few things to note about when `ctx.id.name` is set: - -- **Populated** when the Durable Object is accessed via `idFromName()` or `getByName()`. -- **`undefined`** for Durable Objects created with `newUniqueId()`. -- **`undefined`** when accessed via `idFromString()`, even if the ID was originally created from a name. -- **`undefined`** for names longer than 1,024 bytes. - -For more information, refer to the [Durable Object ID documentation](/durable-objects/api/id/#name).