withastro / astro

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

Assets in public failing to resolve during dev with Node 23 #12260

Open ssalbdivad opened 5 days ago

ssalbdivad commented 5 days ago

Astro Info

Astro                    v4.16.6
Node                     v23.0.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/starlight
                         @astrojs/react

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

No response

Describe the Bug

After a recent upgrade, my references to assets in public have been failing to resolve in dev:

15:49:10 [WARN] [router] A `getStaticPaths()` route pattern was matched, but no matching static path was found for requested path `/copy.svg`.

Possible dynamic routes being matched: ../../node_modules/.pnpm/@astrojs+starlight@0.28.3_astro@4.16.5_@types+node@22.7.5_rollup@4.24.0_typescript@5.7.0-beta_/node_modules/@astrojs/starlight/routes/static/index.astro.
15:49:10 [404] /copy.svg 14ms

Those assets are also broken when rendered in the app itself.

I'm currently on the latest astro (4.16.5) and @astrojs/starlight (0.28.3).

When run from dist or in prod, the path resolves correctly.

A minimal repro from @kitschpatrol:

mkdir public-test
cd public-test

pnpm create astro@latest . --yes

pnpm run dev

curl -s -o /dev/null -w "%{http_code}" http://localhost:4321/favicon.svg

@kitschpatrol Found the following workaround:

import { defineConfig } from "astro/config";

export default defineConfig({
  vite: {
    publicDir: "public",
  },
});

What's the expected result?

Assets are rendered normally without a warning.

Link to Minimal Reproducible Example

Didn't repro when I tried in StackBlitz, instructions above

Participation

kitschpatrol commented 5 days ago

Thanks ssalbdivad!

I think this might have something to do with the trailing slash Astro is using in the absolute path to the public folder...

Running astro dev --verbose (without any modifications to the Astro config file) shows that Astro is setting the following absolute path (obviously unique to my machine):

vite:config     publicDir: '/Users/mika/Desktop/astro-asset-test/public/',

With this config, http://localhost:4321/favicon.svg 404s.

If I drop the trailing slash in an Astro config Vite override:

import { defineConfig } from "astro/config";

export default defineConfig({
  vite: {
    publicDir: "/Users/mika/Desktop/astro-asset-test/public",
  },
});

Then http://localhost:4321/favicon.svg resolves correctly.

kitschpatrol commented 4 days ago

Quick update:

I failed to notice / mention that I was running on Node 23... (it auto-updated and I wasn't paying attention). 🤦‍♂️

For me at least, non-resolution of /public assets in astro preview and astro dev with a default astro.config is only happening with Node 23. Rolling back and running against Node 22.9.0 works fine.