Vue.js ã®ãã¬ãŒã ã¯ãŒã¯ã§ãã Nuxt.js ã® v3 ãã2021/10/12 ã«ãããªãã¯ããŒã¿çã«ç§»è¡ããŸãã ð
Monstarlab ã§ã®ãããžã§ã¯ãã®å€ã㯠React.js ã§éçºãããããšãå€ãã§ãããå人çã«ã¯äžçªåãã«è§Šãã Web Frontend JavaScript ãã¬ãŒã ã¯ãŒã¯ã Vue.js ã ã£ãããšããããNuxt.js ã v1 ãªãªãŒã¹çŽåŸãã䜿ã£ãŠããŸããã
ãããªæãå
¥ãããããã¬ãŒã ã¯ãŒã¯ã§ã瀟å
ã§ã Vue/Nuxt ã奜ããšå
¬èšããŠããŸãããæè¿ã¯è§ŠããããšãããŸããªããªã£ãŠããŸãã»ã»ã»ããæ©äŒãªã®ã§ãä»å㯠Nuxt3 ã«ã€ããŠå€æŽç¹ãæ¹åç¹ããŸãšããããšæããŸãã
Nuxt.js ã«ã€ããŠ
ãã®ããã°ãèŠãŠããã»ãšãã©ã®æ¹ã¯ãã§ã« Nuxt.js ã«ã€ããŠç¥èããããããããŸãããããŸã㯠Nuxt.js ã«ã€ããŠç°¡åã«èª¬æããŸãã
Nuxt.js ã¯ãJavaScript ãã¬ãŒã ã¯ãŒã¯ã§ãã Vue.js ã® Web ã¢ããªã±ãŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯ã§ãVercel ãéçºãã Next.js ã«åœ±é¿ãåããŠéçºãããŸãããåºæ¬çã«ã¯ Vue.js ã§éçºããããã«å¿ èŠãªãšã³ã·ã¹ãã ãéçºã«å¿ èŠãªèŠçŽãç°¡åã«å°å ¥ã§ããããšã«å ããSSR/SSG/SPA ããããã®ã¬ã³ããªã³ã°ã¢ãŒã㧠Build å¯èœã§ãããå人çã«ã¯ Vue.js ã§éçºããã®ã§ããã° Nuxt.js ã䜿çšããããšãäžçªã«èãããããè¯ãã§ãããã¬ãŒã ã¯ãŒã¯ã ãšæããŸãã
äžè¬çã«å€§èŠæš¡éçºã« Vue.js ã¯åããªãããã»ã»ã»ãšãã話ã Web Frontend çéã§ã¯ããèããŸãã Nuxt.js ã®èŠçŽãããšã«éçºããããšã§ãèŠæš¡ã倧ãããããžã§ã¯ãã§ãåå䜿çšã§ãããšæã£ãŠããŸãã倧èŠæš¡ã®å Žå Vue.js ã ãšããã©ãŒãã³ã¹ãã»ã»ã»ãªã©ã®èšäºãèŠãŸããããã¬ãŒã ã¯ãŒã¯ã«ããããã©ãŒãã³ã¹ã®åªå£ãæããããšã¯ãããŸã§å€ããããŸãããïŒãããŸã§å人çèŠè§£ã§ãïŒ
React.js ã Vue.js ãéçºãã©ãã©ãé²ã¿ãããéããã䟿å©ã«ãªã£ãŠããŸãã®ã§ãã©ã¡ãããããè¯ããšããããšã¯ãªããããããŸãããMonstarlab ã§ã¯ React ãå€ã䜿ãããŠãããšåé ã§èšèŒããŸãããããã㯠TypeScript ã Web frontend éçºã«ãããŠã¹ã¿ã³ããŒããšèšã£ãŠè¯ãç¶æ³ã®äžã§ãVue.jsïŒTypeScript ã®èª²é¡ãå€ããã£ãããšãäžçªã®çç±ãªã®ããªãšæããŸãã
ä»åã® Nuxt3(Vue3) 㯠TypeScript ã®ãã€ãã£ããµããŒãããããŠãããä»åŸã¯ä»ãŸã§ããã Nuxt.js ãéžæããå¯èœæ§ãããã®ã§ã¯ïŒãšæåŸ ããŠããŸãã
Nuxt3 ã®äž»ãªæ°æ©èœ/å€æŽç¹
Nuxt2 â Nuxt3 ã¯å€§éšåãæ°ãã«èšèšãéçºãããããã§å€ãã®æ°æ©èœãå€æŽç¹ããããŸãããªããžããªãæ°ããäœãããŠããŸããã
Nuxt2 https://github.com/nuxt/nuxt.js
Nuxt3 https://github.com/nuxt/framework
äž»ãªæ°æ©èœã¯ https://v3.nuxtjs.org/ ã«èšèŒãããŠããŸãã
äžèšä»¥å€ã«ã现ããªéšåå«ãå€ãã®å€æŽãããããã§ããããã®äžã§ãå人çã«å€§ããªå€æŽç¹ã¯ä»¥äžã§ãã
- Vue3 ã®ãµããŒã
- TypeScript ã®ãã€ãã£ããµããŒã
- Nitro Engine
Vue3 ã®ãµããŒã
2021 幎 9 æã«ãªãªãŒã¹ããã Vue3 ãæ¡çšããŠãããCompositionAPI ã Teleportã <script setup>
ãVite ãªã©ãVue3 ã§æ°ããè¿œå ãããæ©èœããã§ã«ãµããŒãããŠããŸãããã ããã®ä»£ãã Vue2 ã¯ãµããŒãããŠããªããããNuxt2(Vue2)ããã®ç§»è¡ã«ã¯ Nuxt Bridge ãçšæãããŠããŸããäžéšã®æ©èœã¯äœ¿ããŸããã Nuxt3 ã§è¿œå ãããå€ãã®æ©èœã¯ Nuxt Bridge ã䜿çšããããšã§äœéšã§ãããã§ãã
https://v3.nuxtjs.org/getting-started/introduction#comparison
ãŸãã¯æ¢åã® Nuxt2 ãããžã§ã¯ãã« Nuxt Bridge ãé©çšããããšããNuxt3 ã«ããããã©ãŒãã³ã¹ã®æ¹åãæããããšãã§ããäžçªã®æ¹æ³ãããããŸããã
TypeScript ã®ãã€ãã£ããµããŒã
Nuxt3 ã Vue3 ã JavaScript ãã TypeScript ãžäžæ°ãããComposition API ãžã®å€æŽããã TypeScript ã®æ©æµãã¹ãã¬ã¹ãªãåããããšãã§ããããã«ãªããŸãããå人çã«ã¯ãããäžçªããã£ãããšããããã£ãšå¯Ÿå¿ããã»ã»ã»ãšããæ°æã¡ã§ããæè¿ React ã°ãã䜿ã£ãŠããäžã§ããã£ãšãã㧠Nuxt ãæ¬æ Œçã«äœ¿ããããïŒãšæåŸ ããŠããŸããïŒãŸã ãããªãã¯ããŒã¿ãªã®ã§ããå°ãæéã¯ããããããããŸããïŒ
Nitro Engine
ãããããã®æ°æ©èœãäžçªè©±é¡ã«ãªã£ãŠããããã«æããŸããNuxt3 ã®ãµã€ãã§ããããããŒãžã§å€§ããåãäžããŠããŸããã軜éã§é«éã«åäœããNuxt2 ã§ã¯ãªãã£ãæ©èœãæ°ãã«è¿œå ãããŠããŸãããŸã ããŒã«ã«ã§ããã€ãã®æ©èœã確èªããã¬ãã«ã§ãããåäœã«ã€ããŠã¯ Nuxt2 ãããé«éã« Server ãç«ã¡äžãããŸãããéçºã§ã¯ããŒã«ã«ç°å¢ã§ã®ããã©ãŒãã³ã¹ãéèŠã«ãªããŸãã®ã§ãããã¹ãã¬ã¹ãæããªããªã£ãŠå¹çãäžããã®ã§ã¯ãªãããšæããŸããïŒVite ã Webpack5 ãžã®ç§»è¡ã圱é¿ããŠãããšæããŸãïŒ
Nitro Engine ã«ã€ããŠã¯ç§ããŸã ç解ã§ããŠãªãéšåãå€ãã®ã§ãããäž»ãªæ©èœã 2 ã€çŽ¹ä»ããŸãã
ã¯ãã¹ãã©ãããã©ãŒã ã®ãµããŒã
ãããã SSR(Server Side Rendering)ãããããã«ãNuxt2 ã§ã¯ Node.js ãå¿
èŠã§ããããNitro Engine ã®æ¡çšã«ããè€æ°ã®ç°å¢ã§ã®å®è¡ãå¯èœã«ãªããŸããããã®æ©èœã¯ããªãé©ãã§ãWeb Frontend ã®éçºãä»ãŸã§ãšã¯éãéžæè¢ãæã€å¯èœæ§ããããšæããŸãããæ
å ±ããŸã å°ãªãå®ç§ã«ã¯ç解ã§ããŠããªãã®ã§ãééã£ãéšåããããããããŸããããäºæ¿ãã ãã ð
Document å
ã§ã¯ä»¥äžã®ãããªç°å¢ãèšèŒãããŠããŸããã
- Node.js
- Browser
- service-workers
- serverless
å ·äœçã«ã¯ Nuxt3 ã® Docs ã® Deployment ã«ããã€ãã®ãµãŒãã¹ã§ã®ãããã€æ¹æ³ã«ã€ããŠèª¬æãããŠããŸãã
- Azure (Azure Functions)
- Cloudflare Workers
- Firebase Hosting
- Netlify
- PM2
- Vercel
- Presets
- Node.js server (default)
- Node.js function
- Lambda function
- Service worker
èšå®æ¹æ³ã¯ãšãŠãç°¡åã§ãnuxt.config.ts
ã« Nitro Engin ã®ã¿ãŒã²ãããèšå®ããã ãã®ããã§ãã
export default {
nitro: {
preset: 'worker' // service-worker
}
}
ãã ããããŒã«ã«ç°å¢ã§ Build ãäžæãè¡ãã確èªããããšã¯ã§ããŸããã§ãã ð
ä»ãŸã§ã¯ Nuxt ããããã€ããã«ããã£ãŠãNode.js ã䜿çšã§ãããµãŒããŒã§ SSRãCloudFront+S3 ã Netlify ãªã©ã§ Hosting ããå Žå㯠SSG ã§éçãã¡ã€ã«ãçæããäžã§ãªãªãŒã¹ããšããéžæè¢ããŸãèããŠããŸããã
ä»åã®å€æŽã«ãããNode.js ã ãã§ãªã CDN ã® Edge server ã Netlify ã®ãã㪠Hosting ãµãŒãã¹ããã©ãŠã¶ã® Service worker ã§ãµãŒããŒãåäœããããã§ããäžèšã®ãããªæ§ã
ãªç°å¢ã§ãµãŒããŒãèµ·åã§ãããšããããšã¯ãä»ãŸã§ã®éžæè¢ã ãã§ãªã Serverless + JAMstack ã®ãããªæ°ããªéžæè¢ãããç°¡åã«äœ¿çšã§ããäºã«ãªãããã§ãã
Github ã«ã¯ Nitro Engin ã®ãã¢ãªããžããªããããããã€ãã®ãµãŒãã¹ã« Nitro Engin ã䜿çšãã Nuxt ã§ããµã€ãããããã€ãããŠããŸããã
Github: nuxt/nitro-demo
Nitro Project Demo: Netlify
Vercel, Cloudflare ãªã©ä»ã®ãã¢ãµã€ãããããŸããããå šãŠ SSR ãäžæãåããŠããããã§ã¯ãªãäžã«ã¯ 404 ã®ããŒãžããã£ãã®ã§ãçŸæç¹ã§ã¯ãªããšãèšããŸãããã»ã»ã»Netlify 㧠SSR ãã§ããŠããããšã¯ãããã§ããã
API ã«ãŒãã®ãµããŒã
Nuxt ãããžã§ã¯ãã®äžã§ API ãšã³ããã€ã³ããäœæã§ããããã«ãªããŸããããã®æ©èœã¯ Next.js ãªã©ã§ã¯ãã§ã«å®è£
ãããŠããã®ã§éŠŽæã¿ãããæ¹ãããããç¥ããŸããã
server/api
ãã£ã¬ã¯ããªã« xxx.ts|js
ãã¡ã€ã«ãäœæãããšãã¯ã©ã€ã¢ã³ãåŽããåŒã³åºãããšãã§ãããšã³ããã€ã³ããšããŠæäŸããã/api/xxx
ãšãã圢ã§åŒã³åºããŸãããŸããNuxt3 ã§è¿œå ããã useFetch()
useAsyncData()
ã§ãšã³ããã€ã³ããåŒã³åºããå Žåãèªåçã« type ãã¡ã€ã«ãçæããã¬ã¹ãã³ã¹ã«å¯ŸããŠåãå®çŸ©ãããŸãã
äŸãã°ä»¥äžã®ãããªãšã³ããã€ã³ããäœæãã
// server/api/test.ts
import type { IncomingMessage, ServerResponse } from 'http'
let count = 0
export default async (req: IncomingMessage, res: ServerResponse) => {
return {
Hello: "Monstarlab",
count: ++counter,
}
}
yarn dev
ããã㯠yarn build
ãå®è¡ããå Žåã.nuxt
ãã£ã¬ã¯ããªã«ä»¥äžã®å®çŸ©ãèªåçã«äœæãããŸãã
// .nuxt/nitro.d.ts
// Generated by nitro
declare module '@nuxt/nitro' {
type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T
interface InternalApi {
'/api/test': Awaited<ReturnType<typeof import('../server/api/test').default>>
}
}
export {}
çµæã以äžã®ããã« Pick åã§å®çŸ©ãããŸããã
const response: Ref<Pick<{
Hello: string;
count: number;
}, "Hello" | "count">>
ãã¡ãããå€éšã® API ã server/api/xxx
ã§åŒã³åºããå Žåã«ã¯ any åã«ãªã£ãŠããŸãã®ã§åå®çŸ©ãå¿
èŠã§ãããéçºããéã«ããªã䟿å©ãªæ©èœã§ã¯ãªãã§ããããã
æåŸã«
ä»ã«ããã£ã¬ã¯ããªã®æ§é ãå€æŽãããããCLI ãæ°ãããªã£ãããšæ§ã
ãªå€æŽç¹ããããŸãããç§ãæãã倧ããªå€æŽç¹ãšããŠã¯ä»¥äžãšãªããŸããïŒã¡ãªã¿ã« IE ã¯ãµããŒãããŠããŸãã ðïŒ
ãŸã ããŒã¿çãšããããšã§ãããã«ãããžã§ã¯ãã§äœ¿çšã§ãããšã¯æã£ãŠããŸããããä»åŸã® Web Frontend éçºã®éžæè¢ã®äžã€ãšããŠååã«æåŸ
ã§ãããã¬ãŒã ã¯ãŒã¯ã ãšæããäºãã§ããNuxt(Vue)ãã¡ã³ãšããŠã¯ãšãŠãå¬ããã§ã ð¥³
Article Photo by NuxtJS