Skip to content

Commit

Permalink
fix(dev-server): set navigator.userAgent correctly (#156)
Browse files Browse the repository at this point in the history
* fix(dev-server): set `navigator.userAgent` with `Object.defineProperty`

* add changeset

* works on Node.js 20.x

* fixed the changeset
  • Loading branch information
yusukebe authored Jul 12, 2024
1 parent d833ce9 commit e62bf1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-lies-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-dev-server': patch
---

fix: set `navigator.userAgent` correctly
15 changes: 11 additions & 4 deletions packages/dev-server/src/adapter/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e62bf1b

Please sign in to comment.