From 28f39bdc6ac1af7ffe7e3ff722a09f54666fced7 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 12 Jul 2024 19:03:53 +0900 Subject: [PATCH 1/4] fix(dev-server): set `navigator.userAgent` with `Object.defineProperty` --- packages/dev-server/src/adapter/cloudflare.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/dev-server/src/adapter/cloudflare.ts b/packages/dev-server/src/adapter/cloudflare.ts index f7fcdec..f1f2f20 100644 --- a/packages/dev-server/src/adapter/cloudflare.ts +++ b/packages/dev-server/src/adapter/cloudflare.ts @@ -16,11 +16,11 @@ 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: { - userAgent: 'Cloudflare-Workers', - }, + Object.defineProperty(globalThis.navigator, 'userAgent', { + value: 'Cloudflare-Workers', + writable: false, }) + return { env: proxy.env, executionContext: proxy.ctx, From 198f7ab2721a7d699863246f2d42db0d91c48ff7 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 12 Jul 2024 19:05:49 +0900 Subject: [PATCH 2/4] add changeset --- .changeset/quick-lies-pull.md | 5 +++++ 1 file changed, 5 insertions(+) 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..b7a60d3 --- /dev/null +++ b/.changeset/quick-lies-pull.md @@ -0,0 +1,5 @@ +--- +'@hono/vite-dev-server': patch +--- + +fix: set `navigator.userAgent` with `Object.defineProperty` From 10f2695c843e622cbb7a8dbd9dd56d5858060113 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 12 Jul 2024 19:10:09 +0900 Subject: [PATCH 3/4] works on Node.js 20.x --- packages/dev-server/src/adapter/cloudflare.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/dev-server/src/adapter/cloudflare.ts b/packages/dev-server/src/adapter/cloudflare.ts index f1f2f20..6a4892f 100644 --- a/packages/dev-server/src/adapter/cloudflare.ts +++ b/packages/dev-server/src/adapter/cloudflare.ts @@ -16,10 +16,17 @@ 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.defineProperty(globalThis.navigator, 'userAgent', { - value: 'Cloudflare-Workers', - writable: false, - }) + 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, From 3c0605534c73bfd48649e663021d43df44e8e525 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 12 Jul 2024 19:11:58 +0900 Subject: [PATCH 4/4] fixed the changeset --- .changeset/quick-lies-pull.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/quick-lies-pull.md b/.changeset/quick-lies-pull.md index b7a60d3..7dbf8e9 100644 --- a/.changeset/quick-lies-pull.md +++ b/.changeset/quick-lies-pull.md @@ -2,4 +2,4 @@ '@hono/vite-dev-server': patch --- -fix: set `navigator.userAgent` with `Object.defineProperty` +fix: set `navigator.userAgent` correctly