@ethercorps/sveltekit-og 0.0.5 → 0.0.6
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 +101 -22
- package/index.d.ts +2 -0
- package/index.js +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,38 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Open Graph Image Generation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
About
|
|
4
|
+
Generate Open Graph Images dynamically from HTML/CSS without a browser in SvelteKit.
|
|
4
5
|
|
|
5
|
-
##
|
|
6
|
+
## Quick Start
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Install `@ethercorps/sveltekit-og`, then use it inside a server endpoint route (+server.ts or +server.js):
|
|
8
9
|
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
```typescript
|
|
11
|
+
// /routes/og/+server.ts
|
|
12
|
+
import { ImageResponse } from '$lib';
|
|
13
|
+
import type { RequestHandler } from './$types';
|
|
14
|
+
|
|
15
|
+
const template = `
|
|
16
|
+
<div tw="bg-gray-50 flex w-full h-full items-center justify-center">
|
|
17
|
+
<div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
|
|
18
|
+
<h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
|
|
19
|
+
<span>Ready to dive in?</span>
|
|
20
|
+
<span tw="text-indigo-600">Start your free trial today.</span>
|
|
21
|
+
</h2>
|
|
22
|
+
<div tw="mt-8 flex md:mt-0">
|
|
23
|
+
<div tw="flex rounded-md shadow">
|
|
24
|
+
<a href="#" 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>
|
|
25
|
+
</div>
|
|
26
|
+
<div tw="ml-3 flex rounded-md shadow">
|
|
27
|
+
<a href="#" 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>
|
|
28
|
+
</div>
|
|
29
|
+
</div>
|
|
30
|
+
</div>
|
|
31
|
+
</div>
|
|
32
|
+
`;
|
|
33
|
+
const fontFile400 = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
|
|
34
|
+
const fontFile700 = await fetch('https://og-playground.vercel.app/inter-latin-ext-700-normal.woff');
|
|
35
|
+
const fontData400: ArrayBuffer = await fontFile400.arrayBuffer();
|
|
36
|
+
const fontData700: ArrayBuffer = await fontFile700.arrayBuffer();
|
|
37
|
+
|
|
38
|
+
export const GET: RequestHandler = async () => {
|
|
39
|
+
return new ImageResponse(template, {
|
|
40
|
+
height: 250,
|
|
41
|
+
width: 500,
|
|
42
|
+
fonts: [
|
|
43
|
+
{
|
|
44
|
+
name: 'Inter Latin',
|
|
45
|
+
data: fontData400,
|
|
46
|
+
weight: 400
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'Inter Latin',
|
|
50
|
+
data: fontData700,
|
|
51
|
+
weight: 700
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
});
|
|
55
|
+
};
|
|
12
56
|
|
|
13
|
-
# create a new project in my-app
|
|
14
|
-
npm create svelte@latest my-app
|
|
15
57
|
```
|
|
16
58
|
|
|
17
|
-
|
|
59
|
+
Then run `pnpm dev` and access localhost:5173/og, the React element will be rendered and responded as a PNG from that endpoint:
|
|
18
60
|
|
|
19
|
-
|
|
61
|
+

|
|
20
62
|
|
|
21
|
-
|
|
22
|
-
npm run dev
|
|
63
|
+
Read more about the API, supported features and check out the examples in the following sections.
|
|
23
64
|
|
|
24
|
-
|
|
25
|
-
npm run dev -- --open
|
|
26
|
-
```
|
|
65
|
+
## API Reference
|
|
27
66
|
|
|
28
|
-
|
|
67
|
+
The package exposes an `ImageResponse` constructor, with the following options available:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { ImageResponse } from '@ethercorps/sveltekit-og'
|
|
71
|
+
|
|
72
|
+
// ...
|
|
73
|
+
new ImageResponse(
|
|
74
|
+
element: string,
|
|
75
|
+
options: {
|
|
76
|
+
width?: number = 1200
|
|
77
|
+
height?: number = 630
|
|
78
|
+
fonts?: {
|
|
79
|
+
name: string,
|
|
80
|
+
data: ArrayBuffer,
|
|
81
|
+
weight: number,
|
|
82
|
+
style: 'normal' | 'italic'
|
|
83
|
+
}[]
|
|
84
|
+
debug?: boolean = false
|
|
85
|
+
graphemeImages?: Record<string, string>;
|
|
86
|
+
loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
|
|
87
|
+
// Options that will be passed to the HTTP response
|
|
88
|
+
status?: number = 200
|
|
89
|
+
statusText?: string
|
|
90
|
+
headers?: Record<string, string>
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
```
|
|
29
94
|
|
|
30
|
-
|
|
95
|
+
When running in production, these headers will be included by `@ethercorps/sveltekit-og`:
|
|
31
96
|
|
|
32
|
-
```
|
|
33
|
-
|
|
97
|
+
```typescript
|
|
98
|
+
'content-type': 'image/png',
|
|
99
|
+
'cache-control': 'public, immutable, no-transform, max-age=31536000',
|
|
34
100
|
```
|
|
35
101
|
|
|
36
|
-
|
|
102
|
+
During development, the `cache-control: no-cache, no-store` header is used instead.
|
|
103
|
+
|
|
104
|
+
### Supported HTML and CSS Features
|
|
105
|
+
|
|
106
|
+
Please refer to [Satori’s documentation](https://github.com/vercel/satori#documentation) for a list of supported HTML and CSS features.
|
|
107
|
+
|
|
108
|
+
By default, `@ethercorps/sveltekit-og` only has the 'Inter Latin' font included. If you need to use other fonts, you can pass them in the `fonts` option.
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Acknowledgements
|
|
112
|
+
|
|
113
|
+
This project will not be possible without the following projects:
|
|
37
114
|
|
|
38
|
-
|
|
115
|
+
- [Satori](https://github.com/vercel/satori)
|
|
116
|
+
- [Google Fonts](https://fonts.google.com) and [Noto Sans](https://www.google.com/get/noto/)
|
|
117
|
+
- [Resvg](https://github.com/RazrFalcon/resvg) and [Resvg.js](https://github.com/yisibl/resvg-js)
|
package/index.d.ts
CHANGED
|
@@ -12,5 +12,7 @@ declare type ImageOptions = {
|
|
|
12
12
|
height?: number;
|
|
13
13
|
debug?: boolean;
|
|
14
14
|
fonts?: SatoriOptions['fonts'];
|
|
15
|
+
graphemeImages?: Record<string, string>;
|
|
16
|
+
loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
|
|
15
17
|
};
|
|
16
18
|
export {};
|
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { readFileSync } from 'fs';
|
|
|
6
6
|
import { fileURLToPath } from 'url';
|
|
7
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
8
|
const resSvgWasm = initWasm(readFileSync(join(__filename, '../vendors/resvg.wasm')));
|
|
9
|
-
const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-
|
|
9
|
+
const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-700-normal.woff');
|
|
10
10
|
const fontData = await fontFile.arrayBuffer();
|
|
11
11
|
export const ImageResponse = class {
|
|
12
12
|
constructor(htmlTemplate, optionsByUser) {
|
|
@@ -19,22 +19,23 @@ export const ImageResponse = class {
|
|
|
19
19
|
height: options.height,
|
|
20
20
|
fonts: options.fonts || [
|
|
21
21
|
{
|
|
22
|
-
name: '
|
|
22
|
+
name: 'Noto Sans',
|
|
23
23
|
data: fontData,
|
|
24
|
-
weight: 400,
|
|
25
24
|
style: 'normal'
|
|
26
25
|
}
|
|
27
26
|
]
|
|
28
27
|
});
|
|
29
28
|
const pngData = new Resvg(svg, { fitTo: { mode: 'width', value: options.width } });
|
|
30
|
-
a.enqueue(pngData.render().asPng());
|
|
29
|
+
a.enqueue(await pngData.render().asPng());
|
|
31
30
|
a.close();
|
|
32
31
|
}
|
|
33
32
|
});
|
|
34
33
|
return new Response(png, {
|
|
35
34
|
headers: {
|
|
36
35
|
'Content-Type': 'image/png',
|
|
37
|
-
'cache-control':
|
|
36
|
+
'cache-control': import.meta.env.DEV
|
|
37
|
+
? 'no-cache, no-store'
|
|
38
|
+
: 'public, immutable, no-transform, max-age=31536000',
|
|
38
39
|
...options.headers
|
|
39
40
|
},
|
|
40
41
|
status: options.status,
|