Skip to content

Commit 8e2d93b

Browse files
authored
Merge pull request #359 from vim-jp-radio/remote
Remote function
2 parents 95c1306 + 7607ae0 commit 8e2d93b

File tree

9 files changed

+354
-168
lines changed

9 files changed

+354
-168
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@std/collections": "jsr:^1.0.8",
3030
"@sveltejs/adapter-static": "^3.0.8",
3131
"@sveltejs/enhanced-img": "^0.6.0",
32-
"@sveltejs/kit": "^2.21.2",
32+
"@sveltejs/kit": "^2.27.0",
3333
"@sveltejs/vite-plugin-svelte": "^5.0.3",
3434
"@types/node": "^20.17.57",
3535
"@unocss/eslint-plugin": "^66.1.3",
@@ -42,14 +42,15 @@
4242
"lint-staged": "^16.1.0",
4343
"renovate": "^40.40.1",
4444
"std-env": "^3.9.0",
45-
"svelte": "^5.33.14",
45+
"svelte": "^5.37.2",
4646
"svelte-check": "^4.2.1",
4747
"svelte-eslint-parser": "^1.2.0",
4848
"svelte-preprocess-budoux": "^1.1.2",
4949
"sveltweet": "^0.4.3",
5050
"typescript": "^5.8.3",
5151
"typescript-svelte-plugin": "^0.3.47",
5252
"unocss": "^66.1.3",
53+
"valibot": "^1.1.0",
5354
"vite": "npm:rolldown-vite@latest",
5455
"vite-plugin-favicons": "^0.1.7"
5556
},

pnpm-lock.yaml

Lines changed: 292 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/Tweet/Tweet.svelte

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang='ts'>
2+
import { Tweet as Sveltweet } from 'sveltweet';
3+
import { getTweet } from './getTweet.remote';
4+
5+
type Props = Parameters<typeof getTweet>[0];
6+
7+
const { id }: Props = $props();
8+
const tweet = getTweet({ id });
9+
</script>
10+
11+
{#if tweet.current != null}
12+
<Sveltweet tweet={tweet.current} />
13+
{/if}

src/lib/Tweet/getTweet.remote.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { prerender } from '$app/server';
2+
import { retry } from '@std/async';
3+
import { error } from '@sveltejs/kit';
4+
import * as sveltweet from 'sveltweet/api';
5+
import * as v from 'valibot';
6+
7+
const getTweetPropsSchema = v.object({
8+
id: v.string(),
9+
});
10+
11+
/**
12+
* tweet を取得するRemote function
13+
* https://svelte.dev/docs/kit/remote-functions
14+
*/
15+
export const getTweet = prerender(getTweetPropsSchema, async ({ id }) => {
16+
const tweet = await retry(async () => {
17+
const _tweet = await sveltweet.getTweet(id);
18+
19+
if (_tweet == null) {
20+
throw new Error(`Retry: Tweet not found: ${id}`);
21+
}
22+
return _tweet;
23+
});
24+
if (tweet == null) {
25+
error(404, `Tweet not found: ${id}`);
26+
}
27+
return tweet;
28+
});

src/lib/Tweet/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Tweet from './Tweet.svelte';
2+
3+
export { Tweet };

src/routes/+page.server.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/routes/+page.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import Personalities from './Personalities.svelte';
77
import Platforms from './Platforms.svelte';
88
import RadioLetter from './RadioLetter.svelte';
9-
10-
/* +page.server.ts からデータを取得 */
11-
const { data } = $props();
129
</script>
1310

1411
<Header />
@@ -18,7 +15,7 @@
1815
<Platforms />
1916
<Personalities />
2017
<RadioLetter />
21-
<ListenersTweets tweets={data.tweets} />
18+
<ListenersTweets />
2219
</main>
2320

2421
<Footer />

src/routes/ListenersTweets.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<script lang='ts'>
2-
import type { Tweet as TweetT } from 'sveltweet/api';
32
import { Heading } from '$/lib/Heading';
4-
import { Tweet } from 'sveltweet';
5-
6-
const { tweets }: { tweets: TweetT[] } = $props();
3+
import { LISTENERS_TWEET_IDS } from '$/lib/links';
4+
import { Tweet } from '$/lib/Tweet';
75
</script>
86

97
<section>
108
<Heading title='リスナーの声' />
119
<div data-theme='dark'>
12-
{#each tweets as tweet (tweet.id_str)}
13-
<Tweet {tweet} />
10+
{#each LISTENERS_TWEET_IDS as id (id)}
11+
<Tweet {id} />
1412
{/each}
1513
</div>
1614
</section>

svelte.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const config = {
1919
budouxPreprocess({ language: 'ja', attribute: 'data-budoux' }), // budouxを使って日本語の改行をいい感じにする。 https://github.com/google/budoux/tree/main/javascript https://github.com/ryoppippi/svelte-preprocess-budoux
2020
],
2121

22+
compilerOptions: {
23+
experimental: {
24+
async: true,
25+
},
26+
},
27+
2228
kit: {
2329
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
2430
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
@@ -27,6 +33,10 @@ const config = {
2733
fallback: '404.html',
2834
}),
2935

36+
experimental: {
37+
remoteFunctions: true,
38+
},
39+
3040
typescript: {
3141
config(config) {
3242
config.include.push(path.join(import.meta.dirname, 'uno.config.ts'));

0 commit comments

Comments
 (0)