Skip to main content

Function: parsePathname()

parsePathname(pathname, options?): undefined | { doBindingNameSegment: string; doInstanceNameOrId: undefined | string; }

Defined in: packages/utils/src/parse-pathname.ts:45

Parse pathname to extract DO binding name segment and instance identifier, accounting for optional prefix.

This function handles the core pathname parsing logic for Durable Object routing:

  1. Removes optional prefix if specified (returns undefined if prefix doesn't match)
  2. Splits remaining path into segments
  3. Extracts first segment as binding name and second as instance identifier
  4. Returns undefined if required segments are not present

Parameters

pathname

string

The URL pathname to parse

options?

RouteOptions

Route options containing optional prefix configuration

Returns

undefined | { doBindingNameSegment: string; doInstanceNameOrId: undefined | string; }

Object containing the parsed segments, or undefined if parsing fails

Example

// Basic parsing without prefix
parsePathname('/my-do/instance-123/path');
// → { doBindingNameSegment: 'my-do', doInstanceNameOrId: 'instance-123' }

// With prefix removal
parsePathname('/api/v1/chat-room/lobby/messages', { prefix: '/api/v1' });
// → { doBindingNameSegment: 'chat-room', doInstanceNameOrId: 'lobby' }

// No match cases return undefined
parsePathname('/wrong-prefix/my-do/instance', { prefix: '/api/v1' });
// → undefined

parsePathname(''); // Empty path
// → undefined

// Missing instance segment returns binding with undefined instance
parsePathname('/my-do');
// → { doBindingNameSegment: 'my-do', doInstanceNameOrId: undefined }

// Handles unique IDs
parsePathname('/my-do/8aa7a69131efa8902661702e701295f168aa5806045ec15d01a2f465bd5f3b99');
// → { doBindingNameSegment: 'my-do', doInstanceNameOrId: '8aa7a69131efa8902661702e701295f168aa5806045ec15d01a2f465bd5f3b99' }