Skip to content

Commit

Permalink
perf: use rel=preload for pages that perform fetching (denoland#520)
Browse files Browse the repository at this point in the history
closes denoland#515.

applied `link`s as suggested, yet additionally had to add
`crossOrigin="anonymous"` to resolve the following warning in the
console:
![Bildschirm­foto 2023-09-06 um 22 08
24](https://github.com/denoland/saaskit/assets/8133892/d28f93f4-c090-4539-ae08-b885928f7265)

also homogenized the components using `fetch` to have an `endpoint` prop
  • Loading branch information
mbhrznr authored Sep 6, 2023
1 parent 1df196c commit c4c7a5f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
3 changes: 1 addition & 2 deletions islands/CommentsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ function CommentSummary(props: Comment) {
);
}

export default function CommentsList(props: { itemId: string }) {
export default function CommentsList({ endpoint }: { endpoint: string }) {
const commentsSig = useSignal<Comment[]>([]);
const cursorSig = useSignal("");
const isLoadingSig = useSignal(false);
const endpoint = `/api/items/${props.itemId}/comments`;

async function loadMoreComments() {
if (isLoadingSig.value) return;
Expand Down
3 changes: 1 addition & 2 deletions islands/NotificationsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ function NotificationSummary(props: Notification) {
);
}

export default function NotificationsList() {
export default function NotificationsList({ endpoint }: { endpoint: string }) {
const notificationsSig = useSignal<Notification[]>([]);
const cursorSig = useSignal("");
const isLoadingSig = useSignal(false);
const endpoint = `/api/me/notifications`;

async function loadMoreNotifications() {
if (isLoadingSig.value) return;
Expand Down
3 changes: 1 addition & 2 deletions islands/UsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ function UserTableRow(props: User) {
);
}

export default function UsersTable() {
export default function UsersTable({ endpoint }: { endpoint: string }) {
const usersSig = useSignal<User[]>([]);
const cursorSig = useSignal("");
const isLoadingSig = useSignal(false);
const endpoint = "/api/users";

async function loadMoreUsers() {
if (isLoadingSig.value) return;
Expand Down
13 changes: 11 additions & 2 deletions routes/dashboard/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ export default async function DashboardUsersPage(
_req: Request,
ctx: RouteContext,
) {
const endpoint = "/api/users";

return (
<>
<Head title="Users" href={ctx.url.href} />
<Head title="Users" href={ctx.url.href}>
<link
as="fetch"
crossOrigin="anonymous"
href={endpoint}
rel="preload"
/>
</Head>
<main class="flex-1 p-4">
<h1 class={HEADING_WITH_MARGIN_STYLES}>Dashboard</h1>
<TabsBar
Expand All @@ -25,7 +34,7 @@ export default async function DashboardUsersPage(
}]}
currentPath={ctx.url.pathname}
/>
<UsersTable />
<UsersTable endpoint={endpoint} />
</main>
</>
);
Expand Down
13 changes: 11 additions & 2 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ export default async function HomePage(
_req: Request,
ctx: RouteContext<undefined, State>,
) {
const endpoint = "/api/items";

return (
<>
<Head href={ctx.url.href} />
<Head href={ctx.url.href}>
<link
as="fetch"
crossOrigin="anonymous"
href={endpoint}
rel="preload"
/>
</Head>
<main class="flex-1 p-4">
{NEEDS_SETUP && <SetupInstruction />}
<ItemsList endpoint={"/api/items"} />
<ItemsList endpoint={endpoint} />
</main>
</>
);
Expand Down
12 changes: 10 additions & 2 deletions routes/items/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default async function ItemsItemPage(
const item = await getItem(itemId);
if (item === null) return await ctx.renderNotFound();

const endpoint = `/api/items/${ctx.params.id}/comments`;
const isSignedIn = ctx.state.sessionUser !== undefined;
let isVoted = false;

Expand All @@ -46,14 +47,21 @@ export default async function ItemsItemPage(

return (
<>
<Head title={item.title} href={ctx.url.href} />
<Head title={item.title} href={ctx.url.href}>
<link
as="fetch"
crossOrigin="anonymous"
href={endpoint}
rel="preload"
/>
</Head>
<main class="flex-1 p-4 space-y-8">
<ItemSummary
item={item}
isVoted={isVoted}
/>
<CommentInput isSignedIn={isSignedIn} itemId={ctx.params.id} />
<CommentsList itemId={ctx.params.id} />
<CommentsList endpoint={endpoint} />
</main>
</>
);
Expand Down
13 changes: 11 additions & 2 deletions routes/notifications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ export default async function NotificationsPage(
_req: Request,
ctx: RouteContext<undefined, SignedInState>,
) {
const endpoint = "/api/me/notifications";

return (
<>
<Head title="Notifications" href={ctx.url.href} />
<Head title="Notifications" href={ctx.url.href}>
<link
as="fetch"
crossOrigin="anonymous"
href={endpoint}
rel="preload"
/>
</Head>
<main class="flex-1 p-4">
<h1 class={HEADING_WITH_MARGIN_STYLES}>Notifications</h1>
<NotificationsList />
<NotificationsList endpoint={endpoint} />
</main>
</>
);
Expand Down
9 changes: 8 additions & 1 deletion routes/users/[login].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ export default async function UsersUserPage(

return (
<>
<Head title={user.login} href={ctx.url.href} />
<Head title={user.login} href={ctx.url.href}>
<link
as="fetch"
crossOrigin="anonymous"
href={`/api/users/${login}/items`}
rel="preload"
/>
</Head>
<main class="flex-1 p-4">
<Profile
isSubscribed={user.isSubscribed}
Expand Down

0 comments on commit c4c7a5f

Please sign in to comment.