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

Open
1 task
cworld1 opened this issue Dec 3, 2024 · 14 comments
Open
1 task

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

cworld1 opened this issue Dec 3, 2024 · 14 comments
Labels
needs triage Issue needs to be triaged

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.

But I managed to fix it, apparently it was my mistake.

@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.🙏

@bluwy bluwy reopened this Jan 6, 2025
@bluwy bluwy removed the needs repro Issue needs a reproduction label Jan 6, 2025
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 6, 2025
@mseele
Copy link

mseele commented Jan 8, 2025

Please see https://github.com/mseele/sve-website/tree/next for reproduction. Checkout and build will fail:

❯ bb  
$ astro build
17:31:59 [WARN] [config] The adapter @astrojs/netlify provides experimental support for "astro:env getSecret". You may experience issues or breaking changes until this feature is fully supported by the adapter.
17:31:59 [vite] Re-optimizing dependencies because lockfile has changed
17:31:59 [content] Syncing content
17:31:59 [content] Astro version changed
17:31:59 [content] Clearing content store
17:32:00 [content] Synced content
17:32:00 [types] Generated 956ms
17:32:00 [build] output: "server"
17:32:00 [build] directory: /Users/mseele/dev/sve/sve-website/dist/
17:32:00 [build] adapter: @astrojs/netlify
17:32:00 [build] Collecting build info...
17:32:00 [build] ✓ Completed in 1.01s.
17:32:00 [build] Building server entrypoints...
17:32:02 [astro-icon] Loaded icons from src/assets/icons, mdi
17:32:02 [vite] ✓ built in 2.56s
17:32:02 [build] ✓ Completed in 2.58s.

 building client (vite) 
17:32:02 [vite] ✓ 29 modules transformed.
17:32:02 [ERROR] [vite] x Build failed in 55ms
transforming (447) node_modules/.pnpm/[email protected]/node_modules/date-fns/locale/cs/_lib/localize.js[ServerOnlyModule] [astro-content-virtual-mod-plugin] Could not load astro:content (imported by src/types.ts): The "astro:content" module is only available server-side.
  Error reference:
    https://docs.astro.build/en/reference/errors/server-only-module/
  Stack trace:
    at generateContentEntryFile (file:///Users/mseele/dev/sve/sve-website/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:199:11)
    at Object.handler (file:///Users/mseele/dev/sve/sve-website/node_modules/vite/dist/node/chunks/dep-BJP6rrE_.js:51141:15)
    at async PluginDriver.hookFirstAndGetPlugin (file:///Users/mseele/dev/sve/sve-website/node_modules/rollup/dist/es/shared/node-entry.js:19953:28)
    at async Queue.work (file:///Users/mseele/dev/sve/sve-website/node_modules/rollup/dist/es/shared/node-entry.js:20163:32)
error: script "build" exited with code 1

That code worked before.

@mseele
Copy link

mseele commented Jan 8, 2025

I've digged down deeper. It's this line of code that triggers the error: https://github.com/mseele/sve-website/blob/ec75d17a182fbca49c1a04eed844ba659f60116c/src/types.ts#L2

import { z } from 'astro:content'

mseele added a commit to mseele/sve-website that referenced this issue Jan 8, 2025
@cworld1
Copy link
Author

cworld1 commented Jan 9, 2025

@mseele Zod is a classical module which should be used for server side. This has been told at official docs. I suggest you to use Astro integration instead, which also can be processed and import by Vite.
Example: https://github.com/cworld1/astro-theme-pure/blob/v4.0.1-beta/packages/pure/index.ts where I process user configuration.

@moon0101
Copy link

moon0101 commented Jan 9, 2025

I fixed the The "astro:content" module is only available server-side. in my setup. In the Upgrade guide to Astro 5, you can read that astro:content is no longer available on the client side (https://docs.astro.build/en/guides/upgrade-to/v5/#changed-astrocontent-can-no-longer-be-used-on-the-client).
To fix the issue you need to find the locations where you import astro:content properties outside of this scope.
In my case I used the getCollection method in a js file to get the collection and return filtered posts. Moving getCollection() into the [bracket].astro files and rewrite the contentFilter.js file did the trick for this specific error. It surfaced other issues with the updated content collection API but this is subject to another issue.

  1. Find the locations where you import astro:content
  2. move it into a corresponding astro parent file
  3. hand down the collection data as prop

@cworld1
Copy link
Author

cworld1 commented Jan 9, 2025

@moon0101 It is not one easy thing. The more interaction & integration you add, the more difficulties you will face. As an example https://github.com/cworld1/astro-theme-pure/blob/v3.1.4/src/components/search/Search.astro the integration Fuse.js will not be supported easily.

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

No branches or pull requests

7 participants