astro-community / worker

Use Web Workers in Astro
15 stars 4 forks source link

Cannot read properties of undefined (reading 'ssr') #1

Closed IrvingArmenta closed 2 months ago

IrvingArmenta commented 7 months ago

I am getting an error when running astro build + astro preview and it seems is related to this package.

my astro config file:

import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import sitemap from '@astrojs/sitemap';
import biome from 'astro-biome';
import worker from '@astropub/worker';

// https://astro.build/config
export default defineConfig({
  site: 'https://example.com',
  integrations: [mdx(), sitemap(), worker(), biome()],
});

Error message:

16:23:50 [vite] ✓ 3 modules transformed.
16:23:50 [ERROR] [vite] x Build failed in 18ms
[at position 40] Cannot read properties of undefined (reading 'ssr')
file: ///IntroPhotoSection.astro?astro&type=script&index=0&lang.ts
  Stack trace:
    at Object.handler (///node_modules/.pnpm/@astropub+worker@0.1.0/node_modules/@astropub/worker/integration.js:20:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

shortened some paths for privacy

Any other information I might be able to provide:

packages:

  "dependencies": {
    "@astrojs/check": "^0.5.6",
    "@astrojs/mdx": "^2.1.1",
    "@astrojs/rss": "^4.0.5",
    "@astrojs/sitemap": "^3.1.1",
    "@astropub/worker": "^0.1.0",
    "astro": "^4.4.5",
    "astro-biome": "^0.0.6",
    "typescript": "^5.3.3"
  },
  "devDependencies": {
    "@biomejs/biome": "1.5.3",
    "sass": "^1.71.1",
    "sharp": "^0.33.2",
    "typescript-esbuild": "^0.3.10"
  }

platform: Windows 11 node version: v20.10.0

nerdoza commented 6 months ago

This can be easily corrected by adding a check to the options param in the integration.js file.

// patch for globalThis.Worker
if (options && options.ssr) {                                
 source.appendLeft(0, `import "@astropub/worker/polyfill";`)
}

Even with this error fixed, the plugin doesn't move the worker files to the dist directory, so the files are not available for the client to utilize.

It does appear that Astro will pick up the files required for the Worker and compile them, however, they don't seem to respect the build.assets configuration and will be output to the dist/assets directory regardless of configuration. I'll open an issue with Astro for this bug.


If all you want is to use workers on the client side, you don't need this integration, just add the worker to your code.

const worker = new Worker(
  new URL('../workers/myWorker.ts', import.meta.url)
)
mariusbolik commented 1 month ago

Can you publish this fix on npm?

jonathantneal commented 1 month ago

Yes! Expect an update today.

jonathantneal commented 1 month ago

Published as 0.2.0