SvelteKit 원격 함수

SvelteKit remote functions

앱의 어느 곳에서 호출하든 함수 본문은 서버에서 실행되고 결과만 클라이언트에 돌아옵니다.

···
html
<section class="stage"><div class="heading">REMOTE FUNCTION <span class="hint">call crosses boundary</span></div><div class="remote"><div class="tile client">CLIENT <small>query()</small></div><div class="link"><i></i></div><div class="tile server">SERVER <small>database</small></div></div><div class="result" id="remote-result">waiting for result</div><div class="caption">The server-only work stays across the boundary</div></section>
css
.stage{width:min(94vw,780px);height:min(88vh,322px);padding:clamp(12px,3vmin,20px);border:1px solid var(--line);border-radius:16px;background:var(--surface);box-shadow:0 12px 36px #0001;display:flex;flex-direction:column;gap:clamp(7px,2vmin,14px);font:500 clamp(12px,3.5vmin,17px)/1.3 var(--font-sans,system-ui,sans-serif);overflow:hidden}.heading{display:flex;justify-content:space-between;align-items:center;gap:8px;font-weight:750;color:var(--accent)}.hint{color:var(--muted);font-size:clamp(12px,3.2vmin,14px);font-weight:500}.caption{margin-top:auto;color:var(--muted);font-size:clamp(12px,3.2vmin,14px)}.row{display:flex;gap:clamp(5px,1.5vmin,12px);align-items:center}.tile{padding:clamp(6px,1.8vmin,13px);border:1px solid var(--line);border-radius:9px;background:var(--bg);text-align:center;min-width:0}.arrow{color:var(--accent);font-weight:800}.stage [data-on]{transition:background .3s,border-color .3s,opacity .3s,transform .3s}.stage .on{border-color:var(--accent);background:color-mix(in srgb,var(--accent) 14%,var(--surface));color:var(--accent)}.remote{flex:1;display:flex;align-items:center;justify-content:center;gap:8px}.remote .tile{width:32%;max-width:150px;font-weight:750}.remote small{display:block;margin-top:7px;color:var(--muted);font:600 .75em ui-monospace,monospace}.link{width:23%;height:4px;background:var(--line);position:relative}.link i{position:absolute;left:0;top:-8px;width:20px;height:20px;border-radius:50%;background:var(--accent-2);transition:left .35s,background .3s}.stage[data-step="1"] .link i{left:calc(100% - 20px)}.stage[data-step="2"] .link i{left:0;background:var(--accent)}.stage[data-step="1"] .server,.stage[data-step="2"] .client{border-color:var(--accent)}.result{text-align:center;color:var(--accent-2);font-weight:750}.stage small,.stage button,.stage .cards b,.stage .phase b,.stage .pair b,.stage .tree-mini b{font-size:clamp(12px,3.2vmin,14px)!important}
js
const stage=document.querySelector(".stage"),result=document.getElementById("remote-result"),messages=["calling query()","server reads data","result: 3 rows"];let step=0;function tick(){stage.dataset.step=String(step);result.textContent=messages[step];step=(step+1)%3}tick();let timer=setInterval(tick,1000);stage.addEventListener("pointerdown",()=>{clearInterval(timer);tick()})

SvelteKit 원격 함수는 클라이언트 컴포넌트에서 호출해도 서버에서 실행됩니다. 따라서 데이터베이스 클라이언트나 서버 전용 환경 변수에 접근할 수 있습니다.

호출은 네트워크 경계를 넘으므로 지연, 입력 검증, 권한 확인을 고려해야 합니다. 데모는 브라우저의 호출이 서버 경계를 건너 결과로 되돌아오는 흐름입니다.

언제 쓰나

SvelteKit UI에서 서버 데이터나 비밀 값이 필요한 로직을 안전한 실행 경계 뒤로 둘 때 사용합니다.

페이지로 열기 ↗