Guide: Hyperliquid on SvelteKit (tested starter and findings)

オープン 初心者向け
#62 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
documentation

調査の方向性

docs/hyperliquid/environments/ にある既存の Next.js と Vite の環境ページを読み、この issue のテスト済みスターターと調査結果を使って SvelteKit の環境ページを追加してください。ページの内容を、記載されている SvelteKit のセットアップ、REST ルート、ブラウザー WebSocket のライフサイクル、開発時と本番環境での結果と照らし合わせて確認してください。新しいページで SvelteKit 3 アダプターのセットアップを説明し、テスト済みスターターと実行手順を示せば完了です。

索引モデルが issue の本文から書いたものです。

説明

Hyperliquid on SvelteKit: tested starter and findings

There is no SvelteKit page under docs/hyperliquid/environments/ yet (bun, cloudflare-workers, deno, expo, next, node, react, typescript and vite exist). A web search for "hyperliquid sveltekit websocket price feed" this morning returned the Hyperliquid WebSocket docs and our Vite page as the nearest match, so a SvelteKit page would fill an empty spot. The starter below follows the Next.js page (server route) plus the Vite page (browser stream).

Tested 27 Nov 2026 on Node.js 24.22.1 with @siebly/hyperliquid-api 1.0.5, @sveltejs/kit 3.4.1, svelte 5.59.2, vite 8.4.0, @sveltejs/adapter-node 6.0.6 and @sveltejs/vite-plugin-svelte 7.3.2. Browser checks ran in headless Chrome.

Results

Check Dev (vite dev) Production (vite build, node build)
GET /api/price (RestClient in +server.js) 200, BTC mid returned 200, BTC mid returned
Page server render with a top-level import { WebsocketClient } 200 200
Live allMids in the browser (client created in onMount) BTC price shown BTC price shown
Leaving the page (cleanup calls closeAll()) no further errors no further errors

Findings

  1. SvelteKit 3 no longer reads svelte.config.js. With one present, vite build stops with config_file_unsupported. The adapter now goes into the Vite plugin: sveltekit({ adapter: adapter() }) in vite.config.js. Most SvelteKit tutorials still show svelte.config.js, so the page should say this up front.
  2. The SDK import is safe during server rendering. A plain top-level import in +page.svelte renders on the server without errors. Only creating the client and subscribing must wait for onMount, and the function returned from onMount is the place for closeAll().
  3. Server routes work as in Next.js. RestClient in src/routes/api/price/+server.js runs on the Node adapter with no extra config.
  4. WebSocket handshake time. From the test box the browser stream only connected with new WebsocketClient({ connectionTimeout: 5000 }). This is the same connectionTimeout default already raised for the other browser pages, so the starter below keeps the default to match them.

Starter files

package.json

{
  "name": "hyperliquid-sveltekit-quickstart",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite dev --host 127.0.0.1",
    "build": "vite build",
    "start": "node build"
  },
  "dependencies": {
    "@siebly/hyperliquid-api": "latest"
  },
  "devDependencies": {
    "@sveltejs/adapter-node": "6.0.6",
    "@sveltejs/kit": "3.4.1",
    "@sveltejs/vite-plugin-svelte": "7.3.2",
    "svelte": "5.59.2",
    "vite": "8.4.0"
  }
}

vite.config.js

import adapter from '@sveltejs/adapter-node';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [sveltekit({ adapter: adapter() })],
});

src/app.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    %sveltekit.head%
  </head>
  <body>
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

src/routes/api/price/+server.js

import { json } from '@sveltejs/kit';
import { RestClient } from '@siebly/hyperliquid-api';

export async function GET() {
  try {
    const client = new RestClient();
    const mids = await client.getAllMids();
    const price = mids.BTC;

    if (!price) {
      throw new Error('No price was returned.');
    }

    return json({ symbol: 'BTC', price: String(price) });
  } catch (error) {
    console.error('Price request failed:', error);
    return json(
      { error: 'Exchange price request failed. Check the server logs.' },
      { status: 502 },
    );
  }
}

src/routes/+page.svelte

<script>
  import { onMount } from 'svelte';
  import { WebsocketClient } from '@siebly/hyperliquid-api';

  let restPrice = $state('Loading');
  let livePrice = $state('Connecting');
  let errorText = $state('');

  onMount(() => {
    let stopped = false;
    const ws = new WebsocketClient();

    fetch('/api/price')
      .then((res) => res.json())
      .then((body) => {
        if (!stopped) restPrice = body.price ?? body.error;
      })
      .catch((error) => {
        if (!stopped) errorText = String(error);
      });

    ws.on('message', (message) => {
      if (message.channel !== 'allMids') return;
      const price = message.data?.mids?.BTC;
      if (!stopped && price) livePrice = String(price);
    });
    ws.on('exception', (event) => {
      if (!stopped) errorText = 'WebSocket error, see the console.';
      console.error(event);
    });

    ws.subscribe({ type: 'allMids' })
      .then(() => {
        if (stopped) ws.closeAll();
      })
      .catch((error) => {
        if (!stopped) errorText = String(error);
      });

    return () => {
      stopped = true;
      ws.closeAll();
    };
  });
</script>

<main>
  <h1>BTC price</h1>
  <p>REST (server route): <span id="rest">{restPrice}</span></p>
  <p>Live (WebSocket): <span id="live">{livePrice}</span></p>
  {#if errorText}<p id="error">{errorText}</p>{/if}
</main>

Run with npm install, then npm run dev, or npm run build and npm start (Node adapter, listens on port 3000 by default). Open / for the REST and live prices, or /api/price for the JSON.

Happy to turn this into a PR for starters/hyperliquid/sveltekit plus the environment page if that is easier.

主要言語
JavaScript
スター
1
フォーク
3
平均マージ
5時間 40分
マージ済み PR(30日)
57

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。