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

fix: social thumbnail assistants in prod #732

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.ttf" {
const value: ArrayBuffer;
export default value;
}
10 changes: 6 additions & 4 deletions src/routes/assistant/[assistantId]/thumbnail.png/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import type { SvelteComponent } from "svelte";
import { Resvg } from "@resvg/resvg-js";
import satori from "satori";
import { html } from "satori-html";
import { base } from "$app/paths";

export const GET: RequestHandler = (async ({ url, params, fetch }) => {
import InterRegular from "../../../../../static/fonts/Inter-Regular.ttf";
import InterBold from "../../../../../static/fonts/Inter-Bold.ttf";

export const GET: RequestHandler = (async ({ url, params }) => {
const assistant = await collections.assistants.findOne({
_id: new ObjectId(params.assistantId),
});
Expand Down Expand Up @@ -39,12 +41,12 @@ export const GET: RequestHandler = (async ({ url, params, fetch }) => {
fonts: [
{
name: "Inter",
data: await fetch(base + "/fonts/Inter-Regular.ttf").then((r) => r.arrayBuffer()),
data: InterRegular as unknown as ArrayBuffer,
weight: 500,
},
{
name: "Inter",
data: await fetch(base + "/fonts/Inter-Bold.ttf").then((r) => r.arrayBuffer()),
data: InterBold as unknown as ArrayBuffer,
weight: 700,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<img class="mr-1.5 h-8 w-8" src={imgUrl} alt="app logo" />
AI assistant
</p>
<h1 class="m-0 {name.length < 38 ? 'text-5xl' : 'text-4xl'} text-balance font-black">
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the tailwind classes that were not recognized by satori cc @gary149

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they caused warnings in the log every time a thumbnail was rendered

<h1 class="m-0 {name.length < 38 ? 'text-5xl' : 'text-4xl'} font-black">
{name}
</h1>
<p class="mb-8 text-pretty text-2xl">
<p class="mb-8 text-2xl">
{description.slice(0, 160)}
{#if description.length > 160}...{/if}
</p>
Expand Down
18 changes: 17 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { defineConfig, type PluginOption } from "vite";
import Icons from "unplugin-icons/vite";
import { promises } from "fs";

// used to load fonts server side for thumbnail generation
function loadTTFAsArrayBuffer(): PluginOption {
return {
name: "load-ttf-as-array-buffer",
async transform(_src, id) {
if (id.endsWith(".ttf")) {
return `export default new Uint8Array([
${new Uint8Array(await promises.readFile(id))}
]).buffer`;
}
},
};
}

export default defineConfig({
plugins: [
sveltekit(),
Icons({
compiler: "svelte",
}),
loadTTFAsArrayBuffer(),
],
optimizeDeps: {
include: ["browser-image-resizer"],
Expand Down
Loading