Skip to content

Commit 7622228

Browse files
authored
Merge branch 'main' into docs/fix-headless-login-cicd-article
2 parents 810f0a4 + ce72079 commit 7622228

File tree

1,667 files changed

+3742
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,667 files changed

+3742
-77
lines changed

.github/workflows/tests.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,27 @@ jobs:
7777
APPWRITE_API_KEY_INIT: ${{ secrets.APPWRITE_API_KEY_INIT }}
7878
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7979
run: bun run build
80+
assets:
81+
runs-on: ubuntu-latest
82+
steps:
83+
- uses: actions/checkout@v4
84+
with:
85+
ref: ${{ github.event.pull_request.head.sha }}
86+
- uses: oven-sh/setup-bun@v2
87+
with:
88+
bun-version: 1.3
89+
- name: Install dependencies
90+
run: bun install --frozen-lockfile
91+
- name: Optimize assets
92+
run: CI=true bun run optimize
93+
- name: Check for uncommitted changes
94+
run: |
95+
if ! git diff --exit-code; then
96+
echo "Assets are not optimized! The following changes were detected:"
97+
echo ""
98+
git diff
99+
echo ""
100+
echo "Please run 'bun run optimize' locally and commit the changes."
101+
exit 1
102+
fi
103+
echo "✅ All assets are properly optimized."

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
assets/**/*.optimized
12
.DS_Store
23
node_modules
34
/build

scripts/optimize-assets.js

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ const exceptions = ['assets/'];
1111
* @type {{
1212
* jpeg: sharp.JpegOptions,
1313
* webp: sharp.WebpOptions,
14-
* png: sharp.PngOptions,
15-
* gif: sharp.GifOptions,
16-
* avif: sharp.AvifOptions
14+
* png: sharp.PngOptions
1715
* }}
1816
*/
1917
const config = {
@@ -25,12 +23,6 @@ const config = {
2523
},
2624
png: {
2725
quality: 100
28-
},
29-
gif: {
30-
quality: 100
31-
},
32-
avif: {
33-
lossless: true
3426
}
3527
};
3628

@@ -67,27 +59,84 @@ function get_relative_path(file) {
6759
}
6860

6961
async function main() {
62+
console.log(
63+
'This script runs for ~5 mins. It runs silently if all files are already optimized.'
64+
);
65+
7066
for (const file of walk_directory(join(__dirname, '../static'))) {
7167
const relative_path = get_relative_path(file);
72-
const is_animated = file.endsWith('.gif');
7368
if (!is_image(file)) continue;
7469
if (exceptions.some((exception) => relative_path.startsWith(exception))) continue;
7570

76-
const image = sharp(file, {
77-
animated: is_animated
78-
});
79-
const size_before = (await image.toBuffer()).length;
80-
const meta = await image.metadata();
71+
const image = sharp(file);
72+
73+
let meta;
74+
try {
75+
meta = await image.metadata();
76+
} catch (err) {
77+
const msg = `${relative_path} failed: ${err.message}`;
78+
if (Bun.env.CI) {
79+
throw new Error(msg);
80+
}
81+
82+
console.log(msg);
83+
continue;
84+
}
85+
86+
if (Bun.env.CI && (meta.width > 1980 || meta.height > 1980)) {
87+
const msg = `${relative_path} is too large: ${meta.width}x${meta.height}`;
88+
throw new Error(msg);
89+
}
90+
8191
const buffer = await image[meta.format](config[meta.format])
8292
.resize(resize_config)
8393
.toBuffer();
84-
const size_after = buffer.length;
8594

86-
if (size_after >= size_before) continue;
95+
await sharp(buffer).toFile(file + '.optimized');
8796

88-
console.log(relative_path);
97+
const file_before = Bun.file(file);
98+
await file_before.arrayBuffer();
99+
const size_before = file_before.size;
89100

90-
await sharp(buffer).toFile(file);
101+
const file_after = Bun.file(file + '.optimized');
102+
const file_after_contents = await file_after.arrayBuffer();
103+
const size_after = file_after.size;
104+
105+
const size_diff = size_before - size_after;
106+
if (size_diff <= 0) {
107+
await Bun.file(file + '.optimized').delete();
108+
continue;
109+
}
110+
111+
const size_diff_percent = size_diff / size_before;
112+
if (size_diff_percent < 0.2) {
113+
await Bun.file(file + '.optimized').delete();
114+
continue;
115+
}
116+
117+
// Atomic rewrite
118+
try {
119+
await Bun.write(file, file_after_contents);
120+
await Bun.file(file + '.optimized').delete();
121+
} catch (error) {
122+
try {
123+
await Bun.file(file + '.optimized').delete();
124+
} catch {
125+
// Silenced
126+
}
127+
128+
throw new Error(`Failed to replace ${relative_path}: ${error.message}`);
129+
}
130+
131+
const diff_verbose = Math.round(size_diff_percent * 100);
132+
console.log(`✅ ${relative_path} has been optimized (-${diff_verbose}%)`);
133+
134+
if (Bun.env.CI) {
135+
console.log(
136+
`Stopping optimization in CI/CD env, as one diff is enough to make test fail`
137+
);
138+
break;
139+
}
91140
}
92141
}
93142

src/lib/components/BlogCta.svelte

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@
22
import { getAppwriteDashboardUrl } from '$lib/utils/dashboard';
33
import { Button } from '$lib/components/ui';
44
5-
export let heading: string = 'Start building with Appwrite today';
6-
export let label: string = 'Get started';
5+
let {
6+
heading = 'Start building with Appwrite today',
7+
label = 'Get started',
8+
description = undefined,
9+
href = getAppwriteDashboardUrl(),
10+
event = 'blog-cta-get_started_btn-click'
11+
}: {
12+
heading?: string;
13+
label?: string;
14+
description?: string;
15+
href?: string;
16+
event?: string;
17+
} = $props();
718
</script>
819

920
<div
10-
class="bg relative mt-12 -mb-6 flex min-h-[12rem] items-center justify-center overflow-hidden border-t border-[hsl(var(--web-color-subtle))] py-12"
21+
class="bg relative mt-12 -mb-6 flex min-h-48 items-center justify-center overflow-hidden border-t border-[hsl(var(--web-color-subtle))] py-12"
1122
>
1223
<div class="flex max-w-3xs flex-col items-center justify-center gap-5 text-center">
1324
<h2 class="text-label text-primary font-aeonik-pro">{heading}</h2>
14-
<Button href={getAppwriteDashboardUrl()} event="blog-cta-get_started_btn-click"
15-
>{label}</Button
16-
>
25+
{#if description}
26+
<p class="text-main-body text-muted-foreground">{description}</p>
27+
{/if}
28+
<Button {href} {event}>{label}</Button>
1729
</div>
1830
</div>
1931

0 commit comments

Comments
 (0)