Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

need help: Promise.withResolvers is not a function #127

Closed
DidoFitch opened this issue Jul 10, 2024 · 7 comments
Closed

need help: Promise.withResolvers is not a function #127

DidoFitch opened this issue Jul 10, 2024 · 7 comments

Comments

@DidoFitch
Copy link

Hello, this issue is not a bug but I need some help.

I‘m using version 1.10.0 of this repository, and in older versions of Chrome (earlier than 105), Promise.withResolvers is not supported, causing an error when I preview PDFs: Promise.withResolvers is not a function.

I tried using Babel to support it or ask ChatGPT, but it didn't work. Can anyone help me?

Snipaste_2024-07-10_09-47-39

I have tried this:

yarn add @babel/core @babel/preset-env @vitejs/plugin-vue vite-plugin-babel -D

yarn add promise-polyfill

vite.config.js:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import viteBabel from 'vite-plugin-babel';

export default defineConfig({
  plugins: [
    vue(),
    viteBabel()
  ]
});

main.js:

import 'promise-polyfill/src/polyfill';
if (!Promise.withResolvers) {
  Promise.withResolvers = function() {
    let resolve, reject;
    const promise = new Promise((res, rej) => {
      resolve = res;
      reject = rej;
    });
    return { promise, resolve, reject };
  };
}
@TaTo30
Copy link
Owner

TaTo30 commented Jul 14, 2024

After checking this I found this problem on two points:
imagen

The first one is from pdf.js file and can be patched using a polyfill or the code @DidoFitch shared.

The second one is from the pdf.worker.js and cannot be polyfilled surely because the worker is running on different thread and context. In this library the worker is embebed directly:

import PDFWorker from 'pdfjs-dist/build/pdf.worker.min?url'

Using the legacy worker fix the second point but It's still required to add the polyfill for the first point.

import PDFWorker from 'pdfjs-dist/legacy/build/pdf.worker.min?url'

@TaTo30
Copy link
Owner

TaTo30 commented Jul 14, 2024

This should works, It's important configure the legacy worker before using usePDF:

<script setup lang="ts">
+ import * as PDFJS from 'pdfjs-dist';
+ import LegacyWorker from 'pdfjs-dist/legacy/build/pdf.worker.min?url';
import { VuePDF, usePDF } from '@tato30/vue-pdf';

+ PDFJS.GlobalWorkerOptions.workerSrc = LegacyWorker

const { pdf } = usePDF('https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf')
</script>

<template>
  <div>
    <VuePDF :pdf="pdf" />
  </div>
</template>

@ChiefTechDev
Copy link

Hi TaTo!
I need you help
Could you tell me how can I fix this deployment issue?
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

@ChiefTechDev
Copy link

ChiefTechDev commented Jul 17, 2024

I am sorry to border you.
I fixed this problem.
`
...

  default_type application/octet-stream;
  include  /etc/nginx/mime.types;
  # include the server config for your application
  include /etc/nginx/app.conf;

  types {
      text/javascript                 js;
      application/javascript          mjs;
  }

...
`
I updated nginx.conf file.
I leave code of nginx.conf file for other developers
Thank you for your time.

@ZumelzuR
Copy link

ZumelzuR commented Jul 26, 2024

There are PR for this?

@TaTo30
Copy link
Owner

TaTo30 commented Jul 29, 2024

@ZumelzuR Nope, this issue can be fixed using #127 (comment)

It is not necessary to do something on source code.

@TaTo30 TaTo30 closed this as completed Jul 29, 2024
@ZumelzuR
Copy link

@ZumelzuR Nope, this issue can be fixed using #127 (comment)

It is not necessary to do something on source code.

I saw that there are some code in master (that is doing this PDFJS.GlobalWorkerOptions.workerSrc = LegacyWorker) but is there is not a compiled version of this. Would be great have this master changes on a compiled version :)

Repository owner locked and limited conversation to collaborators Aug 17, 2024
@TaTo30 TaTo30 converted this issue into discussion #136 Aug 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants