From e62bf1b49d96cd2d59d98572a034bee7514cd00f Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 12 Jul 2024 19:14:33 +0900 Subject: [PATCH] fix(dev-server): set `navigator.userAgent` correctly (#156) * fix(dev-server): set `navigator.userAgent` with `Object.defineProperty` * add changeset * works on Node.js 20.x * fixed the changeset --- .changeset/quick-lies-pull.md | 5 +++++ packages/dev-server/src/adapter/cloudflare.ts | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .changeset/quick-lies-pull.md diff --git a/.changeset/quick-lies-pull.md b/.changeset/quick-lies-pull.md new file mode 100644 index 0000000..7dbf8e9 --- /dev/null +++ b/.changeset/quick-lies-pull.md @@ -0,0 +1,5 @@ +--- +'@hono/vite-dev-server': patch +--- + +fix: set `navigator.userAgent` correctly diff --git a/packages/dev-server/src/adapter/cloudflare.ts b/packages/dev-server/src/adapter/cloudflare.ts index f7fcdec..6a4892f 100644 --- a/packages/dev-server/src/adapter/cloudflare.ts +++ b/packages/dev-server/src/adapter/cloudflare.ts @@ -16,11 +16,18 @@ export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise< proxy ??= await getPlatformProxy(options?.proxy) // Cache API provided by `getPlatformProxy` currently do nothing. Object.assign(globalThis, { caches: proxy.caches }) - Object.assign(globalThis, { - navigator: { + if (typeof globalThis.navigator === 'undefined') { + // @ts-expect-error not typed well + globalThis.navigator = { userAgent: 'Cloudflare-Workers', - }, - }) + } + } else { + Object.defineProperty(globalThis.navigator, 'userAgent', { + value: 'Cloudflare-Workers', + writable: false, + }) + } + return { env: proxy.env, executionContext: proxy.ctx,