1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| Node.js ๋ฐํ์ | Node.js v18.17.0+ (LTS) | Node.js v22.x LTS ๋๋ Bun v1.2+ | ES Modules, Web Streams API ๋ฐ Top-level Await ํ์ |
| ํจํค์ง ๋งค๋์ | npm v9.0+ / pnpm v8.0+ | pnpm v10.0+ ๋๋ bun | ์ด๊ณ ์ ๋ชจ๋ ์บ์ฑ ๋ฐ ๋ชจ๋ ธ๋ ํฌ ์ฌ๋ณผ๋ฆญ ๋งํฌ ์ต์ ํ |
| ์ด์์ฒด์ (OS) | macOS 12+, Windows 10/11, Linux (Ubuntu 20.04+) | macOS 14+ (Apple Silicon), Windows 11, Linux | Rust ๊ธฐ๋ฐ Qwik Optimizer ์ปดํ์ผ๋ฌ ํฌ๋ก์ค ํ๋ซํผ ์ง์ |
| CPU | 2 ์ฝ์ด ์ด์ (x86_64 / ARM64) | 4 ์ฝ์ด ~ 8 ์ฝ์ด ์ด์ ์ต์ CPU | Vite ๋ฐ Rust Optimizer($ ๊ฒฝ๊ณ ์ฒญํฌ ๋ฏธ์ธ ๋ถํ ) ๊ณ ์ ๋น๋ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | ์ต์ 4 GB ์ด์ | 8 GB ~ 16 GB ์ด์ | Qwik City SSR ๊ฐ๋ฐ ์๋ฒ ๋ฐ ๋ธ๋ผ์ฐ์ HMR ๋ฒํผ ํ๋ณด |
| ๋์คํฌ (Storage) | 2 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 10 GB ์ด์์ ๊ณ ์ NVMe SSD | node_modules ๋ฐ .qwik ๋น๋ ์ํฐํฉํธ ์บ์ ๊ณต๊ฐ |
[!IMPORTANT] ๊ณต์ ๋ฒค๋ ์ต์ ๋ฒ์ ์ ๋ณด (Builder.io / Qwik Team):
- Qwik ์ฝ์ด & ์ปดํ์ผ๋ฌ: Qwik v1.12+ (์์ ํ LTS ๋ฒ์ , Rust ๊ธฐ๋ฐ Optimizer ํ์ฌ)
- Qwik City ๋ฉํ ํ๋ ์์ํฌ: Qwik City v1.12+ (๋๋ ํฐ๋ฆฌ ๊ธฐ๋ฐ ํ์ผ ๋ผ์ฐํ , ์๋ฒ ์ก์ , RPC ๋ฐ ์์ง ์ด๋ํฐ ์ง์)
- ์ฌ๊ฐ ๊ฐ๋ฅ์ฑ(Resumability)๊ณผ ๊ธฐ์กด ํ์ด๋๋ ์ด์ (Hydration)์ ๊ทผ๋ณธ์ ์ฐจ์ด:
- React, Vue, Svelte, Next.js ๋ฑ์ SSR ํ ๋ธ๋ผ์ฐ์ ๊ฐ ์ ์ฒด ์ปดํฌ๋ํธ ํธ๋ฆฌ๋ฅผ ๋ค์ ์คํํ๊ณ ๋ฉ๋ชจ๋ฆฌ์ VDOM/์ํ๋ฅผ ๋ณต์ํ๋ ํ์ด๋๋ ์ด์ (Hydration) ๊ณผ์ ์ ๊ฑฐ์ณ์ผ ๋ฒํผ ํด๋ฆญ ๋ฑ์ ์ํธ์์ฉ์ด ๊ฐ๋ฅํด์ง๋๋ค(TTI ์ง์ฐ ๋ฐ ๋ฉ์ธ ์ค๋ ๋ ๋ธ๋กํน).
- **Qwik์ ํ์ด๋๋ ์ด์ ์ ์์ ํ ์ ๊ฑฐ(Zero Hydration)**ํ์ต๋๋ค. ์๋ฒ๊ฐ ๋ ๋๋งํ ๋ชจ๋ ์ํ์ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ ๋ณด๋ฅผ HTML ๋ฐ ์ธ๋ผ์ธ JSON(
qwik/json)์ ์๋ฒฝํ๊ฒ ์ง๋ ฌํ(Serialize)ํ์ฌ ๋ด๋ ค๋ณด๋ ๋๋ค. ๋ธ๋ผ์ฐ์ ๋ ์ด๊ธฐ ๋ก๋ฉ ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๋จ 1๋ฐ์ดํธ๋ ์ฆ์ ์คํํ์ง ์์ผ๋ฉฐ, ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ๋ ์๊ฐ ๊ทธ ๋ฒํผ์ ํธ๋ค๋ฌ ์ฝ๋๋ง ์ง์ฐ ๋ก๋ฉ(QRL)ํ์ฌ ์ฆ์ โ์ฌ๊ฐ(Resume)โํฉ๋๋ค.
๐ 1. ํ๋ก์ ํธ ์์ฑ ๋ฐ ํจํค์ง ์ค์น
๊ณต์ CLI(create-qwik)๋ฅผ ํตํด TypeScript ๊ธฐ๋ฐ Qwik City ํ์คํ ์ ํ๋ฆฌ์ผ์ด์
์ ์์ฑํฉ๋๋ค.
# [1] pnpm์ ์ด์ฉํ ํ๋ก์ ํธ ์์ฑ (๊ถ์ฅ)
pnpm create qwik@latest my-qwik-app
# [2] npm์ ์ด์ฉํ ์์ฑ
npm create qwik@latest my-qwik-app
# [3] bun์ ์ด์ฉํ ์์ฑ
bun create qwik@latest my-qwik-app
# ๋ํํ ํ๋กฌํํธ ์ ํ ๊ฐ์ด๋:
# ? Project name: my-qwik-app
# ? Select a starter: Starter (Basic App with Qwik City and sample routes)
# ? Install dependencies: Yes
# ? Initialize a new git repository: Yes
# ํ๋ก์ ํธ ๋๋ ํฐ๋ฆฌ ์ด๋ ๋ฐ ๊ฐ๋ฐ ์๋ฒ ์คํ
cd my-qwik-app
pnpm install
pnpm run dev
# ์ถ๋ ฅ: Local: http://localhost:5173/
2. Qwik ์ํคํ ์ฒ & ํต์ฌ ๋ฉํ ๋ชจ๋ธ (Mental Model & Architecture)
๐งญ 1. Resumability vs Hydration ์คํ ๋ผ์ดํ์ฌ์ดํด ๋น๊ต
[ ์ ํต์ ์ธ React / Vue SSR (Hydration) ]
์๋ฒ SSR HTML ์์ฑ โ ๋ธ๋ผ์ฐ์ HTML ์์ โ ์ ์ฒด JS ๋ฒ๋ค ๋ค์ด๋ก๋ โ ์ ์ฒด ์ปดํฌ๋ํธ ํธ๋ฆฌ ์ฌ์คํ (Hydration) โ [์ํธ์์ฉ ๊ฐ๋ฅ (TTI)]
(๋จ์ : ํ์ด์ง๊ฐ ์๋ฌด๋ฆฌ ์ปค์ ธ๋ ์ด๊ธฐ ์๋ฐ์คํฌ๋ฆฝํธ ์คํ ๋น์ฉ์ด ๋น๋กํด์ ํญ์ฆํจ)
[ Qwik์ ์ฌ๊ฐ ๊ฐ๋ฅ์ฑ (Resumability) ]
์๋ฒ SSR HTML + qwik/json ์์ฑ โ ๋ธ๋ผ์ฐ์ HTML ๋ ๋๋ง [์ฆ์ ์ธํฐ๋ํฐ๋ธ ์ค๋น ์๋ฃ!]
(์ด๊ธฐ ์๋ฐ์คํฌ๋ฆฝํธ ์คํ: 0ms, ์ฝ 1KB ์์ค์ ์ด๊ฒฝ๋ ์ด๋ฒคํธ ๋์คํจ์ฒ qwikloader๋ง ๊ตฌ๋)
โ
โผ ์ฌ์ฉ์๊ฐ ํน์ ๋ฒํผ ํด๋ฆญ ์
HTML์ QRL ์์ฑ ๊ฐ์ง โ ํด๋น ํธ๋ค๋ฌ ์ฒญํฌ๋ง ์ง์ฐ ๋ก๋ฉ (Lazy-load) โ ์ง๋ ฌํ๋ ์ํ ๋ณต์ โ ์ด๋ฒคํธ ์ฆ์ ์คํ
๐งฉ 2. QRL(Qwik Resource Locator)๊ณผ ๋ฌ๋ฌ ๊ธฐํธ($)์ ์ปดํ์ผ ๊ฒฝ๊ณ
Qwik ์ฝ๋์์ ํจ์ ์ด๋ฆ ๋์ ๋ถ๋ **$ ๊ธฐํธ(์: component$, onClick$, useTask$, routeLoader$)**๋ ๋จ์ํ ๋ค์ด๋ฐ์ด ์๋๋ผ, Qwik Optimizer(Rust ์ปดํ์ผ๋ฌ)์๊ฒ ํด๋น ํด๋ก์ ๋ฅผ ๋ณ๋์ ๋
๋ฆฝ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฒญํฌ ํ์ผ๋ก ๋ถํ ์ถ์ถํ๋ผ๋ ์ปดํ์ผ ์ง์์ด์
๋๋ค.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ src/components/counter.tsx (์์ค ์ฝ๋) โ
โ โ
โ export const Counter = component$(() => { โโโ [์ฒญํฌ A๋ก ๋ถ๋ฆฌ] โ
โ const count = useSignal(0); โ
โ return ( โ
โ <button onClick$={() => count.value++}> โโโ [์ฒญํฌ B๋ก ๋ถ๋ฆฌ] โ
โ Count: {count.value} โ
โ </button> โ
โ ); โ
โ }); โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ Qwik Optimizer ๋น๋ ์ฐ์ถ๋ฌผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ chunk-component.js (A) โ โ chunk-click-handler.js (B)โ
โ ์ปดํฌ๋ํธ ๋ ๋๋ง ๋ก์ง ์ ์ โ โ () => count.value++ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3. [์ด๋ณด์ 1๋จ๊ณ] ํฌ๋ก์๋ & ์ปดํฌ๋ํธ ๊ธฐ์ด (component$, useSignal, onClick$)
๋ณต์ฌํ์ฌ ๋ฐ๋ก ์คํํ ์ ์๋ ์ต์ ๋จ์์ ๋ฐ์ํ ์นด์ดํฐ ์ปดํฌ๋ํธ ์์ ์ ๋๋ค.
// src/components/counter/counter.tsx
import { component$, useSignal } from '@builder.io/qwik';
/**
* [์ด๋ณด์ 1๋จ๊ณ] ๊ธฐ๋ณธ ์นด์ดํฐ ์ปดํฌ๋ํธ
* - component$: ์ปดํฌ๋ํธ ๊ฒฝ๊ณ๋ฅผ ์ ์ํ๋ ์ต์ ํ ๋งคํฌ๋ก
* - useSignal: ๋จ์ผ ๋ฐ์ํ ๊ฐ(์์ํ)์ ๋ณด๊ดํ๋ ์ ํธ(Signal)
* - onClick$: ํด๋ฆญ ์์๋ง ํด๋น ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฒญํฌ๋ฅผ ์ง์ฐ ๋ค์ด๋ก๋ํ์ฌ ์คํ
*/
export const Counter = component$(() => {
// 1. ๋ฐ์ํ ์ ํธ ์ ์ธ (์ด๊น๊ฐ 0)
const count = useSignal(0);
return (
<div class="counter-card" style={{ padding: '1.5rem', border: '1px solid #10b981', borderRadius: '8px' }}>
<h3 style={{ margin: '0 0 1rem 0' }}>๐ Qwik ๋ฐ์ํ ์นด์ดํฐ</h3>
<p style={{ fontSize: '1.25rem', fontWeight: 'bold' }}>
ํ์ฌ ์นด์ดํธ: <span style={{ color: '#10b981' }}>{count.value}</span>
</p>
<div style={{ display: 'flex', gap: '0.5rem' }}>
{/* onClick$ ํธ๋ค๋ฌ๋ ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ๋๋ฅด๊ธฐ ์ ๊น์ง ๋ธ๋ผ์ฐ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ค์ด๋ก๋๋์ง ์์ */}
<button
type="button"
onClick$={() => count.value++}
style={{ padding: '0.5rem 1rem', background: '#10b981', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }}
>
์ฆ๊ฐ (+1)
</button>
<button
type="button"
onClick$={() => count.value--}
style={{ padding: '0.5rem 1rem', background: '#ef4444', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }}
>
๊ฐ์ (-1)
</button>
<button
type="button"
onClick$={() => (count.value = 0)}
style={{ padding: '0.5rem 1rem', background: '#64748b', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }}
>
์ด๊ธฐํ
</button>
</div>
</div>
);
});
// ์์ ๋ธ๋ผ์ฐ์ ์ถ๋ ฅ:
// [์ด๊ธฐ HTML ๋ก๋ฉ ์]: ์๋ฐ์คํฌ๋ฆฝํธ ๋ค์ด๋ก๋ 0B, ์ ์ HTML ํํ๋ก ์ฆ์ ํ๋ฉด ๋ ๋๋ง
// [์ฆ๊ฐ ๋ฒํผ ํด๋ฆญ ์]: chunk-button-click.js(์๋ฐฑ ๋ฐ์ดํธ)๋ง ๋คํธ์ํฌ ํญ์ ๋ก๋๋ ํ {count.value} ํ
์คํธ ๋
ธ๋๋ง ์ ๋ฐ ํํฌ์ธํธ ๊ฐฑ์
4. [2๋จ๊ณ] ๋ฐ์ํ ์ํ ๊ด๋ฆฌ & ๋ผ์ดํ์ฌ์ดํด (Signals vs Stores, useTask$, useComputed$, useVisibleTask$)
๋จ์ํ ์์๊ฐ(useSignal)๋ถํฐ ๋ณตํฉ ๊ฐ์ฒด(useStore), ํ์ ๊ณ์ฐ๊ฐ(useComputed$), ์๋ฒ/ํด๋ผ์ด์ธํธ ๋น๋๊ธฐ ๋ฐ์ํ ํ์คํฌ(useTask$), ๋ธ๋ผ์ฐ์ ์ ์ฉ ๋ง์ดํธ ํ์คํฌ(useVisibleTask$)๋ฅผ ๋น๊ตํฉ๋๋ค.
// src/components/advanced-state/user-profile.tsx
import {
component$,
useSignal,
useStore,
useComputed$,
useTask$,
useVisibleTask$
} from '@builder.io/qwik';
interface UserState {
username: string;
role: 'admin' | 'editor' | 'viewer';
loginCount: number;
preferences: {
darkMode: boolean;
notifications: boolean;
};
}
export const UserProfile = component$(() => {
// 1. ๋จ์ผ ์์ํ ๊ฐ: useSignal
const searchKeyword = useSignal('');
// 2. ๋ณตํฉ ๊ฐ์ฒด ๋ฐ ์ค์ฒฉ ์ํ: useStore (deep: true๋ก ๊น์ ๊ฐ์ ํ์ฑํ)
const user = useStore<UserState>({
username: 'lucky_developer',
role: 'admin',
loginCount: 42,
preferences: {
darkMode: true,
notifications: true,
},
}, { deep: true });
// 3. ํ์ ์ํ: useComputed$ (์์กดํ๋ Signal์ด๋ Store๊ฐ ๋ฐ๋ ๋๋ง ์ฌ๊ณ์ฐ)
const isSuperAdmin = useComputed$(() => {
return user.role === 'admin' && user.loginCount >= 10;
});
// 4. ๋ฐ์ํ ํ์คํฌ: useTask$ (์๋ฒ์ ํด๋ผ์ด์ธํธ ๋ชจ๋์์ ์คํ ๊ฐ๋ฅ, ์์กด์ฑ ์ถ์ )
useTask$(({ track }) => {
// searchKeyword.value๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ์๋ ํธ๋ฆฌ๊ฑฐ
const keyword = track(() => searchKeyword.value);
console.log(`[useTask$] ๊ฒ์ ํค์๋ ๊ฐ์ง: "${keyword}"`);
});
// 5. ๋ธ๋ผ์ฐ์ ์ ์ฉ ๋ง์ดํธ ํ์คํฌ: useVisibleTask$
// โ ๏ธ ์ฃผ์: ๋ธ๋ผ์ฐ์ DOM์ด ํ๋ฉด์ ๋ณด์ผ ๋๋ง ์คํ๋๋ฏ๋ก Resumability ์ต์ ํ๋ฅผ ํด์น์ง ์๋๋ก ์ ์คํ ์ฌ์ฉ
useVisibleTask$(({ cleanup }) => {
console.log('[useVisibleTask$] ๋ธ๋ผ์ฐ์ ํ๊ฒฝ ๋ง์ดํธ ์๋ฃ, ์๋์ฐ ๋ฆฌ์ฌ์ด์ฆ ๊ฐ์ ์์');
const onResize = () => console.log('์๋์ฐ ๋๋น:', window.innerWidth);
window.addEventListener('resize', onResize);
cleanup(() => {
window.removeEventListener('resize', onResize);
});
});
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem', padding: '1rem', background: '#1e293b', color: '#f8fafc', borderRadius: '8px' }}>
<h3>๐ค ์ฌ์ฉ์ ํ๋กํ & ๋ฐ์ํ ์ํ ์ผํฐ</h3>
<div>
<label>์ค์๊ฐ ๊ฒ์ ํํฐ: </label>
<input
type="text"
value={searchKeyword.value}
onInput$={(e, el) => (searchKeyword.value = el.value)}
placeholder="๊ฒ์์ด๋ฅผ ์
๋ ฅํ์ธ์..."
style={{ padding: '0.4rem 0.8rem', borderRadius: '4px', border: '1px solid #475569' }}
/>
</div>
<div style={{ padding: '0.75rem', background: '#334155', borderRadius: '6px' }}>
<p>๊ณ์ ๋ช
: <strong>{user.username}</strong></p>
<p>๊ถํ: <strong>{user.role}</strong> ({isSuperAdmin.value ? '๐ ์ต๊ณ ๊ด๋ฆฌ์ ๋ฑ๊ธ' : '์ผ๋ฐ ๋ฑ๊ธ'})</p>
<p>๋ก๊ทธ์ธ ํ์: <strong>{user.loginCount}</strong>ํ</p>
<p>๋คํฌ๋ชจ๋ ์ํ: <strong>{user.preferences.darkMode ? 'ON ๐' : 'OFF โ๏ธ'}</strong></p>
</div>
<div style={{ display: 'flex', gap: '0.5rem' }}>
<button
type="button"
onClick$={() => user.loginCount++}
style={{ padding: '0.4rem 0.8rem', background: '#3b82f6', color: '#fff', border: 'none', borderRadius: '4px' }}
>
๋ก๊ทธ์ธ ์นด์ดํธ ์ฆ๊ฐ
</button>
<button
type="button"
onClick$={() => (user.preferences.darkMode = !user.preferences.darkMode)}
style={{ padding: '0.4rem 0.8rem', background: '#8b5cf6', color: '#fff', border: 'none', borderRadius: '4px' }}
>
๋คํฌ๋ชจ๋ ํ ๊ธ
</button>
</div>
</div>
);
});
5. [3๋จ๊ณ] Qwik City ํ์คํ ์น ๊ฐ๋ฐ (Routing, routeLoader$, routeAction$, server$)
Qwik City๋ ๊ณต์ ๋๋ ํฐ๋ฆฌ ๊ธฐ๋ฐ ๋ผ์ฐํฐ์ด์ ํ์คํ ์น ํ๋ ์์ํฌ์
๋๋ค. ์๋ฒ ๋ฐ์ดํฐ ํจ์นญ(routeLoader$), ํผ ์ ์ถ ์ฒ๋ฆฌ(routeAction$), ์ธ๋ผ์ธ ์๋ฒ ํจ์(server$)๋ฅผ ์๋ฒฝํ๊ฒ ์ง์ํฉ๋๋ค.
๐ Qwik City ๋ผ์ฐํ ํ์ผ ์์คํ ๊ท์น
src/routes/
โโ layout.tsx # ์ ์ญ ๋ ์ด์์ (GNB, Header, Footer)
โโ index.tsx # ๋ฉ์ธ ๋๋ฉ ํ์ด์ง (/)
โโ about/
โ โโ index.tsx # ์๊ฐ ํ์ด์ง (/about)
โโ products/
โโ layout.tsx # ์ํ ์๋ธ ๋ ์ด์์
โโ index.tsx # ์ํ ๋ชฉ๋ก (/products)
โโ [id]/
โโ index.tsx # ๋์ ํ๋ผ๋ฏธํฐ ์ํ ์์ธ (/products/:id)
๐ป routeLoader$์ routeAction$์ ํ์ฉํ ํ์คํ ํ์ด์ง ์์
// src/routes/products/index.tsx
import { component$ } from '@builder.io/qwik';
import {
routeLoader$,
routeAction$,
Form,
zod$,
z,
server$,
type DocumentHead
} from '@builder.io/qwik-city';
// ์ํ ๋ฐ์ดํฐ ํ์
์ธํฐํ์ด์ค
interface Product {
id: string;
name: string;
price: number;
}
// ๊ฐ์์ ์ธ๋ฉ๋ชจ๋ฆฌ DB ๋ฐ์ดํฐ๋ฒ ์ด์ค
const DB_PRODUCTS: Product[] = [
{ id: 'prod-1', name: '๋งฅ๋ถ ํ๋ก M3 Max', price: 4200000 },
{ id: 'prod-2', name: '๋ก์งํ
MX Master 3S', price: 139000 },
{ id: 'prod-3', name: 'LG 4K 32์ธ์น ๋ชจ๋ํฐ', price: 790000 },
];
/**
* [1] routeLoader$: ํ์ด์ง ๋ ๋๋ง ์ ์๋ฒ์์ ์คํ๋์ด ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๋ ๋ก๋
* - ๋ธ๋ผ์ฐ์ ๋ฒ๋ค์ ์ ๋ ํฌํจ๋์ง ์์ผ๋ฉฐ, ์๋ฒ์์๋ง ์์ ํ๊ฒ ์คํ๋จ (DB ์ง์ ์ฟผ๋ฆฌ ๊ฐ๋ฅ)
*/
export const useProductList = routeLoader$(async (requestEvent) => {
console.log(`[์๋ฒ ๋ก๋] ํด๋ผ์ด์ธํธ IP: ${requestEvent.clientConn.ip} ์์ ์ํ ๋ชฉ๋ก ์์ฒญ`);
// ์ค์ ํ๊ฒฝ: const products = await db.query('SELECT * FROM products');
return DB_PRODUCTS;
});
/**
* [2] routeAction$: ํผ ์ ์ถ(Form submission)์ ์ฒ๋ฆฌํ๋ ์๋ฒ ์ก์
* - Zod ์คํค๋ง ๊ฒ์ฆ๊ธฐ(zod$)๋ฅผ ๋ด์ฅํ์ฌ ํ์
์์ ํ ์
๋ ฅ๊ฐ ์๋ ๊ฒ์ฆ ์ง์
* - ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋นํ์ฑํ๋ ํ๊ฒฝ์์๋ ํ์ค HTML POST ํผ์ผ๋ก ์ ์ง์ ํฅ์(Progressive Enhancement) ๋์
*/
export const useAddProduct = routeAction$(
async (formData, requestEvent) => {
const newProduct: Product = {
id: `prod-${Date.now()}`,
name: formData.name,
price: formData.price,
};
DB_PRODUCTS.push(newProduct);
console.log('[์๋ฒ ์ก์
] ์ ๊ท ์ํ ๋ฑ๋ก ์ฑ๊ณต:', newProduct);
return { success: true, product: newProduct };
},
zod$({
name: z.string().min(2, { message: '์ํ๋ช
์ ์ต์ 2๊ธ์ ์ด์์ด์ด์ผ ํฉ๋๋ค.' }),
price: z.coerce.number().min(1000, { message: '๊ฐ๊ฒฉ์ ์ต์ 1,000์ ์ด์์ด์ด์ผ ํฉ๋๋ค.' }),
})
);
/**
* [3] server$: ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ ๋ด๋ถ์์ ๋ฐ๋ก ํธ์ถํ ์ ์๋ RPC ์๊ฒฉ ํจ์
*/
export const checkServerHealth = server$(async function() {
return {
status: 'HEALTHY',
serverTime: new Date().toISOString(),
nodeVersion: process.version,
};
});
// ๋ฉ์ธ Qwik ํ์ด์ง ์ปดํฌ๋ํธ
export default component$(() => {
const productsSignal = useProductList();
const addAction = useAddProduct();
return (
<div style={{ maxWidth: '800px', margin: '0 auto', padding: '2rem', fontFamily: 'sans-serif' }}>
<h1>๐ฆ Qwik City ํ์คํ ์ํ ๊ด๋ฆฌ ์์คํ
</h1>
{/* 1. ์๋ฒ ๋ก๋ ๋ฐ์ดํฐ ๋ ๋๋ง */}
<section style={{ marginBottom: '2rem' }}>
<h2>ํ์ฌ ๋ฑ๋ก๋ ์ํ ๋ชฉ๋ก</h2>
<ul style={{ listStyle: 'none', padding: 0 }}>
{productsSignal.value.map((item) => (
<li
key={item.id}
style={{ display: 'flex', justifyContent: 'space-between', padding: '0.75rem', borderBottom: '1px solid #e2e8f0' }}
>
<span>{item.name}</span>
<strong style={{ color: '#10b981' }}>{item.price.toLocaleString()} ์</strong>
</li>
))}
</ul>
</section>
{/* 2. routeAction ๊ธฐ๋ฐ ์ ์ง์ ํฅ์ Form ์ปดํฌ๋ํธ */}
<section style={{ padding: '1.5rem', background: '#f8fafc', borderRadius: '8px', border: '1px solid #cbd5e1' }}>
<h2>์ ๊ท ์ํ ๋ฑ๋ก</h2>
<Form action={addAction} style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
<div>
<label style={{ display: 'block', marginBottom: '0.25rem' }}>์ํ๋ช
</label>
<input
name="name"
type="text"
placeholder="์: ์ ํ ๋งค์ง ํธ๋ํจ๋"
style={{ width: '100%', padding: '0.5rem', borderRadius: '4px', border: '1px solid #94a3b8' }}
/>
{addAction.value?.failed && addAction.value.fieldErrors?.name && (
<span style={{ color: '#ef4444', fontSize: '0.85rem' }}>{addAction.value.fieldErrors.name}</span>
)}
</div>
<div>
<label style={{ display: 'block', marginBottom: '0.25rem' }}>๊ฐ๊ฒฉ (์)</label>
<input
name="price"
type="number"
placeholder="์: 159000"
style={{ width: '100%', padding: '0.5rem', borderRadius: '4px', border: '1px solid #94a3b8' }}
/>
{addAction.value?.failed && addAction.value.fieldErrors?.price && (
<span style={{ color: '#ef4444', fontSize: '0.85rem' }}>{addAction.value.fieldErrors.price}</span>
)}
</div>
<button
type="submit"
disabled={addAction.isRunning}
style={{ padding: '0.75rem', background: '#10b981', color: '#fff', border: 'none', borderRadius: '4px', fontWeight: 'bold', cursor: 'pointer' }}
>
{addAction.isRunning ? '๋ฑ๋ก ์ฒ๋ฆฌ ์ค...' : '์ํ ๋ฑ๋กํ๊ธฐ'}
</button>
{addAction.value?.success && (
<p style={{ color: '#10b981', margin: 0 }}>โ ์ํ์ด ์ฑ๊ณต์ ์ผ๋ก ๋ฑ๋ก๋์์ต๋๋ค!</p>
)}
</Form>
</section>
{/* 3. server$ RPC ํจ์ ์ฆ์ ํธ์ถ */}
<section style={{ marginTop: '2rem' }}>
<button
type="button"
onClick$={async () => {
const health = await checkServerHealth();
alert(`[์๋ฒ ์ํ]: ${health.status}\n์๊ฐ: ${health.serverTime}\n๋ฐํ์: ${health.nodeVersion}`);
}}
style={{ padding: '0.5rem 1rem', background: '#3b82f6', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }}
>
๐ ์๋ฒ ํฌ์ค์ฒดํฌ RPC ํธ์ถ
</button>
</section>
</div>
);
});
// DocumentHead: SEO ๋ฉํ ํ๊ทธ ๋ฐ ์คํ๊ทธ๋ํ ์ค์
export const head: DocumentHead = {
title: '์ํ ๊ด๋ฆฌ ์ผํฐ | Qwik City ํ์คํ',
meta: [
{
name: 'description',
content: 'Qwik City routeLoader$์ routeAction$์ผ๋ก ๊ตฌ์ถ๋ ๊ณ ์ฑ๋ฅ ์ด๊ณ ์ ์ด์ปค๋จธ์ค ๊ด๋ฆฌ์ ํ์ด์ง',
},
{
property: 'og:title',
content: '์ํ ๊ด๋ฆฌ ์ผํฐ | Qwik City',
},
],
};
6. ๊ณ ๊ธ ์ค๋ฌด ์ต์ ํ & ํ๋ก๋์ ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ (Performance & Deployment)
โ ๏ธ 1. Qwik ๊ฐ๋ฐ ์ ํํ ๋ฒํ๋ ์ค์์ ์ฃผ์์ฌํญ (Gotchas)
- ํด๋ก์ ๋ด ์ง๋ ฌํ ๋ถ๊ฐ(Non-serializable) ๊ฐ์ฒด ์บก์ฒ ์ฃผ์:
onClick$์ด๋useTask$๋ฑ$๊ฒฝ๊ณ ๋ด๋ถ์๋ DOM ๋ ธ๋(window,document), WebSocket ์ธ์คํด์ค, ์ ๊ท์ ๋ฑ ์ง๋ ฌํํ ์ ์๋ ๊ฐ์ฒด๋ฅผ ํด๋ก์ ๋ณ์๋ก ์ง์ ์บก์ฒํ ์ ์์ต๋๋ค.- ๋ธ๋ผ์ฐ์ ์ ์ฉ ์ ์ญ ๊ฐ์ฒด๋
$ํจ์ ๋ด๋ถ์์ ์ง์ ์ฐธ์กฐ(if (typeof window !== 'undefined'))ํ๊ฑฐ๋useVisibleTask$๋ด๋ถ์์ ์ ๊ทผํด์ผ ํฉ๋๋ค.
useVisibleTask$์ ๊ณผ๋ํ ์ฌ์ฉ ์ง์:useVisibleTask$๋ ๋ธ๋ผ์ฐ์ ๊ฐ ํ๋ฉด์ ๋์ธ ๋ ํด๋น ์คํฌ๋ฆฝํธ๋ฅผ ์ฆ์ ๋ค์ด๋ก๋ํ๊ณ ์คํํ๋ฏ๋ก, ๋จ์ฉํ๋ฉด Qwik์ ๋ณธ์ง์ธ Resumability(์ง์ฐ ๋ก๋ฉ) ์ด ๊นจ์ง๊ณ ์ผ๋ฐ React ์ฑ์ฒ๋ผ ํด๋ผ์ด์ธํธ ๋ฉ์ธ ์ค๋ ๋์ ๋ถ๋ด์ ์ค๋๋ค. ์ธ๋ถ ์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(Chart.js, D3 ๋ฑ) DOM ๋ฐ์ธ๋ฉ์ด ๋ถ๊ฐํผํ ๋๋ง ์ ํ์ ์ผ๋ก ์ฌ์ฉํ์ธ์.
- Signal ๊ฐ ๊ฐฑ์ ์
.value๋๋ฝ ๊ธ์ง:const count = useSignal(0);์์ ๊ฐ์ ๋ณ๊ฒฝํ๊ฑฐ๋ ์ฝ์ ๋๋ ํญ์count.value์์ฑ์ ์ฌ์ฉํด์ผ ๋ฐ์์ฑ ์ถ์ ์ด ๋๋ฝ๋์ง ์์ต๋๋ค.
๐ 2. ํด๋ผ์ฐ๋ ํ๋ซํผ๋ณ ํ๋ก๋์ ๋ฐฐํฌ ์ด๋ํฐ ์ถ๊ฐ
Qwik City๋ ๊ณต์ CLI ํตํฉ ๋ช ๋ น์ด๋ก ์ฃผ์ ์๋ฒ๋ฆฌ์ค ๋ฐ ์์ง ๋ฐํ์ ์ด๋ํฐ๋ฅผ ์์ฝ๊ฒ ์ถ๊ฐํ ์ ์์ต๋๋ค.
# [1] Cloudflare Pages ๋ฐฐํฌ ์ด๋ํฐ ์ถ๊ฐ
pnpm run qwik add cloudflare-pages
# [2] Vercel Edge ์ด๋ํฐ ์ถ๊ฐ
pnpm run qwik add vercel-edge
# [3] Node.js Express/Fastify ๋
๋ฆฝ ์คํํ ์๋ฒ ์ด๋ํฐ ์ถ๊ฐ
pnpm run qwik add node
# [4] Netlify Edge ์ด๋ํฐ ์ถ๊ฐ
pnpm run qwik add netlify-edge
# ๋น๋ ๋ฐ ํ๋ก๋์
๋ฒ๋ค ์์ฑ
pnpm run build
โ 3. ํ๋ก๋์ ๋ฐฐํฌ ์ ์ฒดํฌ๋ฆฌ์คํธ
| ์ ๊ฒ ํญ๋ชฉ | ๊ถ์ฅ ์ค์ ๋ฐ ํ์ธ ์ฌํญ | ๊ฒ์ฆ ๋ฐฉ๋ฒ |
|---|---|---|
| QRL ์ง๋ ฌํ ๊ฒ์ฌ | ํด๋ก์ ๋ด ์ง๋ ฌํ ๋ถ๊ฐ๋ฅํ ์ํ๊ฐ ์๋์ง ํ์ธ | pnpm run build ์ Optimizer ๊ฒฝ๊ณ ํ์ธ |
| Lighthouse ์ ์ | ์ด๊ธฐ ์๋ฐ์คํฌ๋ฆฝํธ ์คํ๋ ์ ๋ก(0ms) ๋ฐ TTI 100์ ๋ฌ์ฑ ํ์ธ | Chrome DevTools Lighthouse ์คํ |
| ์ ์ง์ ํฅ์(No-JS) | ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋นํ์ฑํ๋ ์ํ์์๋ Form๊ณผ ๋งํฌ๊ฐ ์๋ํ๋์ง ํ์ธ | ๋ธ๋ผ์ฐ์ ์ค์ ์์ JS ๋๊ณ ํผ ์ ์ถ ํ ์คํธ |
| ํ๊ฒฝ ๋ณ์ ๋ณด์ | ํด๋ผ์ด์ธํธ ๋
ธ์ถ ๋ณ์๋ PUBLIC_ ์ ๋์ฌ ์ฌ์ฉ, ๋ฏผ๊ฐ ํค๋ ์๋ฒ ์ ์ฉ ์ฌ์ฉ | .env ๋ฐ routeLoader$ ๋ด๋ถ ์ ๊ทผ ๋ถ๋ฆฌ |
| ์์ง ์ด๋ํฐ ๋ฒ๋ค๋ง | Cloudflare KV ๋๋ Vercel Edge ๋ฐํ์ ํธํ ์ฌ๋ถ ํ์ธ | pnpm run preview ๋ก์ปฌ ์๋ฎฌ๋ ์ด์
ํ
์คํธ |
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์