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

chore: add cache headers to storage proxies #762

Merged
merged 1 commit into from
Sep 1, 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
2 changes: 1 addition & 1 deletion apps/platform/trpc/routers/convoRouter/convoRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { TRPCError } from '@trpc/server';
import { env } from '~platform/env';
import { z } from 'zod';

const tipTapExtensions = createExtensionSet({ storageUrl: env.STORAGE_URL });
const tipTapExtensions = createExtensionSet();

type Attachment = {
orgPublicId: TypeId<'org'>;
Expand Down
6 changes: 4 additions & 2 deletions apps/storage/proxy/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ export const attachmentProxy = createHonoApp<Ctx>().get(
Key: `${attachmentQueryResponse.org.publicId}/${attachmentId}/${filename}`
});
const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
const res = await fetch(url).then((res) => c.body(res.body, res));
const res = await fetch(url);
if (!res.ok) {
return c.json(
{ error: 'Error while fetching attachment' },
{ status: 500 }
);
}
return res;
// Cache for 1 hour
c.header('Cache-Control', 'private, max-age=3600');
return c.body(res.body);
}
);
9 changes: 7 additions & 2 deletions apps/storage/proxy/avatars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ export const avatarProxy = createHonoApp<Ctx>().get(
const proxy = c.req.param('proxy');
const res = await fetch(
`${env.STORAGE_S3_ENDPOINT}/${env.STORAGE_S3_BUCKET_AVATARS}/${proxy}`
).then((res) => c.body(res.body, res));
);
if (res.status === 404) {
return c.json({ error: 'Not Found' }, 404);
}
return res;
// Avatars are immutable so we can cache them for a long time
c.header(
'Cache-Control',
'public, immutable, max-age=86400, stale-while-revalidate=604800'
);
return c.body(res.body, res);
}
);
6 changes: 4 additions & 2 deletions apps/storage/proxy/inline-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ export const inlineProxy = createHonoApp<Ctx>()
Key: `${attachmentQueryResponse.org.publicId}/${attachmentId}/${filename}`
});
const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
const res = await fetch(url).then((res) => c.body(res.body, res));
const res = await fetch(url);
if (!res.ok) {
return c.json(
{ error: 'Error while fetching attachment' },
{ status: 500 }
);
}
return res;
// Cache for 1 hour
c.header('Cache-Control', 'private, max-age=3600');
return c.body(res.body, res);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { type TypeId } from '@u22n/utils/typeid';
import { cva } from 'class-variance-authority';
import { cn } from '@/src/lib/utils';
import { ms } from '@u22n/utils/ms';
import { env } from '@/src/env';
import { useAtom } from 'jotai';
import { toast } from 'sonner';

Expand Down Expand Up @@ -209,9 +208,7 @@ export const MessagesPanel = memo(

MessagesPanel.displayName = 'MessagesPanel';

const tipTapExtensions = createExtensionSet({
storageUrl: env.NEXT_PUBLIC_STORAGE_URL
});
const tipTapExtensions = createExtensionSet();

const MessageItem = memo(
function MessageItem({
Expand Down
2 changes: 0 additions & 2 deletions apps/web/src/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import { slashCommand, suggestionItems } from './slash-commands';
import { createExtensionSet } from '@u22n/tiptap/extensions';
import { useIsMobile } from '@/src/hooks/use-is-mobile';
import { forwardRef, memo, useState } from 'react';
import { env } from '@/src/env';

const tipTapExtensions = createExtensionSet({
storageUrl: env.NEXT_PUBLIC_STORAGE_URL,
className: {
placeholderImage: 'rounded-md border opacity-50'
}
Expand Down
18 changes: 0 additions & 18 deletions packages/tiptap/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import _ExtensionLink from '@tiptap/extension-link';
import { type AnyExtension } from '@tiptap/react';

type CreateExtensionSetOptions = {
storageUrl?: string;
className?: {
link?: string;
image?: string;
Expand All @@ -21,7 +20,6 @@ type CreateExtensionSetOptions = {
};

export const createExtensionSet = ({
storageUrl,
className
}: CreateExtensionSetOptions = {}) => {
const ExtensionImage = ExtensionImageResize.extend({
Expand All @@ -36,22 +34,6 @@ export const createExtensionSet = ({
}
};
},
renderHTML: ({ HTMLAttributes, node }) => {
const isInternalImage =
storageUrl &&
typeof node.attrs.src === 'string' &&
node.attrs.src.startsWith(storageUrl);
return [
'img',
isInternalImage
? {
...node.attrs,
...HTMLAttributes,
crossorigin: 'use-credentials'
}
: { ...node.attrs, ...HTMLAttributes }
];
},
addProseMirrorPlugins: () => {
return [
UploadImagesPlugin({
Expand Down
Loading