対象èªè
- Next.jsã®è¿å¹´ã®æ¹åç¹ã«ã¤ãã¦ç¥ãããæ¹
åæç°å¢
ãçè ã®æ¤è¨¼ç°å¢ã¯ä»¥ä¸ã®éãã§ãã
- macOS Sequoia 15.1
- Node.js 23.2.0
- npm 10.9.0
- Next.js 15.0.4-canary.13
Next.js 15ãªãªã¼ã¹ä»¥åã®å¤æ´ãæ¯ãè¿ã
ã2024å¹´10æ21æ¥ã«Next.js 15ããªãªã¼ã¹ããã¾ãããNext.js 13.4ã®æ°æ©è½ãç´¹ä»ããé£è¼ãNext.jsã®æ°ããæ¦å¿µãå¦ã¶ãã®å¾ãNext.jsã¯é²åãç¶ããApp Routerã®æ©è½ãæ´ã«å¼·åããã¦ãã¾ãã
ãæ¬é£è¼ã§ã¯ããã¼ã¸ã§ã³15ã§Next.jsãã©ããã£ã使ãåæã«ãªã£ãã®ããããããã©ããªã£ã¦ããã®ãã解説ãã¾ããã¾ãä»åã¯ãåè¿°ã®é£è¼ã®ç¶ãã¨ãã¦ãNext.js 14ã¾ã§ã«ç»å ´ããæ©è½ãæ¯ãè¿ã£ã¦ããã¾ãããµã³ãã«ã³ã¼ãã«ã¯Next.js 15ãå©ç¨ããã®ã§ã14ã¨15ã§APIãå¤ãã£ã¦ããå ´åã¯ã15ã§ã®APIãæ¡ç¨ãã¾ãã
ãNext.js 14ã®å¤§ããªç®çã¯ãNext.js 13.4ã§ã¢ã«ãã¡çã¨ãã¦æä¾ããã¦ããServer Actionsã®å®å®çã®ãªãªã¼ã¹ã¨ãæ°ãã«ãã¬ãã¥ã¼çã¨ãã¦è¿½å ãããPartial Prerenderingã§ããããããããApp Routerã®æ©è½ãå¼·åããå¼·åãªä»çµã¿ã§ããããããè¦ã¦ããã¾ãããã
Server Actions
ãServer Actionsã¯Next.js 13.4ã®è¨äºã§åãä¸ããã¨ãã¨APIã¯ã»ã¨ãã©å¤ãã£ã¦ããããNext.js 15ã§ããã®ã¾ã¾å©ç¨ã§ãã¾ãããã®ãããæ¬è¨äºã§ã¯ç°¡åã«èãæ¹ã解説ããã®ã¿ã«ã¨ã©ãã¾ããServer Actionsã®åºæ¬çãªä½¿ãæ¹ã¯æ¬¡ã®éãã§ãã
- ãµã¼ãã¼ãµã¤ãã§ãã¼ã¿ãä½æã»æ´æ°ããããã®é¢æ°ï¼Server Actionï¼ãå®ç¾©ãã
-
ãã®é¢æ°ã
form
è¦ç´ ã®action
å±æ§ã«æå®ãã - ãã©ã¼ã ã®éä¿¡æä½ã«ããããµã¼ãã¼ãµã¤ãã®é¢æ°ãå®è¡ããã
ãServer Actionãå®ç¾©ããæ¹æ³ã¯2種é¡ãããServer Componentså ã«ç´æ¥è¨è¿°ããæ¹æ³ã¨ãå¤é¨ãã¡ã¤ã«ã«è¨è¿°ãã¦ã¤ã³ãã¼ãããæ¹æ³ãããã¾ãããªã¹ã1ã¯ãServer Componentså ã«Server Actionãè¨è¿°ããä¾ã§ãã
// app/server-actions/page.js export default function Page() { // (1) Server Action async function create() { 'use server' // Web APIã®å¼ã³åºãããã¼ã¿ãã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãªã©ãè¡ã } return ( <form action={create}>{/* (2) */} <button type="submit">Create</button> </form> ) }
ãï¼1ï¼ã¯Server Actionã§ããé¢æ°ã®å®ç¾©ã§ããé¢æ°å
ã§use server
ãã£ã¬ã¯ãã£ããè¨è¿°ãããã¨ã§ããã®é¢æ°ããµã¼ãã¼ãµã¤ãã§å®è¡ããããã¨ã示ãã¾ããï¼2ï¼ã¯ããã®Server Actionãå®è¡ããããã®ãã©ã¼ã ã§ãããã©ã¼ã ã®action
å±æ§ã«Server Actionãæå®ãããã¨ã§ããã©ã¼ã ã®éä¿¡æä½ãè¡ã£ãéã«Server Actionããµã¼ãã¼ãµã¤ãã§å®è¡ããã¾ãã
ãuse server
ãã£ã¬ã¯ãã£ãããã¡ã¤ã«åä½ã§æå®ãã¦ããã®ãã¡ã¤ã«å
ã®é¢æ°ãServer Actionã¨ãã¦å©ç¨ãããã¨ãã§ãã¾ãããã®å ´åãé¢æ°å
ã§use server
ãã£ã¬ã¯ãã£ããæå®ããå¿
è¦ã¯ããã¾ããããªã¹ã2ã¯ãServer Actionãå¤é¨ãã¡ã¤ã«ã«è¨è¿°ãã¦ã¤ã³ãã¼ãããä¾ã§ãã
// app/server-actions/update.js "use server" // (1) // (2) export async function update() { // Web APIã®å¼ã³åºãããã¼ã¿ãã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãªã©ãè¡ã }
ããã¡ã¤ã«ã®æä¸é¨ã§ï¼1ï¼ã®ããã«use server
ãã£ã¬ã¯ãã£ããæå®ãããã¨ã§ãï¼2ï¼ã®é¢æ°ã«ã¯ãã£ã¬ã¯ãã£ãæå®ãä¸è¦ã«ãªã£ã¦ãã¾ããããã¯ããã¼ã¸ãããªã¹ã3ã®ããã«ã¤ã³ãã¼ããã¦å©ç¨ãã¾ãã
// app/server-actions/page.js import { update } from "./update" // (1) export default function Page() { return ( <form action={update}>{/* (2) */} <button type="submit">Update</button> </form> ) }
ãï¼1ï¼ã§Server Actionãã¤ã³ãã¼ãããï¼2ï¼ã§ãã©ã¼ã ã®action
å±æ§ã«æå®ãã¦ãã¾ãããã®ããã«ãServer Actionãå¤é¨ãã¡ã¤ã«ã«è¨è¿°ãã¦ã¤ã³ãã¼ããããã¨ã§ãã³ã¼ãã®åå©ç¨æ§ãé«ãããã¨ãã§ãã¾ãã
ãç´°ãã使ãæ¹ã«ã¤ãã¦ã¯ãNext.js 13.4ã®è¨äºã§è©³ãã解説ãã¦ããã®ã§ããã¡ããåç §ãã¦ãã ããã