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

Partial prerendering of public pages for better core web vitals #740

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 21 additions & 6 deletions src/lib/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export type AppStore = {
themeInUse: 'light' | 'dark';
};

export const app = writable<AppStore>({
const defaultValue = {
theme: 'auto',
themeInUse: 'light'
});
};

export const app = writable<AppStore>(getInitialValue());

export const iconPath = derived(app, ($app) => {
return (name: string, type: 'color' | 'grayscale') => {
Expand All @@ -18,9 +20,22 @@ export const iconPath = derived(app, ($app) => {
});

if (browser) {
app.update((n) => ({
...n,
...(JSON.parse(localStorage.getItem('appwrite')) ?? {})
}));
app.subscribe((u) => localStorage.setItem('appwrite', JSON.stringify(u) ?? '{}'));
}

function getInitialValue() {
if (!browser) {
return defaultValue;
}
const localStorageValue = JSON.parse(localStorage.getItem('appwrite')) || {};
const value = { ...localStorageValue, ...defaultValue };
if (value.theme === 'auto' && window.matchMedia) {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)');
if (darkThemeMq.matches) {
value.themeInUse = 'dark';
} else {
value.themeInUse = '';
}
}
return value;
}
32 changes: 10 additions & 22 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
window.VERCEL_ANALYTICS_ID = import.meta.env.VERCEL_ANALYTICS_ID?.toString() ?? false;
}

const prerenderedRoutes = ['/login', '/register', '/recover', '/invite'];
if (prerenderedRoutes.some((n) => $page.url.pathname.startsWith(n))) {
loading.set(false);
}

onMount(async () => {
if ($page.url.searchParams.has('migrate')) {
const migrateData = $page.url.searchParams.get('migrate');
Expand Down Expand Up @@ -65,14 +70,7 @@
* Handle initial load.
*/
if (!$page.url.pathname.startsWith('/auth') && !$page.url.pathname.startsWith('/git')) {
const acceptedRoutes = [
'/login',
'/register',
'/recover',
'/invite',
'/card',
'/hackathon'
];
const acceptedRoutes = prerenderedRoutes.concat(['/card', '/hackathon']);
if ($user) {
if (
!$page.url.pathname.startsWith('/console') &&
Expand Down Expand Up @@ -106,20 +104,10 @@

$: {
if (browser) {
const isCloudClass = isCloud ? 'is-cloud' : '';
if ($app.theme === 'auto') {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)');
if (darkThemeMq.matches) {
document.body.setAttribute('class', `theme-dark ${isCloudClass}`);
$app.themeInUse = 'dark';
} else {
document.body.setAttribute('class', `theme-light ${isCloudClass}`);
$app.themeInUse = 'light';
}
} else {
document.body.setAttribute('class', `theme-${$app.theme} ${isCloudClass}`);
$app.themeInUse = $app.theme;
}
document.body.setAttribute(
'class',
`theme-${$app.themeInUse} ${isCloud ? 'is-cloud' : ''}`
);
}
}
</script>
Expand Down
2 changes: 2 additions & 0 deletions src/routes/invite/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ssr = true;
export const prerender = true;
97 changes: 52 additions & 45 deletions src/routes/invite/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { Button, Form, FormList, InputChoice } from '$lib/elements/forms';
Expand Down Expand Up @@ -43,53 +44,59 @@
<title>Accept invite - Appwrite</title>
</svelte:head>

<!-- if browser checks allow us to partially prerender on the server to improve FCP and LCP -->
<Unauthenticated>
<svelte:fragment slot="title">
{#if !userId || !secret || !membershipId || !teamId}
Invalid invite
{:else}
Invite
{/if}
<svelte:fragment slot="title"
><span
>{#if browser}
{#if !userId || !secret || !membershipId || !teamId}
Invalid invite
{:else}
Invite
{/if}{/if}</span>
</svelte:fragment>
<svelte:fragment>
{#if !userId || !secret || !membershipId || !teamId}
<Alert type="warning">
<svelte:fragment slot="title">The invite link is not valid</svelte:fragment>
Please ask the project owner to send you a new invite.
</Alert>
<div class="u-flex u-main-end u-margin-block-start-40">
<Button href={`${base}/register`}>Sign up to Appwrite</Button>
</div>
{:else}
<p class="text">You have been invited to join a team project on Appwrite</p>
<Form onSubmit={acceptInvite}>
<FormList>
<InputChoice
required
bind:value={terms}
id="terms"
label="terms"
showLabel={false}>
By accepting the invitation, you agree to the <a
class="link"
href="https://appwrite.io/terms"
target="_blank"
rel="noopener noreferrer">Terms and Conditions</a>
and
<a
class="link"
href="https://appwrite.io/privacy"
target="_blank"
rel="noopener noreferrer">
Privacy Policy</a
>.</InputChoice>

<div class="u-flex u-main-end u-gap-12">
<Button secondary href={`${base}/login`}>Cancel</Button>
<Button submit>Accept</Button>
<svelte:fragment
><div>
{#if browser}
{#if !userId || !secret || !membershipId || !teamId}
<Alert type="warning">
<svelte:fragment slot="title">The invite link is not valid</svelte:fragment>
Please ask the project owner to send you a new invite.
</Alert>
<div class="u-flex u-main-end u-margin-block-start-40">
<Button href={`${base}/register`}>Sign up to Appwrite</Button>
</div>
</FormList>
</Form>
{/if}
{:else}
<p class="text">You have been invited to join a team project on Appwrite</p>
<Form onSubmit={acceptInvite}>
<FormList>
<InputChoice
required
bind:value={terms}
id="terms"
label="terms"
showLabel={false}>
By accepting the invitation, you agree to the <a
class="link"
href="https://appwrite.io/terms"
target="_blank"
rel="noopener noreferrer">Terms and Conditions</a>
and
<a
class="link"
href="https://appwrite.io/privacy"
target="_blank"
rel="noopener noreferrer">
Privacy Policy</a
>.</InputChoice>

<div class="u-flex u-main-end u-gap-12">
<Button secondary href={`${base}/login`}>Cancel</Button>
<Button submit>Accept</Button>
</div>
</FormList>
</Form>
{/if}{/if}
</div>
</svelte:fragment>
</Unauthenticated>
2 changes: 2 additions & 0 deletions src/routes/login/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ssr = true;
export const prerender = true;
83 changes: 45 additions & 38 deletions src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { browser } from '$app/environment';
import { goto, invalidate } from '$app/navigation';
import { base } from '$app/paths';
import {
Expand Down Expand Up @@ -74,49 +75,55 @@
<title>Sign in - Appwrite</title>
</svelte:head>

<!-- if browser checks allow us to partially prerender on the server to improve FCP and LCP -->
<Unauthenticated>
<svelte:fragment slot="title">Sign in</svelte:fragment>
<svelte:fragment slot="title"
>{#if browser}Sign in{/if}</svelte:fragment>
<svelte:fragment>
<Form onSubmit={login}>
<FormList>
<InputEmail
id="email"
label="Email"
placeholder="Email"
autofocus={true}
required={true}
bind:value={mail} />
<InputPassword
id="password"
label="Password"
placeholder="Password"
required={true}
meter={false}
showPasswordButton={true}
bind:value={pass} />
<FormItem>
<Button fullWidth submit {disabled}>Sign in</Button>
</FormItem>
{#if isCloud}
<span class="with-separators eyebrow-heading-3">or</span>
{#if browser}
<Form onSubmit={login}>
<FormList>
<InputEmail
id="email"
label="Email"
placeholder="Email"
autofocus={true}
required={true}
bind:value={mail} />
<InputPassword
id="password"
label="Password"
placeholder="Password"
required={true}
meter={false}
showPasswordButton={true}
bind:value={pass} />
<FormItem>
<Button github fullWidth on:click={onGithubLogin} {disabled}>
<span class="icon-github" aria-hidden="true" />
<span class="text">Sign in with GitHub</span>
</Button>
<Button fullWidth submit {disabled}>Sign in</Button>
</FormItem>
{/if}
</FormList>
</Form>
{#if isCloud}
<span class="with-separators eyebrow-heading-3">or</span>
<FormItem>
<Button github fullWidth on:click={onGithubLogin} {disabled}>
<span class="icon-github" aria-hidden="true" />
<span class="text">Sign in with GitHub</span>
</Button>
</FormItem>
{/if}
</FormList>
</Form>
{/if}
</svelte:fragment>
<svelte:fragment slot="links">
<li class="inline-links-item">
<a href={`${base}/recover`}><span class="text">Forgot Password?</span></a>
</li>
<li class="inline-links-item">
<a href={`${base}/register${$page?.url?.search ?? ''}`}>
<span class="text">Sign Up</span>
</a>
</li>
{#if browser}
<li class="inline-links-item">
<a href={`${base}/recover`}><span class="text">Forgot Password?</span></a>
</li>
<li class="inline-links-item">
<a href={`${base}/register${$page?.url?.search ?? ''}`}>
<span class="text">Sign Up</span>
</a>
</li>
{/if}
</svelte:fragment>
</Unauthenticated>
2 changes: 2 additions & 0 deletions src/routes/recover/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ssr = true;
export const prerender = true;
Loading