Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect browsers in a Deno-compatible way #821

Merged

Conversation

yudai-nkt
Copy link
Contributor

@yudai-nkt yudai-nkt commented Sep 9, 2023

I'm trying to use Tesseract.js in Deno, but currently I can't because of an incorrect runtime detection:

import { createWorker } from 'https://cdn.skypack.dev/tesseract.js@4.1.2?dts'

const worker = await createWorker()
// The above line throws the following error.
//
// error: Uncaught TypeError: Cannot read properties of undefined (reading 'href')
// const resolveURL = isBrowser ? (s) => new URL(s, window.location.href).href : (s) => s;
//                                                                  ^
//     at resolveURL (https://cdn.skypack.dev/-/tesseract.js@v4.1.2-tN0T6LCzDNBo5QPjk3os/dist=es2019,mode=imports/optimized/tesseractjs.js:313:66)
//     at https://cdn.skypack.dev/-/tesseract.js@v4.1.2-tN0T6LCzDNBo5QPjk3os/dist=es2019,mode=imports/optimized/tesseractjs.js:318:19
//     at Array.forEach (<anonymous>)
//     at resolvePaths (https://cdn.skypack.dev/-/tesseract.js@v4.1.2-tN0T6LCzDNBo5QPjk3os/dist=es2019,mode=imports/optimized/tesseractjs.js:316:42)
//     at createWorker (https://cdn.skypack.dev/-/tesseract.js@v4.1.2-tN0T6LCzDNBo5QPjk3os/dist=es2019,mode=imports/optimized/tesseractjs.js:606:7)
//     at file:///Users/ynkt/tmp/tesseract-deno/main.ts:3:22

Unlike Node.js, Deno has window global so Tesseract.js misunderstands it's running in browsers. As Remix's doc suggests, checks for document is preferred for this reason. This check is also backward compatible in other environments.

I don't claim first-class support for Deno (of course, I'd be happy if you do), but I just wish if I could use Tesseract.js in Deno in some way.

You can find similar PRs merged at other popular libraries like framer/motion#1522, TanStack/virtual#516, melt-ui/melt-ui#14, and chakra-ui/zag#694.

@Balearica
Copy link
Collaborator

Balearica commented Sep 11, 2023

Thank you for your contribution. Merging. This will be reflected in the next npm release, which will be 4.1.3.

@Balearica Balearica merged commit d23a8d5 into naptha:master Sep 11, 2023
6 checks passed
@yudai-nkt yudai-nkt deleted the deno-compatible-browser-detection branch September 28, 2023 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants