-
Notifications
You must be signed in to change notification settings - Fork 96
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
Cannot use 'import.meta' outside a module when building a ServiceWorker script #930
Comments
I randomly had a look at the line of code you are referencing. Are you bundling your SW using ES modules? |
Yes, I used the ES module for packaging. I spent half a day resolving the issue and was only able to package successfully after modifying the agent file in node_modules. |
Maybe it's specific to service worker? We do use the agent in web workers in Oisy for example and we do not have issue. That's why I was asking about ES modules. |
Thank you for following up. |
It's clear that if the |
Oh yeah we had that issue in Oisy too - i.e. Did you check if |
Are you using That would explain why we do not face the issue in Oisy. |
We use agent-js in service worker to fetch latest data for notifications, some code like this:
|
Weird, I’m a bit out of ideas... |
In which of your repo are you facing the issue? Can I give it a try? |
I spent last hour trying to reproduce the issue with your repo, but I'm having trouble loading the SW. I try to mock and remove some data to achieve the goal but, always ends facing some issue with bundling. I'll stop here for now, but if you're able to reproduce the issue with a sample repo, I'd be happy to give it another try. |
Sorry to take up your time @peterpeterparker I’ve created a test project that can reproduce the issue. It’s confirmed that after introducing SvelteKit, the I’m not sure what configuration causes this issue. My current workaround is to delete the This seems to be a specific issue and it’s probably not worth taking more of your time to diagnose it. Merging #931 should fully resolve it. Working branch: git clone https://github.com/zensh/agent-sw
cd agent-sw
pnpm i
pnpm build
pnpm preview Branch with the issue after introducing SvelteKit: git checkout feat/kit
pnpm i
pnpm dev // and then stop to generate the .svelte-kit directory
pnpm build
pnpm preview |
Thanks, @zensh, for the repo. I can reproduce the issue as well and couldn't find any configuration option to resolve it. While the issue is not addressed yet, I may have a workaround to suggest. Instead of letting the bundler pick the appropriate module, you can try forcing the import to point to
As for your PR, I can't say for sure and still unsure about the root cause. I also don't collaborate to agent-js, just jumped in because I use worker in Oisy Wallet or Juno. I guess @krpeacock, who's currently on PTO, will take a look when they return. |
https://github.com/dfinity/agent-js/blob/main/packages/agent/src/agent/http/index.ts#L1106
await import
is incompatible with the ServiceWorker script. This can cause the error: "Uncaught SyntaxError: Cannot use 'import.meta' outside a module."We encountered this issue while building the ICPanda frontend application. Removing the
await import
here resolved the ServiceWorker script build.https://github.com/ldclabs/ic-panda/blob/main/src/ic_panda_frontend/src/service-worker.ts#L11
The text was updated successfully, but these errors were encountered: