Adapter-static not putting CSS referenced assets in the right place
See original GitHub issueDescribe the bug
Resources linked to from CSS (i.e. background-image: url(...)) are not found after building a SvelteKit site with adapter-static.
The following works in vite (npm run dev) but not after building the site. One could work around it with absolute URLs, yet it’s pretty annoying with many assets.
Reproduction
Create these files:
-
A random image at
static/wallpaper.jpg -
A component
src/lib/components/something.svelte:
<div class="paper">something</div>
<style lang="postcss">
.paper {
background-image: url(./wallpaper.jpg)
}
</style>
- Use the component in
src/routes/__layout.svelte:
<script>
import Something from "$lib/components/something.svelte"
</script>
<Something />
<slot />
- config (
svelte.config.js):
import preprocess from 'svelte-preprocess'
// import adapter from '@sveltejs/adapter-auto'
import adapter from '@sveltejs/adapter-static'
import path from 'path'
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
prerender: { default: true },
},
preprocess: [preprocess({ postcss: true })]
}
export default config
- Build the app with adapter-static (
npm build) and run a test HTTP server:
python3 -m http.server 9000 --directory build
- Observe in your browser’s dev tools that the request to
wallpaper.jpgis 404ing since it goes tohttp://localhost/_app/assets/pages/wallpaper.jpginstead of the ‘http://localhost/wallpaper.jpg’
Logs
No response
System Info
System:
OS: Linux 5.16 Arch Linux
CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
Memory: 50.72 GB / 62.78 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 17.7.1 - /usr/bin/node
npm: 8.5.4 - /usr/bin/npm
Browsers:
Brave Browser: 99.1.36.112
Firefox: 98.0
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.31
@sveltejs/adapter-static: ^1.0.0-next.29 => 1.0.0-next.29
@sveltejs/kit: next => 1.0.0-next.295
svelte: ^3.46.4 => 3.46.4
Severity
serious, but I can work around it
Additional Information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to refer to static files in my css files? - Stack Overflow
Use a relative path. Relative to the folder where the css file reside.
Read more >Adapters • Docs • SvelteKit
Adapters are small plugins that take the built app as input and generate output for deployment. By default, projects are configured to use...
Read more >Untitled
Sveltekit static. These assets will simply be copied and not go through webpack. The routes of your app — i. Another option (I...
Read more >@sveltejs/adapter-static | Yarn - Package Manager
Adapter for SvelteKit apps that prerenders your entire site as a collection of static files. If you'd like to prerender only some pages,...
Read more >Let's learn SvelteKit by building a static Markdown blog from ...
man I just tried to hello world SvelteKit and it is NOT for me ... Inside our new Header component, let's place some...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

note that
url()values in style blocks of svelte components are not processed by default, see https://kit.svelte.dev/docs/assets#importingIf you want to reference an asset placed in the static directory in css without additional processing, it should be done with an absolute path
/image.png.I can’t reproduce this, with or without postcss, so I’m going to close this issue. It’s possible that it works now, but without a repro there’s no way of knowing.