kit icon indicating copy to clipboard operation
kit copied to clipboard

Assets are not included for the server (adapter-node)

Open maximedupre opened this issue 2 years ago • 6 comments

Describe the bug

Bug

If you import an image on the server (e.g. in an endpoint), the file path will point to a non-existent file.

import Image from '$lib/svelte-welcome.webp';

console.log(Image); // /_app/immutable/assets/svelte-welcome-c18bcf5a.webp

When looking in /build/client/_app/immutable/assets folder, the file is not included.

Use case

  1. I am using node-canvas on the server and in this case, I need to place an image on the canvas, so I need the image path
  2. This is not an asset that I want to share publically, which is why I don't want to have it in the static folder

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-eromzr?file=src/routes/todos/_api.js

  1. Run npm run build
  2. Run node build
  3. Click on the "Todos" link
  4. See image path in the console

Logs

No response

System Info

This command doesn't seem to work on StackBlitz 😄

Severity

serious, but I can work around it

Additional Information

I guess I can work around it by having all assets in the static folder, which is included in the build correctly.

But then all the assets are public. So then I guess I can have some sort of logic in hooks.ts that would prevent access to those files.

maximedupre avatar Jun 22 '22 10:06 maximedupre

This is a serious issue ! Because assets are now considered as route by the layout... so it throws a 404 and even could erase $page.stuff if you use it for metadata in your __error.svelte

7antra avatar Jun 29 '22 15:06 7antra

https://github.com/sveltejs/kit/pull/4974

7antra avatar Jun 29 '22 15:06 7antra

possibly related: #1567

Rich-Harris avatar Jul 20 '22 16:07 Rich-Harris

I just got this issue using @sveltejs/[email protected] and my auto deploys (using adapter-auto) on vercel started throwing 404 for missing static files.

Here's my package.json

{
	"name": "parshandata-frontend",
	"version": "0.0.1",
	"scripts": {
		"dev": "svelte-kit dev",
		"build": "svelte-kit build",
		"package": "svelte-kit package",
		"preview": "svelte-kit preview",
		"prepare": "svelte-kit sync",
		"check": "svelte-check --tsconfig ./tsconfig.json",
		"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
		"lint": "prettier --check --plugin-search-dir=. .",
		"format": "prettier --write --plugin-search-dir=. ."
	},
	"devDependencies": {
		"@sveltejs/adapter-auto": "^1.0.0-next.64",
		"@sveltejs/adapter-static": "^1.0.0-next.39",
		"@sveltejs/kit": "next",
		"@types/cookie": "^0.5.1",
		"prettier": "^2.6.2",
		"prettier-plugin-svelte": "^2.7.0",
		"svelte": "^3.46.0",
		"svelte-check": "^2.7.1",
		"svelte-preprocess": "^4.10.6",
		"tslib": "^2.3.1",
		"typescript": "^4.7.2"
	},
	"type": "module",
	"dependencies": {
		"cookie": "^0.4.1"
	}
}

nmfrankel avatar Aug 11 '22 19:08 nmfrankel

Everything is now working after doing npm update.

I got the following message with directions to change config

svelte-kit build is no longer available — use vite build instead

All good 😊

nmfrankel avatar Aug 11 '22 20:08 nmfrankel

Opened https://github.com/vitejs/vite/issues/11429

Rich-Harris avatar Dec 19 '22 20:12 Rich-Harris

Does a workaround exist?

LukaHarambasic avatar Jan 18 '23 09:01 LukaHarambasic

Yes, import the assets somewhere in your client code. As long as you don't use them in client code, the paths will be treeshaken away so they won't impact your client-side bundle size.

Rich-Harris avatar Jan 20 '23 19:01 Rich-Harris

I just tried vite 4.1.0-beta.1 which should have the fix from https://github.com/vitejs/vite/issues/11429 and my similar error still occurs, are others still seeing the same thing?

For reference, my issue is a file static/graphic.svg referenced once in one component as a css background url.

yottapanda avatar Jan 26 '23 23:01 yottapanda

@thechubbypanda I'm also still facing the error with vite 4.1.0-beta.1, but I don't think it includes the fix, since it's not on the changelog document yet.

Edit: seems like it was added to the changelog (as #11430) but the error still occurs with Vite 4.1.1

matfantinel avatar Jan 31 '23 22:01 matfantinel

I had a similar issue with production. I used as background-image: url('images/hexagon.svg') in the <style></style>, but was not getting the image in the production. It was searching in _app/immutable.

But when i put the url as variable in the script, and did an inline styling lin style="background-image: url({src})" it worked just fine! This example helped me.

image image

mahi160 avatar Feb 15 '23 17:02 mahi160

Vite 4.1 will copy the server assets to .sveltekit/output/server, but we need to update the builder used by the adapter to copy the assets from both the server and client to the final output directory

benmccann avatar Feb 16 '23 15:02 benmccann