{"record":{"id":"06aee7bc8a3ba969","repo":"squidfunk/mkdocs-material","slug":"invalid-message-type","errorCode":null,"errorMessage":"Invalid message type","messagePattern":"Invalid message type","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/templates/assets/javascripts/integrations/search/worker/main/index.ts","lineNumber":178,"sourceCode":"      try {\n        return {\n          type: SearchMessageType.RESULT,\n          data: index.search(query)\n        }\n\n      /* Return empty result in case of error */\n      } catch (err) {\n        console.warn(`Invalid query: ${query} – see https://bit.ly/2s3ChXG`)\n        console.warn(err)\n        return {\n          type: SearchMessageType.RESULT,\n          data: { items: [] }\n        }\n      }\n\n    /* All other messages */\n    default:\n      throw new TypeError(\"Invalid message type\")\n  }\n}\n\n/* ----------------------------------------------------------------------------\n * Worker\n * ------------------------------------------------------------------------- */\n\n/* Expose Lunr.js in global scope, or stemmers won't work */\nself.lunr = lunr\n\n/* Monkey-patch Lunr.js to mitigate https://t.ly/68TLq */\nlunr.utils.warn = console.warn\n\n/* Handle messages */\naddEventListener(\"message\", async ev => {\n  postMessage(await handler(ev.data))\n})\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/squidfunk/mkdocs-material/blob/e2136532f40aed98af1f6497c0cc6a3cff9f759b/src/templates/assets/javascripts/integrations/search/worker/main/index.ts#L160-L196","documentation":"The search worker's message handler processes messages posted to the Web Worker via postMessage. When a message arrives whose type does not match any known case in the handler's switch statement, it throws a TypeError(\"Invalid message type\"). This guards the worker's protocol: only defined message types (e.g. search/query setup) are accepted.","triggerScenarios":"postMessage({ type: <unknown> }) sent to the search worker from a custom script or integration; a version mismatch where the host page sends a message type the bundled worker no longer understands; corrupted or hand-built message payloads.","commonSituations":"Custom worker scripts in overrides that fork the search integration; upgrading Material while keeping an old custom search integration (or vice versa); third-party plugins posting their own messages to the shared worker.","solutions":["Check the type field of the message you postMessage; it must exactly match a type the worker's switch handles.","Regenerate/refresh the bundled search worker and integration scripts so host and worker use the same protocol version.","If you need custom messages, extend the worker's handler rather than reusing this one, or handle your messages before forwarding to the worker."],"exampleFix":"// before\nworker.postMessage({ type: \"SEARCH\", query })\n\n// after\nworker.postMessage({ type: \"SEARCH_START\", data: query })","handlingStrategy":"type-guard","validationCode":"const KNOWN = [\"SEARCH_START\", \"SEARCH_READY\"] // types the worker handles\nif (!KNOWN.includes(message.type)) {\n  console.warn(\"Unknown worker message\", message)\n  return\n}\nworker.postMessage(message)","typeGuard":"type WorkerMessageType = \"SEARCH_START\" | \"SEARCH_READY\"\nfunction isWorkerMessage(data: unknown): data is { type: WorkerMessageType; data?: unknown } {\n  return typeof data === \"object\" && data !== null &&\n    \"type\" in data && typeof (data as { type: unknown }).type === \"string\"\n}","tryCatchPattern":"worker.onerror = (event) => {\n  if (String(event.message).includes(\"Invalid message type\")) {\n    // log payload, fall back to non-worker search\n  }\n}","preventionTips":["Keep the host integration and worker scripts from the same Material version.","Centralize message type constants shared between page and worker.","Never post raw unvetted messages to the search worker from custom code.","Write a small test that round-trips each message type through the worker."],"tags":["javascript","web-worker","type-error","message-protocol"],"backgroundTag":"invalid-message-type","analyzedSha":"e2136532f40aed98af1f6497c0cc6a3cff9f759b","analyzedAt":"2026-08-29T11:20:04.299Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}