@ethercorps/sveltekit-og 1.2.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -116
- package/index.d.ts +3 -2
- package/index.js +20 -9
- package/package.json +17 -20
- package/toReactElement.d.ts +1 -1
- package/toReactElement.js +1 -1
package/README.md
CHANGED
|
@@ -1,74 +1,24 @@
|
|
|
1
|
-
# Open Graph Image Generation
|
|
1
|
+
# SvelteKit Open Graph Image Generation
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Generate Open Graph Images dynamically from HTML/CSS without a browser in SvelteKit.
|
|
3
|
+
Dynamically generate Open Graph images from an HTML+CSS template or Svelte component using fast and efficient conversion from HTML > SVG > PNG. Based on [Satori](https://github.com/vercel/satori#documentation). No headless browser required.
|
|
5
4
|
|
|
6
|
-
##
|
|
5
|
+
## Installation
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const htmlString = `
|
|
17
|
-
<div tw="bg-gray-50 flex w-full">
|
|
18
|
-
<div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
|
|
19
|
-
<h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
|
|
20
|
-
<span>Ready to dive in?</span>
|
|
21
|
-
<span tw="text-indigo-600">Start your free trial today.</span>
|
|
22
|
-
</h2>
|
|
23
|
-
<div tw="mt-8 flex md:mt-0">
|
|
24
|
-
<div tw="flex rounded-md shadow">
|
|
25
|
-
<a tw="flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-white">Get started</a>
|
|
26
|
-
</div>
|
|
27
|
-
<div tw="ml-3 flex rounded-md shadow">
|
|
28
|
-
<a tw="flex items-center justify-center rounded-md border border-transparent bg-white px-5 py-3 text-base font-medium text-indigo-600">Learn more</a>
|
|
29
|
-
</div>
|
|
30
|
-
</div>
|
|
31
|
-
</div>
|
|
32
|
-
</div>
|
|
33
|
-
`;
|
|
34
|
-
const newNode = toReactElement(htmlString);
|
|
35
|
-
|
|
36
|
-
/** @type {import('./$types').PageServerLoad} */
|
|
37
|
-
export async function load() {
|
|
38
|
-
const fontFile400 = await fetch(
|
|
39
|
-
'https://og-playground.vercel.app/inter-latin-ext-400-normal.woff'
|
|
40
|
-
);
|
|
41
|
-
const fontData400 = await fontFile400.arrayBuffer();
|
|
42
|
-
const svg = await satori(newNode, {
|
|
43
|
-
height: 350,
|
|
44
|
-
width: 500,
|
|
45
|
-
fonts: [
|
|
46
|
-
{
|
|
47
|
-
name: 'sans serif',
|
|
48
|
-
data: fontData400,
|
|
49
|
-
style: 'normal',
|
|
50
|
-
weight: 700
|
|
51
|
-
}
|
|
52
|
-
]
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
return { svg };
|
|
56
|
-
}
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install -D @ethercorps/sveltekit-og
|
|
9
|
+
```
|
|
10
|
+
> Using with Cloudflare Pages or Workers then you have to provide `url` polyfill by just installing it as `devDependency`.
|
|
11
|
+
>
|
|
12
|
+
```bash
|
|
13
|
+
pnpm i -D url
|
|
57
14
|
```
|
|
58
15
|
|
|
59
|
-
|
|
60
|
-
- Removed `@resvg/resvg-wasm` with `@resvg/resvg-js` because of internal errors.
|
|
61
|
-
- Removed `satori-html` because now we have `toReactElement` out of the box with svelte compiler.
|
|
62
|
-
> If you find a problem related to undefined a please check [_vite.config.js_](/vite.config.ts) and add ` define: { _a: 'undefined' } in config.`
|
|
63
|
-
|
|
64
|
-
> If you find any issue and have suggestion for this project please open a ticket and if you want to contribute please create a new discussion.
|
|
65
|
-
|
|
66
|
-
## Quick Start
|
|
16
|
+
## Usage
|
|
67
17
|
|
|
68
|
-
|
|
18
|
+
Create a file at `/src/routes/og/+server.ts`. Alternatively, you can use JavaScript by removing the types from this example.
|
|
69
19
|
|
|
70
20
|
```typescript
|
|
71
|
-
// /routes/og/+server.ts
|
|
21
|
+
// src/routes/og/+server.ts
|
|
72
22
|
import { ImageResponse } from '@ethercorps/sveltekit-og';
|
|
73
23
|
import { RequestHandler } from './$types';
|
|
74
24
|
|
|
@@ -90,31 +40,49 @@ const template = `
|
|
|
90
40
|
</div>
|
|
91
41
|
</div>
|
|
92
42
|
`;
|
|
43
|
+
|
|
93
44
|
const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
|
|
94
45
|
const fontData: ArrayBuffer = await fontFile.arrayBuffer();
|
|
95
46
|
|
|
96
47
|
export const GET: RequestHandler = async () => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
48
|
+
return await ImageResponse(template, {
|
|
49
|
+
height: 630,
|
|
50
|
+
width: 1200,
|
|
51
|
+
fonts: [
|
|
52
|
+
{
|
|
53
|
+
name: 'Inter Latin',
|
|
54
|
+
data: fontData,
|
|
55
|
+
weight: 400
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
});
|
|
108
59
|
};
|
|
109
60
|
```
|
|
110
61
|
|
|
111
|
-
Then run `
|
|
62
|
+
Then run `npm dev` and visit `localhost:5173/og` to view your generated PNG. Remember that hot module reloading does not work with server routes, so if you change your HTML or CSS, hard refresh the route to see changes.
|
|
63
|
+
|
|
64
|
+
## Example Output
|
|
112
65
|
|
|
113
66
|

|
|
114
67
|
|
|
115
|
-
|
|
68
|
+
## Headers
|
|
69
|
+
|
|
70
|
+
When run in development, image headers contain `cache-control: no-cache, no-store`. In production, image headers contain `'cache-control': 'public, immutable, no-transform, max-age=31536000'`, which caches the image for 1 year. In both cases, the `'content-type': 'image/png'` is used.
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
## Styling
|
|
74
|
+
|
|
75
|
+
Notice that our example uses TailwindCSS classes (e.g. `tw="bg-gray-50"`). Alternatively, your HTML can contain style attributes using any of [the subset of CSS supported by Satori](https://github.com/vercel/satori#css).
|
|
76
|
+
|
|
77
|
+
Satori supports only a subset of HTML and CSS. For full details, see [Satori’s documentation](https://github.com/vercel/satori#documentation). Notably, Satori only supports flex-based layouts.
|
|
116
78
|
|
|
117
|
-
##
|
|
79
|
+
## Fonts
|
|
80
|
+
|
|
81
|
+
Satori supports `ttf`, `otf`, and `woff` font formats. To maximize the font parsing speed, `ttf` or `otf` are recommended over `woff`.
|
|
82
|
+
|
|
83
|
+
By default, `@ethercorps/sveltekit-og` includes only 'Noto Sans' font. If you need to use other fonts, you can specify them as shown in the example. Notably, you can also import a font file that is stored locally within your project and are not required to use fetch.
|
|
84
|
+
|
|
85
|
+
## Examples
|
|
118
86
|
|
|
119
87
|
- `ImageResponse` · [_source_](/src/routes/new/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app/new)
|
|
120
88
|
- `componentToImageResponse` · [_source_](/src/routes/component-og/) · [_demo_](https://sveltekit-og-five.vercel.app/component-og)
|
|
@@ -131,59 +99,76 @@ import {SvelteComponent} from "svelte";
|
|
|
131
99
|
ImageResponse(
|
|
132
100
|
element : string,
|
|
133
101
|
options : {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
102
|
+
width ? : number = 1200
|
|
103
|
+
height ? : number = 630,
|
|
104
|
+
backgroundColor ? : string = "#fff"
|
|
105
|
+
fonts ? : {
|
|
106
|
+
name: string,
|
|
107
|
+
data: ArrayBuffer,
|
|
108
|
+
weight: number,
|
|
109
|
+
style: 'normal' | 'italic'
|
|
110
|
+
}[]
|
|
111
|
+
debug ? : boolean = false
|
|
112
|
+
graphemeImages ? : Record<string, string>;
|
|
113
|
+
loadAdditionalAsset ? : (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
|
|
114
|
+
// Options that will be passed to the HTTP response
|
|
115
|
+
status ? : number = 200
|
|
116
|
+
statusText ? : string
|
|
117
|
+
headers ? : Record<string, string>
|
|
149
118
|
})
|
|
150
119
|
|
|
151
120
|
componentToImageResponse(
|
|
152
121
|
component : typeof SvelteComponent,
|
|
153
122
|
props : {}, // All export let example inside prop dictionary
|
|
154
123
|
options : {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
124
|
+
width ? : number = 1200
|
|
125
|
+
height ? : number = 630
|
|
126
|
+
fonts ? : {
|
|
127
|
+
name: string,
|
|
128
|
+
data: ArrayBuffer,
|
|
129
|
+
weight: number,
|
|
130
|
+
style: 'normal' | 'italic'
|
|
131
|
+
}[]
|
|
132
|
+
debug ? : boolean = false
|
|
133
|
+
graphemeImages ? : Record<string, string>;
|
|
134
|
+
loadAdditionalAsset ? : (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
|
|
135
|
+
// Options that will be passed to the HTTP response
|
|
136
|
+
status ? : number = 200
|
|
137
|
+
statusText ? : string
|
|
138
|
+
headers ? : Record<string, string>
|
|
170
139
|
})
|
|
171
140
|
```
|
|
172
141
|
|
|
173
|
-
|
|
142
|
+
## Changelog
|
|
174
143
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
144
|
+
### v1.2.3 Update (Breaking Changes)
|
|
145
|
+
> Now you have to install dependency by yourself which will make it easier to build for all plateforms.
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
npm i @resvg/resvg-js
|
|
178
149
|
```
|
|
179
150
|
|
|
180
|
-
|
|
151
|
+
```
|
|
152
|
+
npm i satori
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
> From now on their will be no issues related to build, and soon this library going to have its own documentation.
|
|
181
156
|
|
|
182
|
-
###
|
|
157
|
+
### v1.2.2 Update (Breaking Change)
|
|
183
158
|
|
|
184
|
-
|
|
159
|
+
- We don't provide access to satori from `@ethercorps/sveltekit-og`.
|
|
185
160
|
|
|
186
|
-
|
|
161
|
+
### v1.0.0 Update (Breaking Changes)
|
|
162
|
+
|
|
163
|
+
Finally, We have added html to react like element like object converter out of the box and with svelte compiler.
|
|
164
|
+
Now you can use `{ toReactElement }` with `"@ethercorps/sveltekit-og"` like:
|
|
165
|
+
|
|
166
|
+
- We have changed to function based instead of class based ImageResponse and componentToImageResponse.
|
|
167
|
+
- Removed `@resvg/resvg-wasm` with `@resvg/resvg-js` because of internal errors.
|
|
168
|
+
- Removed `satori-html` because now we have `toReactElement` out of the box with svelte compiler.
|
|
169
|
+
> If you find a problem related to undefined a please check [_vite.config.js_](/vite.config.ts) and add ` define: { _a: 'undefined' } in config.`
|
|
170
|
+
|
|
171
|
+
> If you find any issue and have suggestion for this project please open a ticket and if you want to contribute please create a new discussion.
|
|
187
172
|
|
|
188
173
|
## Acknowledgements
|
|
189
174
|
|
|
@@ -191,9 +176,13 @@ This project will not be possible without the following projects:
|
|
|
191
176
|
|
|
192
177
|
- [Satori & @vercel/og](https://github.com/vercel/satori)
|
|
193
178
|
- [Noto by Google Fonts](https://fonts.google.com/noto)
|
|
194
|
-
- [
|
|
179
|
+
- [svg2png-wasm](https://github.com/ssssota/svg2png-wasm)
|
|
195
180
|
|
|
196
181
|
## Authors
|
|
197
182
|
|
|
198
183
|
- [@theetherGit](https://www.github.com/theetherGit)
|
|
199
184
|
- [@etherCorps](https://www.github.com/etherCorps)
|
|
185
|
+
|
|
186
|
+
## Contributors
|
|
187
|
+
|
|
188
|
+
- [@jasongitmail](https://github.com/jasongitmail)
|
package/index.d.ts
CHANGED
|
@@ -9,8 +9,9 @@ type ImageOptions = {
|
|
|
9
9
|
height?: number;
|
|
10
10
|
debug?: boolean;
|
|
11
11
|
fonts?: SatoriOptions['fonts'];
|
|
12
|
+
backgroundColor?: string;
|
|
12
13
|
graphemeImages?: Record<string, string>;
|
|
13
14
|
loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions['fonts'] | string | undefined>;
|
|
14
15
|
};
|
|
15
|
-
type ImageResponseType = typeof ImageResponse;
|
|
16
|
-
export { componentToImageResponse, ImageResponse, toReactElement
|
|
16
|
+
export type ImageResponseType = typeof ImageResponse;
|
|
17
|
+
export { componentToImageResponse, ImageResponse, toReactElement };
|
package/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import satori, {} from 'satori';
|
|
2
|
-
import { Resvg } from '@resvg/resvg-js';
|
|
3
2
|
import toReactElement from './toReactElement';
|
|
3
|
+
import { svg2png, initialize } from 'svg2png-wasm';
|
|
4
|
+
let initialized = false;
|
|
4
5
|
const fontFile = await fetch('https://sveltekit-og.ethercorps.io/noto-sans.ttf');
|
|
5
6
|
const fontData = await fontFile.arrayBuffer();
|
|
7
|
+
const indexWasmRes = await fetch('https://unpkg.com/svg2png-wasm/svg2png_wasm_bg.wasm');
|
|
8
|
+
const svg2PngWasmBuffer = await indexWasmRes.arrayBuffer();
|
|
9
|
+
const initSvgToPng = async () => {
|
|
10
|
+
await initialize(svg2PngWasmBuffer).catch((e) => console.log(e));
|
|
11
|
+
initialized = true;
|
|
12
|
+
};
|
|
6
13
|
const ImageResponse = async (htmlTemplate, optionsByUser) => {
|
|
7
14
|
const options = Object.assign({ width: 1200, height: 630, debug: !1 }, optionsByUser);
|
|
8
15
|
const svg = await satori(toReactElement(htmlTemplate), {
|
|
@@ -18,15 +25,19 @@ const ImageResponse = async (htmlTemplate, optionsByUser) => {
|
|
|
18
25
|
}
|
|
19
26
|
]
|
|
20
27
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
if (!initialized) {
|
|
29
|
+
await initSvgToPng();
|
|
30
|
+
initialized = true;
|
|
31
|
+
}
|
|
32
|
+
const defaultConfig = {
|
|
33
|
+
width: options.width,
|
|
34
|
+
height: options.height, // optional
|
|
26
35
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
if (Object.hasOwn(options, 'backgroundColor')) {
|
|
37
|
+
defaultConfig.backgroundColor = options.backgroundColor;
|
|
38
|
+
}
|
|
39
|
+
const png = await svg2png(svg, defaultConfig);
|
|
40
|
+
return new Response(png, {
|
|
30
41
|
headers: {
|
|
31
42
|
'Content-Type': 'image/png',
|
|
32
43
|
'cache-control': 'public, immutable, no-transform, max-age=31536000',
|
package/package.json
CHANGED
|
@@ -1,39 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ethercorps/sveltekit-og",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"devDependencies": {
|
|
6
|
-
"@playwright/test": "^1.
|
|
7
|
-
"@resvg/resvg-js": "^2.4.1",
|
|
6
|
+
"@playwright/test": "^1.34.3",
|
|
8
7
|
"@sveltejs/adapter-auto": "next",
|
|
9
|
-
"@sveltejs/adapter-vercel": "^2.3
|
|
8
|
+
"@sveltejs/adapter-vercel": "^2.4.3",
|
|
10
9
|
"@sveltejs/kit": "1.10.0",
|
|
11
10
|
"@sveltejs/package": "next",
|
|
12
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
13
|
-
"@typescript-eslint/parser": "^5.
|
|
11
|
+
"@typescript-eslint/eslint-plugin": "^5.59.9",
|
|
12
|
+
"@typescript-eslint/parser": "^5.59.9",
|
|
14
13
|
"autoprefixer": "^10.4.14",
|
|
15
14
|
"brace": "^0.11.1",
|
|
16
|
-
"eslint": "^8.
|
|
17
|
-
"eslint-config-prettier": "^8.
|
|
15
|
+
"eslint": "^8.42.0",
|
|
16
|
+
"eslint-config-prettier": "^8.8.0",
|
|
18
17
|
"eslint-plugin-svelte3": "^4.0.0",
|
|
19
|
-
"postcss": "^8.4.
|
|
20
|
-
"prettier": "^2.8.
|
|
21
|
-
"prettier-plugin-svelte": "^2.
|
|
18
|
+
"postcss": "^8.4.24",
|
|
19
|
+
"prettier": "^2.8.8",
|
|
20
|
+
"prettier-plugin-svelte": "^2.10.1",
|
|
22
21
|
"prismjs": "^1.29.0",
|
|
23
|
-
"
|
|
24
|
-
"svelte": "^3.57.0",
|
|
22
|
+
"svelte": "^3.59.1",
|
|
25
23
|
"svelte-check": "^2.10.3",
|
|
26
24
|
"svelte-preprocess": "^4.10.7",
|
|
27
|
-
"tailwindcss": "^3.2
|
|
28
|
-
"tslib": "^2.5.
|
|
25
|
+
"tailwindcss": "^3.3.2",
|
|
26
|
+
"tslib": "^2.5.3",
|
|
29
27
|
"typescript": "^4.9.5",
|
|
30
|
-
"vite": "^4.
|
|
28
|
+
"vite": "^4.3.9"
|
|
31
29
|
},
|
|
32
30
|
"type": "module",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"svelte": "^3.54.0"
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"satori": "^0.10.1",
|
|
33
|
+
"svg2png-wasm": "^1.4.0"
|
|
37
34
|
},
|
|
38
35
|
"keywords": [
|
|
39
36
|
"open graph image",
|
package/toReactElement.d.ts
CHANGED
package/toReactElement.js
CHANGED