withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
47.45k stars 2.52k forks source link

Script cannot be detected as server or static mode on Astro v5.x #12609

Open cworld1 opened 4 days ago

cworld1 commented 4 days ago

Astro Info

$ astro info
Astro                    v5.0.1
Node                     v23.2.0
System                   Windows (x64)
Package Manager          bun
Output                   server
Adapter                  @astrojs/vercel
Integrations             @astrojs/tailwind
                         @astrojs/sitemap
                         @astrojs/mdx
                         astro-icon

If this issue only occurs in one browser, which browser is a problem?

Firefox

Describe the Bug

For file src/utils.ts

export function A() {
  // Used in dynamic astro page P
}

export function B() {
  // Used as a static typescript function in a simple component S using <script>import xxx</script>
}

but when component S inserted in dynamic server page P, astro seems cannot split out scripts and shows warnings.

Note: this method can work on Astro v4.x

The "astro:content" module is only available server-side.
Stack Trace
at generateContentEntryFile (file:///D:/code/web/astro-theme-pure/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:197:11)
    at LoadPluginContext.load (file:///D:/code/web/astro-theme-pure/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:93:28)
    at EnvironmentPluginContainer.load (file:///D:/code/web/astro-theme-pure/node_modules/vite/dist/node/chunks/dep-A4nAWF7x.js:46981:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async loadAndTransform (file:///D:/code/web/astro-theme-pure/node_modules/vite/dist/node/chunks/dep-A4nAWF7x.js:40782:22)
    at async viteTransformMiddleware (file:///D:/code/web/astro-theme-pure/node_modules/vite/dist/node/chunks/dep-A4nAWF7x.js:42292:24)

What's the expected result?

// src/utils.ts
export function A() {
  // Used in dynamic astro page P
}

export function B() {
  // Used as a static typescript function in a simple component S using <script>import xxx</script>
}

When component S inserted in dynamic server page P, astro can work well.

Link to Minimal Reproducible Example

https://github.com/cworld1/astro-blog-test/tree/utils

Participation

github-actions[bot] commented 3 days ago

Hello @cworld1. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

cworld1 commented 12 hours ago

Well! Here's the minimal reproduction: https://github.com/cworld1/astro-blog-test/tree/utils (Main branch is about another issue)

cworld1 commented 12 hours ago

Error: The "astro:content" module is only available server-side.

Error info on AstroError:

at generateContentEntryFile (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/astro@5.0.3_rollup@4.28.0_typescript@5.7.2/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:195:11)
    at LoadPluginContext.load (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/astro@5.0.3_rollup@4.28.0_typescript@5.7.2/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:91:28)
    at EnvironmentPluginContainer.load (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:46974:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async loadAndTransform (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:40786:22)
    at async viteTransformMiddleware (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:42296:24)

Error info on build (currently on Vercel, and locally deploy is the same):

Running "pnpm run build"
> astro-blog-test@0.0.1 build /vercel/path0
> astro build
08:19:30 [content] Syncing content
08:19:30 [content] Synced content
08:19:30 [types] Generated 632ms
08:19:30 [build] output: "server"
08:19:30 [build] directory: /vercel/path0/dist/
08:19:30 [build] adapter: @astrojs/vercel
08:19:30 [build] Collecting build info...
08:19:30 [build] ✓ Completed in 665ms.
08:19:30 [build] Building server entrypoints...
08:19:32 [WARN] [router] getStaticPaths() ignored in dynamic page /src/pages/blog/[...slug].astro. Add `export const prerender = true;` to prerender the page as static HTML during the build process.
08:19:33 [vite] ✓ built in 3.52s
08:19:33 [build] ✓ Completed in 3.56s.

 building client (vite) 
08:19:34 [vite] transforming...
08:19:34 [vite] ✓ 2 modules transformed.
08:19:34 [ERROR] [vite] x Build failed in 28ms
[ServerOnlyModule] [astro-content-virtual-mod-plugin] Could not load 
  Error reference:
    https://docs.astro.build/en/reference/errors/server-only-module/
  Stack trace:
    at generateContentEntryFile (file:///vercel/path0/node_modules/.pnpm/astro@5.0.3_rollup@4.28.0_typescript@5.7.2/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:195:11)
    at Object.handler (file:///vercel/path0/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50826:15)
    at async PluginDriver.hookFirstAndGetPlugin (file:///vercel/path0/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js:20712:28)
    at async Queue.work (file:///vercel/path0/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js:20922:32)
 ELIFECYCLE  Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1