Add the ability to do @vercel/og for image generation #11638
-
Describe the problemI would like to be able to generate https://vercel.com/docs/functions/edge-functions/og-image-generation Describe the proposed solutionCreate an official SvelteKit Version. Alternatives consideredThere is an unofficial version that translates a string into JSX, but it is buggy, and adds unnecessary translation. I personally couldn't not get SVG images to work correctly. - https://github.com/etherCorps/sveltekit-og Importancewould make my life easier Additional InformationThis may would need to be added to the Vercel adapter, I'm not sure the best place for this. |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 10 replies
-
|
Here's a post from a Svelte maintainer about how to accomplish this: https://geoffrich.net/posts/svelte-social-image/ |
Beta Was this translation helpful? Give feedback.
-
|
@benmccann - Unfortunately the SvelteKit guys don't believe in TS, and this also is extremely complicated and doesn't use the latest SvelteKit. I can't seem to get it to work. There needs to be an official package for this. J |
Beta Was this translation helpful? Give feedback.
-
This isn't the case! You can read more about it here
Your best bet is to take a look at how Geoff re-created the |
Beta Was this translation helpful? Give feedback.
-
|
@jdgamble555 Re this issue, I've made https://github.com/eltigerchino/svelte-og-image based on Geoff's blog post if you need an updated example. We don't need an official package as mentioned above. There are plenty of implementations out there such as https://github.com/etherCorps/sveltekit-og and https://www.npmjs.com/package/svelte-component-to-image |
Beta Was this translation helpful? Give feedback.
-
|
@ghostdevv - This is the case. Notice I did not say the SvelteKit guys don't believe in "types." Do you know how many hours I spend trying to translate REPL's to TypeScript. Statistically MOST people use TS over JS. When you make REPL's support TypeScript, the default option of SvelteKit is NOT JSDoc, and Rich Harris and Geoff start using TS in their example repos, I will agree with you. The argument that Svelte's core code is better off written in JS with JSDoc has nothing to do with all the things I just mentioned. I also don't see anyone using REPL's with JSDoc, but that is neither here nor there. Side Node: @ghostdevv - Geoff's repo will NOT work on Edge functions from my testing. Thanks @eltigerchino. This is awesome, however, I cannot get it to work! I edited your code for simpler typing. FWI, we don't need slots or context because we're not building a full svelte app. You can extract the rest of the types. I also added an unescape function for dealing with text that is escaped. image-response.ts// Thanks to https://geoffrich.net/posts/svelte-social-image/
import { ImageResponse as VercelOGImageResponse } from '@vercel/og';
import { html } from 'satori-html';
import type { ComponentProps, ComponentType, SvelteComponent } from 'svelte';
function unescapeHtml(html: string) {
return html
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
}
// https://svelte.dev/docs/typescript#types
export interface SvelteComponentSSR<T extends SvelteComponent> {
render: (
props: ComponentProps<T>
) => SvelteRenderResult;
}
export interface SvelteRenderResult {
html: string;
css: {
code: string;
map: null;
};
head: string;
}
export class ImageResponse<T extends SvelteComponent> extends VercelOGImageResponse {
constructor(
component: ComponentType<T>,
props: ComponentProps<T>,
options?: ConstructorParameters<typeof VercelOGImageResponse>['1']
) {
const result = (component as unknown as SvelteComponentSSR<T>).render(props);
const element = html(unescapeHtml(`${result.html}<style>${result.css.code}</style>`));
super(element, options);
}
}I'm getting this error when I deploy to Vercel Edge: There seems to be something wrong with Here is my repo: https://github.com/jdgamble555/sveltekit-vercel-og I'm not sure I could make a simpler example :) Please let me know if you see anything, J |
Beta Was this translation helpful? Give feedback.
-
|
@jdgamble555 You can use this: https://github.com/etherCorps/sveltekit-og |
Beta Was this translation helpful? Give feedback.
-
|
Unfortunately, |
Beta Was this translation helpful? Give feedback.
-
|
@DipandaAser - This won't work on the edge because Vercel won't make a simple fix. Maybe if you guys add to the conversation, it will help. J |
Beta Was this translation helpful? Give feedback.
-
|
Hey guys, any update on this? Could we possibly get Vercel to fix the error? |
Beta Was this translation helpful? Give feedback.
-
|
Please see https://sveltekit-og.dev/docs and their platform-specific guide to get it working. They're brilliantly using a vite plugin to solve the wasm issue on Cloudflare Workers |
Beta Was this translation helpful? Give feedback.
Please see https://sveltekit-og.dev/docs and their platform-specific guide to get it working. They're brilliantly using a vite plugin to solve the wasm issue on Cloudflare Workers