plasmo icon indicating copy to clipboard operation
plasmo copied to clipboard

[BUG] runtime.sendMessage does not await in production environment

Open imckl opened this issue 1 year ago • 5 comments

What happened?

I'm trying to use fetch in service worker but encounter this issue when deploying to production environment.

The following sample-code works in development (plasmo dev), but does not work in production environment, which builds by plasmo build Also I've tried plasmo build --no-hoist with no luck

// in content script (contents/page.ts)
async function sendMessageTest() {
    const resp = await chrome.runtime.sendMessage({command: 'Test'})
    console.log(resp)
}
// in service worker (background/index.ts)
chrome.runtime.onMessage(async (message, sender, sendResponse) => {
    console.log('start wait')
    await delay(1000 * 10)
    sendResponse({status: 'ok'})
    console.log('done')
})

In production, sendMessage returns undefined immediately without awaiting until sendResponse gets called. I think it might have something to do with the parcel bundle process?

Version

Latest

What OS are you seeing the problem on?

Windows

What browsers are you seeing the problem on?

Chrome, Microsoft Edge

Relevant log output

No response

(OPTIONAL) Contribution

  • [ ] I would like to fix this BUG via a PR

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct
  • [X] I checked the current issues for duplicate problems.

imckl avatar Oct 11 '23 02:10 imckl

After some digging, I find it works in production if adding some Plasmo Messaging code in service worker

// in background/message/test.ts
import type { PlasmoMessaging } from "@plasmohq/messaging"

// just dummy handler does nothing
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
}

export default handler

For now I add test.ts but does not use it in any code and then runtime.sendMessage works in production environment, as a workaround. Not usre what's the issue with that.

imckl avatar Oct 11 '23 11:10 imckl