question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adapter-static not putting CSS referenced assets in the right place

See original GitHub issue

Describe 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.jpg is 404ing since it goes to http://localhost/_app/assets/pages/wallpaper.jpg instead 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dominikgcommented, Mar 30, 2022

note that url() values in style blocks of svelte components are not processed by default, see https://kit.svelte.dev/docs/assets#importing

If 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.

0reactions
Rich-Harriscommented, Apr 25, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found