withastro / astro

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

assetsInlineLimit doesn't work for client-only scripts #10548

Closed aminya closed 5 months ago

aminya commented 5 months ago

Astro Info

SSL certificate found. The server will run in HTTPS mode.
Astro                    v4.5.9
Node                     v20.2.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/solid-js
                         @astrojs/tailwind

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

No response

Describe the Bug

I am using Solidjs for my client-side scripts, but Astro doesn't respect my assetInlineLimit size, and it creates separate JavaScript files in the dist/_astro folder

 [vite] dist/_astro/logo.De6d2APk.svg        0.46 kB │ gzip:  0.26 kB
 [vite] dist/_astro/Component1.DRflpVO5.js   0.14 kB │ gzip:  0.14 kB
 [vite] dist/_astro/Component2.DK5iz3Tl.js   0.74 kB │ gzip:  0.45 kB
 [vite] dist/_astro/client.BhZioPRu.js       0.90 kB │ gzip:  0.56 kB
 [vite] dist/_astro/solid.DJlj9KZv.js        10.03 kB │ gzip:  4.19 kB

What's the expected result?

Astro should inline scripts smaller than 4KB https://vitejs.dev/config/build-options.html#build-assetsinlinelimit

Link to Minimal Reproducible Example

https://stackblitz.com/github/withastro/astro/tree/latest/examples/framework-solid?file=dist%2F_astro%2FCounter.CL2edFNV.js

The default Solid-js template has this issue. Just run pnpm build

 building client (vite) 
01:19:30 [vite] ✓ 6 modules transformed.
01:19:30 [vite] dist/_astro/index.2cY3e-ie.css    0.15 kB │ gzip: 0.14 kB
01:19:30 [vite] dist/_astro/Counter.CL2edFNV.js   0.47 kB │ gzip: 0.31 kB
01:19:30 [vite] dist/_astro/client.TvExYywe.js    0.87 kB │ gzip: 0.55 kB
01:19:30 [vite] dist/_astro/web.2uYXDb4E.js      10.70 kB │ gzip: 4.40 kB
01:19:30 [vite] ✓ built in 262ms

Participation

lilnasy commented 5 months ago

Do you need this for content security policies?

lilnasy commented 5 months ago

I misunderstood the issue originally.

@aminya You can expect astro/vite to inline assets smaller than 4kB if the page directly references them. In this case, the files you are showing come from framework components. They are requested only when the client directive (visible, load, idle) says they are needed, they are never requested immediately.

Closing as this would be expected behavior. Feel free to re-open if you think that is not the case.

aminya commented 5 months ago

I expect the scripts to be inlined if they are smaller than 4 KB regardless. For example, in the load mode, the scripts should be available immediately. @lilnasy

lilnasy commented 5 months ago

You are right, the load directive means that the component could be included directly. However, it still follows the same architecture as the other directives - it is dynamically imported using its external url.

aminya commented 5 months ago

Yeah, so maybe the change here should be to normally import Scripts whenever load is used.

lilnasy commented 5 months ago

Yes, that is a potential future. It currently does not work that way and code-wise, it would be messy to make an exception for load.