use cache 경계

use cache boundary

Next.js의 use cache 지시어로 함수나 컴포넌트의 결과를 캐시 범위로 지정합니다.

···
html
<section class="stage"><div class="heading">USE CACHE <span class="hint">same input, reused result</span></div><div class="pair"><div class="tile list"><b>PRODUCTS</b><span>A · B · C</span><small>cache hit</small></div><div class="tile cart"><b>CART</b><span id="cart-count">0 items</span><small>new request</small></div></div><div class="caption">One boundary is stable while another changes</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)}.pair{display:flex;flex:1;align-items:stretch;justify-content:center;gap:clamp(8px,3vmin,24px);min-height:75px}.pair .tile{width:42%;max-width:220px;display:flex;flex-direction:column;justify-content:space-around}.pair b{color:var(--accent);font-size:.85em}.pair small{color:var(--muted);font:600 .72em ui-monospace,monospace}.list{border-color:var(--accent)!important}.list::after{content:"";display:block;height:4px;background:var(--accent);border-radius:3px}.cart{border-color:var(--accent-3)!important}.stage[data-step="1"] .cart,.stage[data-step="2"] .cart{background:color-mix(in srgb,var(--accent-3) 18%,var(--surface))}.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"),count=document.getElementById("cart-count");let step=0;function tick(){stage.dataset.step=String(step);count.textContent=step+" items";step=(step+1)%3}tick();let timer=setInterval(tick,900);stage.addEventListener("pointerdown",()=>{clearInterval(timer);tick()})

use cache는 Next.js의 함수 또는 컴포넌트 안에서 캐시 가능한 계산을 선언합니다. 캐시 키는 함수와 직렬화 가능한 입력 등에서 생성되므로 같은 입력을 재사용할 수 있습니다.

인증 정보나 요청마다 달라지는 값은 캐시 경계 밖에서 다루거나 명시적인 입력으로 설계해야 합니다. 데모는 같은 상품 목록이 재사용되는 동안 장바구니가 새로 그려지는 장면입니다.

언제 쓰나

비싼 조회나 렌더 결과가 여러 요청에서 같을 때 좁은 캐시 경계를 설정합니다.

페이지로 열기 ↗