Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prerendering does not crawl imported images outside of pages #1567

Closed
PingTouG opened this issue May 27, 2021 · 4 comments
Closed

prerendering does not crawl imported images outside of pages #1567

PingTouG opened this issue May 27, 2021 · 4 comments
Labels
bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Milestone

Comments

@PingTouG
Copy link

PingTouG commented May 27, 2021

Describe the bug
routes/todos/index.json.ts imports an image. When using @sveltejs/adapter-static, npm run build produces a 404 error

Logs

404 /_app/assets/industry_1.166d527b.png (linked from /todos)

To Reproduce
Checkout https://github.com/PingTouG/svelte-kit-bug. An image was added to the todos tab as shown below

src/routes/todos/index.json.ts

import welcomeImg from '$lib/images/home/industry_1.png';
....
export const get: RequestHandler<Locals> = async (request) => {
....
return { body: { welcomeImg  } }

src/routes/todos/index.svelte

....
<img src={welcomeImg} />
.....

Expected behavior
npm run build is ok

Information about your SvelteKit Installation:

Diagnostics
  • @sveltejs/kit : next

  • @sveltejs/adapter-static : ^1.0.0-next.11

  • svelte : ^3.34.0

  • chorme

Severity
The website cannot run

@benmccann benmccann changed the title @sveltejs/adapter-static build error npm run build with @sveltejs/adapter-static results in 404 error for image file May 27, 2021
@benmccann benmccann changed the title npm run build with @sveltejs/adapter-static results in 404 error for image file npm run build with static adapter gives 404 error for image file May 27, 2021
@benmccann benmccann changed the title npm run build with static adapter gives 404 error for image file npm run build with adapter-static gives 404 error for image file May 27, 2021
@ignatiusmb
Copy link
Member

This only happens for prerendered pages. Images imported images outside of pages, in this case an endpoint, does not get crawled. The error would also appear if we use adapter-node and set export const prerender = true;

@ignatiusmb ignatiusmb changed the title npm run build with adapter-static gives 404 error for image file prerendered pages does not crawl imported images outside of pages Aug 22, 2021
@ignatiusmb ignatiusmb changed the title prerendered pages does not crawl imported images outside of pages prerendering does not crawl imported images outside of pages Aug 23, 2021
@ignatiusmb ignatiusmb added the bug Something isn't working label Aug 23, 2021
@bluwy
Copy link
Member

bluwy commented Jan 4, 2022

This is because assets are only generated for the client side builds (which doesn't bundle endpoints). SSR builds (which bundles endpoints) don't generate assets. To workaround this, you can specify all the possible links from SSR in the client bundle like this, which shouldn't impact bundle size, but not the most ergonomic way of handling it.

I'm not sure how we should handle this though, perhaps SvelteKit can have Vite crawl for asset imports in endpoints. I also think I've seen a similar issue in Vite but I can't find it.

@bluwy bluwy added the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label Jan 4, 2022
@Rich-Harris Rich-Harris added this to the 1.0 milestone Mar 5, 2022
@Rich-Harris Rich-Harris modified the milestones: 1.0, whenever Jul 20, 2022
@m1212e
Copy link

m1212e commented Sep 21, 2022

Converting images to a blob worked for me as a workaround. The +server.ts returns this:

const result = fetch(...)
return new Response(await result.blob());

@Rich-Harris
Copy link
Member

closing as a dupe of #5240 (even though this issue was first) — the underlying issue is vitejs/vite#11429, and there's a fix here vitejs/vite#11430

@Rich-Harris Rich-Harris closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Projects
None yet
Development

No branches or pull requests

5 participants