Skip to content
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

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

Closed
1 task
cworld1 opened this issue Dec 3, 2024 · 9 comments
Closed
1 task

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

cworld1 opened this issue Dec 3, 2024 · 9 comments
Labels
needs repro Issue needs a reproduction

Comments

@cworld1
Copy link

cworld1 commented Dec 3, 2024

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

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 3, 2024
@bluwy bluwy added the needs repro Issue needs a reproduction label Dec 4, 2024
Copy link
Contributor

github-actions bot commented Dec 4, 2024

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.

@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Dec 4, 2024
@cworld1
Copy link
Author

cworld1 commented Dec 7, 2024

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

@cworld1
Copy link
Author

cworld1 commented Dec 7, 2024

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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/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/[email protected]/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/[email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:40786:22)
    at async viteTransformMiddleware (file:///D:/code/test/astro-blog-test/node_modules/.pnpm/[email protected]/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"
> [email protected] 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/[email protected][email protected][email protected]/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:195:11)
    at Object.handler (file:///vercel/path0/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50826:15)
    at async PluginDriver.hookFirstAndGetPlugin (file:///vercel/path0/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20712:28)
    at async Queue.work (file:///vercel/path0/node_modules/.pnpm/[email protected]/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

@Jazee6
Copy link

Jazee6 commented Dec 9, 2024

same

@cworld1
Copy link
Author

cworld1 commented Dec 9, 2024

@Jazee6 A temporary solution is split out the utility scripts to another file, like server.ts (you can check an example at https://github.com/cworld1/astro-theme-pure/blob/v4.0.1-beta/packages/pure/utils/server.ts), then only call on functions using this file instead.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2024
@bernatGene
Copy link

facing the same issue, but I'm unable to pin down the exact issue and/or how to split the utilities file into two as suggested above. The issue came up after trying to upgrade to v5 and merging some changes from an upstream repo.

@FJRG2007
Copy link

FJRG2007 commented Jan 3, 2025

I also get this error after migrating to Astro 5.

@moon0101
Copy link

moon0101 commented Jan 6, 2025

Same error here after migrating to Astro 5.

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

@cworld1
Copy link
Author

cworld1 commented Jan 6, 2025

@bluwy It is a known issue and should not be closed without an official solution. Can u consider it again? So many developers faced the same problem.🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue needs a reproduction
Projects
None yet
Development

No branches or pull requests

6 participants