Skip to content

Commit 2d8a073

Browse files
committed
fix: resolve Svelte 5 reactivity warnings
- Use $derived for all computed values that depend on props - Fix Button, Cards, RelatedCard, and TagCard components - Fix all blog post pages and layouts - Fix date formatting in BlogPreview, BlogPostCard, etc. - Fix CodeBlock syntax highlighting reactivity - Update blog +page to use derived filtering This eliminates 41 of 42 warnings. The remaining warning in Toc.svelte is intentional as the selector is passed to a third-party library that doesn't support dynamic updates.
1 parent 337f6ec commit 2d8a073

File tree

33 files changed

+78
-57
lines changed

33 files changed

+78
-57
lines changed

src/lib/components/atoms/Button.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
...rest
3131
}: Props = $props();
3232
33-
const isExternalLink = !!href && HttpRegex.test(href);
33+
const isExternalLink = $derived(!!href && HttpRegex.test(href));
3434
35-
target = target ?? (isExternalLink ? '_blank' : '_self');
36-
rel = rel ?? (isExternalLink ? 'noopener noreferrer' : undefined);
35+
const finalTarget = $derived(target ?? (isExternalLink ? '_blank' : '_self'));
36+
const finalRel = $derived(rel ?? (isExternalLink ? 'noopener noreferrer' : undefined));
3737
3838
let tag = $derived(href ? 'a' : 'button');
3939
let linkProps = $derived({
4040
href,
41-
target,
42-
rel
41+
target: finalTarget,
42+
rel: finalRel
4343
});
4444
</script>
4545

src/lib/components/atoms/Cards.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
...rest
2020
}: Props = $props();
2121
22-
const isExternalLink = !!href && HttpRegex.test(href);
22+
const isExternalLink = $derived(!!href && HttpRegex.test(href));
2323
24-
target = target ?? (isExternalLink ? '_blank' : '_self');
25-
rel = rel ?? (isExternalLink ? 'noopener noreferrer' : undefined);
24+
const finalTarget = $derived(target ?? (isExternalLink ? '_blank' : '_self'));
25+
const finalRel = $derived(rel ?? (isExternalLink ? 'noopener noreferrer' : undefined));
2626
2727
function getRandomColor() {
2828
const letters = '0123456789ABCDEF';
@@ -45,8 +45,8 @@
4545
let tag = $derived(href ? 'a' : 'article');
4646
let linkProps = $derived({
4747
href,
48-
target,
49-
rel
48+
target: finalTarget,
49+
rel: finalRel
5050
});
5151
</script>
5252

src/lib/components/atoms/RelatedCard.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
...rest
2222
}: Props = $props();
2323
24-
const isExternalLink = !!href && HttpRegex.test(href);
24+
const isExternalLink = $derived(!!href && HttpRegex.test(href));
2525
26-
target = isExternalLink ? '_blank' : '_self';
27-
rel = isExternalLink ? 'noopener noreferrer' : undefined;
26+
const finalTarget = $derived(isExternalLink ? '_blank' : '_self');
27+
const finalRel = $derived(isExternalLink ? 'noopener noreferrer' : undefined);
2828
2929
let tag = $derived(href ? 'a' : 'article');
3030
let linkProps = $derived({
3131
href,
32-
target,
33-
rel
32+
target: finalTarget,
33+
rel: finalRel
3434
});
3535
</script>
3636

src/lib/components/atoms/TagCard.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
...rest
2424
}: Props = $props();
2525
26-
const isExternalLink = !!href && HttpRegex.test(href);
26+
const isExternalLink = $derived(!!href && HttpRegex.test(href));
2727
28-
target = isExternalLink ? '_blank' : '_self';
29-
rel = isExternalLink ? 'noopener noreferrer' : undefined;
28+
const finalTarget = $derived(isExternalLink ? '_blank' : '_self');
29+
const finalRel = $derived(isExternalLink ? 'noopener noreferrer' : undefined);
3030
3131
let tag = $derived(href ? 'a' : 'article');
3232
let linkProps = $derived({
3333
href,
34-
target,
35-
rel
34+
target: finalTarget,
35+
rel: finalRel
3636
});
3737
</script>
3838

src/lib/components/atoms/Toc.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1111
let { class: classes, selector = '#toc-contents' }: Props = $props();
1212
13+
// Note: selector is intentionally captured at initialization for createTableOfContents.
14+
// This library call doesn't support dynamic selector updates, so reactivity isn't needed.
1315
const {
1416
elements: { item },
1517
states: { activeHeadingIdxs, headingsTree }

src/lib/components/molecules/BlogPostCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
let { title, coverImage = undefined, slug, date, contributor }: Props = $props();
1313
14-
const formattedDate = formatDate(date);
14+
const formattedDate = $derived(formatDate(date));
1515
</script>
1616

1717
<a href="/blog/{slug}" class="container">

src/lib/components/molecules/BlogPreview.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
let { post_data } = $props();
55
6-
const formattedDate = formatDate(post_data.date);
6+
const formattedDate = $derived(formatDate(post_data.date));
77
</script>
88

99
<div class="container">

src/lib/components/molecules/CodeBlock.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
return code.replace(/\\n/g, '\n');
4141
}
4242
43-
let normalizedCode = normalizeIndentation(processCodeForDisplay(code));
43+
let normalizedCode = $derived(normalizeIndentation(processCodeForDisplay(code)));
4444
45-
let highlightedCode = hljs.highlight(normalizedCode, { language: lang }).value;
45+
let highlightedCode = $derived(hljs.highlight(normalizedCode, { language: lang }).value);
4646
</script>
4747

4848
<div class="code-block" class:full-bleed={fullBleed} bind:this={codeBlockElement}>

src/lib/components/molecules/RelatedPostCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
let { title, excerpt, slug, tags, readingTime = undefined, date }: Props = $props();
1616
17-
const formattedDate = formatDate(date);
17+
const formattedDate = $derived(formatDate(date));
1818
</script>
1919

2020
<RelatedCard href="/{slug}" target="_self">

src/lib/components/molecules/TagCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
let { title, coverImage, slug, date, excerpt }: Props = $props();
1414
15-
const formattedDate = formatDate(date);
15+
const formattedDate = $derived(formatDate(date));
1616
</script>
1717

1818
<TagCard href="/blog/{slug}" target="_self">

0 commit comments

Comments
 (0)