Vue.jsãããŒã¹ã«ãã匷åãªãã¬ãŒã ã¯ãŒã¯ãNuxtã®äžçãžããããã
1. Nuxt.jsãšã¯äœãïŒ ð€
Nuxt.jsïŒãã¯ã¹ããžã§ã€ãšã¹ïŒã¯ã人æ°ã®ããJavaScriptãã¬ãŒã ã¯ãŒã¯ Vue.js ãããŒã¹ã«ããŠæ§ç¯ãããããªãŒãã³ãœãŒã¹ã®ãã¬ãŒã ã¯ãŒã¯ã§ããVue.jsåäœã§ãçŽ æŽãããUIãæ§ç¯ã§ããŸãããNuxtã¯ãããããã«æ¡åŒµããããå¹ççã§ãã¯ãã«ãªWebã¢ããªã±ãŒã·ã§ã³éçºïŒç¹ã«ãµãŒããŒãµã€ãã¬ã³ããªã³ã°(SSR)ãéçãµã€ãçæ(SSG)ãªã©ïŒãå®çŸããããã®æ§ã ãªæ©èœãæäŸããŸãã
ç°¡åã«èšãã°ãNuxtã¯Vue.jsã䜿ã£ãéçºäœéšãåäžãããè€éãªèšå®ãå®åçãªäœæ¥ãèªååããŠããããé Œããçžæ£ãã®ãããªååšã§ããVue.jsãããŒã¹ã«ããŠãããããVue.jsã®ç¥èãããã°æ¯èŒçã¹ã ãŒãºã«åŠç¿ãå§ããããšãã§ããŸãã
2022幎11æã«ã¯ã¡ãžã£ãŒããŒãžã§ã³ã§ããNuxt 3ãæ£åŒã«ãªãªãŒã¹ãããVue 3ãViteãNitroãšãã£ãææ°æè¡ãåã蟌ã¿ãããã©ãŒãã³ã¹ãšéçºè äœéšãå€§å¹ ã«åäžããŸãããçŸåšïŒ2025幎4ææç¹ïŒã§ã¯ãNuxt 3ãäž»æµãšãªã£ãŠããã掻çºã«éçºãé²ããããŠããŸããNuxt 2ã®å ¬åŒãµããŒãã¯2024幎6ææ«ã«çµäºããŸããã
2. Nuxt.jsã®äž»ãªç¹åŸŽ âš
Nuxt.jsãå€ãã®éçºè ã«éžã°ããçç±ã¯ããã®è±å¯ãªæ©èœãšå©äŸ¿æ§ã«ãããŸããäž»ãªç¹åŸŽãèŠãŠãããŸãããã
- ãµãŒããŒãµã€ãã¬ã³ããªã³ã° (SSR) ãšéçãµã€ãçæ (SSG): Nuxtã¯SSRãšSSGãç°¡åã«å®è£ ã§ããŸããSSRã¯ãªã¯ãšã¹ãããšã«ãµãŒããŒã§HTMLãçæãããããåæ衚瀺é床ã®åäžãSEO察çã«æå©ã§ããSSGã¯ãã«ãæã«å šããŒãžã®HTMLãçæãããããCDNã§ã®é ä¿¡ã«é©ããŠãããéåžžã«é«éãªè¡šç€ºãå¯èœã§ããã¯ã©ã€ã¢ã³ããµã€ãã¬ã³ããªã³ã°(CSR)ãéžæã§ãããããã®ã¢ãŒããçµã¿åããããã€ããªããã¬ã³ããªã³ã°ãå¯èœã§ãã
- ãã¡ã€ã«ã·ã¹ãã ããŒã¹ã«ãŒãã£ã³ã°: `pages/` ãã£ã¬ã¯ããªå ã«Vueãã¡ã€ã«ïŒã³ã³ããŒãã³ãïŒãäœæããã ãã§ãNuxtãèªåçã«ã«ãŒãã£ã³ã°ãèšå®ããŠãããŸããäŸãã°ã`pages/about.vue` ãäœæãããš `/about` ãšããã«ãŒããèªåçæãããŸããããã«ãããè€éãªã«ãŒãã£ã³ã°èšå®ãã¡ã€ã«ãæåã§ç®¡çããæéãçããŸãã
- èªåã€ã³ããŒã (Auto Imports): `components/` ãã£ã¬ã¯ããªã«äœæããVueã³ã³ããŒãã³ãã `composables/` ãã£ã¬ã¯ããªã«äœæããComposition APIé¢æ°ïŒComposableïŒã¯ãæ瀺çã« `import` æãæžããªããŠãããã³ãã¬ãŒããã¹ã¯ãªããå ã§çŽæ¥äœ¿çšã§ããŸããNuxtãå¿ èŠãªã³ã³ããŒãã³ããé¢æ°ã ããèªåã§ã€ã³ããŒããããã³ãã«ãµã€ãºãæé©åããŠãããŸãã
- ããŒã¿ãã§ããã³ã°ãŠãŒãã£ãªãã£: `useFetch` ã `useAsyncData` ãšãã£ãã³ã³ããŒã¶ãã«ïŒComposableé¢æ°ïŒãæäŸããŠãããSSR/SSGç°å¢ã§ãåäœããéåæããŒã¿ååŸãç°¡åã«å®è£ ã§ããŸããããã«ãããã³ã³ããŒãã³ãã®ã»ããã¢ããããžãã¯å ã§ããŒã¿ãååŸããã¬ã³ããªã³ã°ã«å©çšã§ããŸãã
- èšå®æžã¿ã®ãã«ãããŒã« (Vite): Nuxt 3ã§ã¯ãããã©ã«ãã®ãã«ãããŒã«ãšããŠé«é㪠Vite ãæ¡çšããŠããŸããéçºæã«ã¯éåžžã«é«éãªãããã¢ãžã¥ãŒã«ãªãã¬ã€ã¹ã¡ã³ã(HMR)ãæäŸããæ¬çªãã«ãæã«ã¯æé©åãããã³ãŒããçæããŸãã
- 匷åãªã¢ãžã¥ãŒã«ã·ã¹ãã : Nuxtã¯ã¢ãžã¥ãŒã«ã«ãã£ãŠæ©èœãæ¡åŒµã§ããŸããå ¬åŒãã³ãã¥ããã£ããå€ãã®ã¢ãžã¥ãŒã«ãæäŸãããŠããïŒäŸ: ç»åæé©åãUIã©ã€ãã©ãªé£æºãèªèšŒãPWA察å¿ãªã©ïŒãããããå°å ¥ããããšã§ãè€éãªæ©èœãç°¡åã«è¿œå ã§ããŸãã
- ãµãŒããŒãšã³ãžã³ (Nitro): Nuxt 3ããå°å ¥ãããæ°ãããµãŒããŒãšã³ãžã³ãNitroãã¯ããµãŒããŒã¬ã¹ç°å¢ããšããžã³ã³ãã¥ãŒãã£ã³ã°ç°å¢ãžã®ãããã€ã容æã«ããAPIã«ãŒãã®äœæ (`server/api/` ãã£ã¬ã¯ããª) ããµãŒããŒããã«ãŠã§ã¢ã®å®è£ ãå¯èœã«ããŸããããã«ãããNuxtåäœã§ãã«ã¹ã¿ãã¯ã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ã§ããŸãã
- TypeScriptãµããŒã: èšå®äžèŠã§TypeScriptãå°å ¥ã§ããåå®å šãªéçºã匷åã«ãµããŒãããŸããèªåçæãããåå®çŸ©ã«ããããšãã£ã¿ã§ã®è£å®æ©èœãªã©ãå å®ããéçºå¹çãšã³ãŒãã®å質ãåäžããŸãã
3. Nuxt 3 ã®ãã€ã©ã€ã ð
Nuxt 3ã¯ãNuxt 2ããå€ãã®ç¹ã§é²åããŠããŸããç¹ã«æ³šç®ãã¹ãç¹ãããã€ãæããŸãã
- Vue 3 ãµããŒã: Composition APIãTeleportãSuspenseãªã©ãVue 3ã®æ°æ©èœããã«æŽ»çšã§ããŸããç¹ã«Composition APIã¯ãããžãã¯ã®åå©çšæ§ãã³ãŒãã®èŠéããå€§å¹ ã«åäžãããŸãã
- Viteã«ããé«éãªéçºäœéš: éçºãµãŒããŒã®èµ·åãHMRãéåžžã«é«éã«ãªããã¹ãã¬ã¹ã®ãªãéçºãå¯èœã§ãã
- NitroãµãŒããŒãšã³ãžã³: ãã軜éã§é«éãªãµãŒããŒãå®çŸãããããã€å ã®éžæè¢ãåºãããŸããïŒNode.jsãµãŒããŒããµãŒããŒã¬ã¹ããšããžãªã©ïŒãAPIã«ãŒãã®äœæãç°¡åã§ãã
- TypeScriptã®çµ±å匷å: ããå ç¢ãªåãµããŒããšèªååçæã«ãããTypeScriptã§ã®éçºãããã«å¿«é©ã«ãªããŸããã
- ãã³ãã«ãµã€ãºã®åæž: ããã¢ãã³ãªãã©ãŒããããšæé©åã«ãããæ¬çªç°å¢ã§ã®ã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹ãåäžããŠããŸãã
- èªåã€ã³ããŒãã®åŒ·å: ã³ã³ããŒãã³ããComposableã ãã§ãªããäžéšã®Vue APIãªã©ãèªåã€ã³ããŒãã®å¯Ÿè±¡ãšãªããèšè¿°éãåæžãããŸãã
4. Nuxt.jsãå§ãããïŒ ð ïž
Nuxt.jsãããžã§ã¯ããéå§ããã®ã¯éåžžã«ç°¡åã§ããNode.js (æšå¥šããŒãžã§ã³ 18.x 以äž) ãã€ã³ã¹ããŒã«ãããŠããç°å¢ã§ããã°ã以äžã®ã³ãã³ããå®è¡ããã ãã§ãã
# npx ã䜿ã£ãŠæ°ãã Nuxt 3 ãããžã§ã¯ããäœæ (my-nuxt-app ã¯ãããžã§ã¯ãå)
npx nuxi init my-nuxt-app
# äœæããããããžã§ã¯ããã£ã¬ã¯ããªã«ç§»å
cd my-nuxt-app
# äŸåããã±ãŒãžãã€ã³ã¹ããŒã«
npm install
# éçºãµãŒããŒãèµ·å
npm run dev
ããã§ãéçºãµãŒããŒãèµ·åãããã©ãŠã¶ã§ http://localhost:3000
ã«ã¢ã¯ã»ã¹ãããšãNuxtã®ãŠã§ã«ã«ã ããŒãžã衚瀺ãããŸãã
åºæ¬çãªãããžã§ã¯ãæ§é ð
`nuxi init` ã§çæãããåºæ¬çãªãããžã§ã¯ãæ§é ã«ã¯ã以äžã®ãããªäž»èŠãªãã¡ã€ã«ããã£ã¬ã¯ããªãå«ãŸããŸãïŒäžéšã¯å¿ èŠã«å¿ããŠæåã§äœæããŸãïŒã
ãã£ã¬ã¯ããª/ãã¡ã€ã« | 説æ |
---|---|
.nuxt/ |
éçºæããã«ãæã«Nuxtãçæãããã¡ã€ã«ãæ ŒçŽããããã£ã¬ã¯ããªïŒéåžžã¯ç·šéããªãïŒã |
.output/ |
`npm run build` ãå®è¡ããéã®ãã«ãææç©ãæ ŒçŽããããã£ã¬ã¯ããªã |
assets/ |
ãã«ãããŒã«ïŒViteãWebpackïŒã«ãã£ãŠåŠçãããã¢ã»ããïŒSass, Less, ç»å, ãã©ã³ããªã©ïŒãé 眮ãããã£ã¬ã¯ããªã |
components/ |
Vueã³ã³ããŒãã³ããé 眮ãããã£ã¬ã¯ããªãããã«é 眮ãããã³ã³ããŒãã³ãã¯èªåã€ã³ããŒããããŸãã |
composables/ |
Vue Composition APIã®é¢æ°ïŒComposableïŒãé 眮ãããã£ã¬ã¯ããªãããã«é 眮ãããé¢æ°ã¯èªåã€ã³ããŒããããŸãã |
layouts/ |
ã¢ããªã±ãŒã·ã§ã³ã®å ±éã¬ã€ã¢ãŠããå®çŸ©ããVueã³ã³ããŒãã³ããé 眮ãããã£ã¬ã¯ããªã |
middleware/ |
ããŒãžãã¬ã€ã¢ãŠããã¬ã³ããªã³ã°ãããåã«å®è¡ãããããã«ãŠã§ã¢é¢æ°ãé 眮ãããã£ã¬ã¯ããªã |
pages/ |
ã¢ããªã±ãŒã·ã§ã³ã®ãã¥ãŒãšã«ãŒãã£ã³ã°ãå®çŸ©ããVueã³ã³ããŒãã³ããé 眮ãããã£ã¬ã¯ããªããã¡ã€ã«æ§é ã«åºã¥ããŠã«ãŒãã£ã³ã°ãèªåçæãããŸãã |
plugins/ |
Vueã¢ããªã±ãŒã·ã§ã³ã®ã€ã³ã¹ã¿ã³ã¹åæã«å®è¡ããããã©ã°ã€ã³ïŒå€éšã©ã€ãã©ãªã®å°å ¥ãªã©ïŒãé 眮ãããã£ã¬ã¯ããªã |
public/ |
ãã«ãããã»ã¹ãçµãã«ããã®ãŸãŸã«ãŒããã£ã¬ã¯ããªã«é 眮ãããéçãã¡ã€ã«ãæ ŒçŽãããã£ã¬ã¯ããªïŒäŸ: `robots.txt`, `favicon.ico`ïŒã |
server/ |
ãµãŒããŒãµã€ãã®ããžãã¯ïŒAPIã«ãŒãããµãŒããŒããã«ãŠã§ã¢ãªã©ïŒãé 眮ãããã£ã¬ã¯ããªã |
app.vue |
ã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããªãŒãã€ã³ããšãªãã¡ã€ã³ã®Vueã³ã³ããŒãã³ãããã¹ãŠã®ããŒãžã³ã³ããŒãã³ããããã«æç»ãããŸãã |
nuxt.config.ts |
Nuxtã¢ããªã±ãŒã·ã§ã³ã®èšå®ãã¡ã€ã«ãã¢ãžã¥ãŒã«ã®ç»é²ãCSSã®èªã¿èŸŒã¿ãç°å¢å€æ°ã®èšå®ãªã©ãè¡ããŸãã |
package.json |
ãããžã§ã¯ãã®äŸåé¢ä¿ãã¹ã¯ãªãããå®çŸ©ãããã¡ã€ã«ã |
tsconfig.json |
TypeScriptã®èšå®ãã¡ã€ã«ãNuxtãèªåçæã»ç®¡çããŠãããŸãã |
Nuxtã¯ãããã®èŠçŽã«åºã¥ãããã£ã¬ã¯ããªæ§é ãæ¡çšããããšã§ãå€ãã®èšå®ãèªååããéçºè ãã¢ããªã±ãŒã·ã§ã³ã®ããžãã¯ã«éäžã§ããããã«ããŠããŸãã
5. ã³ã¢ã³ã³ã»ããã®è§£èª¬ âïž
Nuxtã®äž»èŠãªæ©èœïŒãã£ã¬ã¯ããªæ§é ãšé¢é£ãããã®ïŒã«ã€ããŠãããå°ã詳ããèŠãŠãããŸãããã
ãã¡ã€ã«ããŒã¹ã«ãŒãã£ã³ã° (pages/)
`pages/` ãã£ã¬ã¯ããªã« `.vue` ãã¡ã€ã«ãäœæããã ãã§ãèªåçã«å¯Ÿå¿ããã«ãŒããçæãããŸãã
pages/index.vue
â/
pages/about.vue
â/about
pages/users/index.vue
â/users
pages/users/[id].vue
â/users/:id
(åçã«ãŒã)pages/products/[...slug].vue
â/products/*
(ãã£ãããªãŒã«ã«ãŒã)
ããã«ãããã«ãŒãèšå®ãã¡ã€ã«ãæžãå¿
èŠããªããçŽæçã«ããŒãžãè¿œå ã»ç®¡çã§ããŸããããŒãžéã®ããã²ãŒã·ã§ã³ã«ã¯ `
<template>
<nav>
<NuxtLink to="/">ããŒã </NuxtLink>
<NuxtLink to="/about">ã¢ããŠã</NuxtLink>
<NuxtLink :to="`/users/${userId}`">ãŠãŒã¶ãŒããŒãž</NuxtLink>
</nav>
</template>
<script setup>
const userId = '123';
</script>
èªåã€ã³ããŒã (components/, composables/)
`components/` ã«é
眮ããã³ã³ããŒãã³ãã¯ã`.vue` ãã¡ã€ã«å
ã§çŽæ¥ã¿ã°ãšããŠäœ¿çšã§ããŸãããã¹ããããã£ã¬ã¯ããªæ§é ããµããŒããããŸãïŒäŸ: `components/common/Button.vue` 㯠`
åæ§ã«ã`composables/` ã«é 眮ãã Composition API é¢æ°ïŒäŸ: `composables/useCounter.ts`ïŒã¯ã`script setup` å 㧠`import` ãªãã« `useCounter()` ã®ããã«åŒã³åºããŸãã
<!-- pages/index.vue -->
<template>
<div>
<h1>ããŒã ããŒãž</h1>
<!-- components/TheHeader.vue ãèªåã€ã³ããŒã -->
<TheHeader />
<p>ã«ãŠã³ã¿ãŒ: {{ count }}</p>
<button @click="increment">+1</button>
</div>
</template>
<script setup>
// composables/useCounter.ts ããèªåã€ã³ããŒã
const { count, increment } = useCounter();
</script>
// composables/useCounter.ts
import { ref } from 'vue'
export const useCounter = () => {
const count = ref(0)
const increment = () => count.value++
return { count, increment }
}
ã¬ã€ã¢ãŠã (layouts/)
ã¢ããªã±ãŒã·ã§ã³å
šäœã§å
±éã®ããããŒãããã¿ãŒãªã©ãé©çšãããå Žåã`layouts/` ãã£ã¬ã¯ããªã䜿ããŸãã`layouts/default.vue` ãããã©ã«ãã®ã¬ã€ã¢ãŠããšããŠé©çšãããŸããç¹å®ã®ããŒãžã«ç°ãªãã¬ã€ã¢ãŠããé©çšããããšãå¯èœã§ããã¬ã€ã¢ãŠããã¡ã€ã«å
ã§ã¯ `
<!-- layouts/default.vue -->
<template>
<div>
<header class="navbar is-primary">
<!-- ããããŒã®å
容 -->
© 2025 ã¢ããªå
</footer>
</div>
</template>
</header>
<main class="section">
<slot /> <!-- ããã«ããŒãžã®å
容ãæ¿å
¥ããã -->
</main>
<footer class="footer">
<!-- ããã¿ãŒã®å
容 -->
ããŒã¿ãã§ããã³ã° (useFetch, useAsyncData)
ã³ã³ããŒãã³ãã® `script setup` å ã§ãµãŒããŒãµã€ã/ã¯ã©ã€ã¢ã³ããµã€ãäž¡æ¹ã§åäœããéåæããŒã¿ãååŸããããã«ã`useFetch` ã `useAsyncData` ã䜿çšããŸãããããã¯SSRæã«ãããŒã¿ãååŸããã¬ã³ããªã³ã°ãããHTMLã«å«ããããšãã§ããŸãã
<!-- pages/posts/[id].vue -->
<template>
<div v-if="pending">èªã¿èŸŒã¿äž...</div>
<div v-else-if="error">ãšã©ãŒãçºçããŸãã: {{ error.message }}</div>
<article v-else>
<h1>{{ post.title }}</h1>
<p>{{ post.body }}</p>
</article>
</template>
<script setup>
const route = useRoute();
const postId = route.params.id;
// useFetch ã䜿ã£ãŠAPIããããŒã¿ãååŸ
// 第1åŒæ°ã¯ãŠããŒã¯ãªã㌠(éåžžã¯URL)ã第2åŒæ°ã¯ãã§ããé¢æ°
const { data: post, pending, error } = await useFetch(
() => `https://jsonplaceholder.typicode.com/posts/${postId}`
);
// useAsyncData ã¯ããè€éãªéåæåŠçããã£ãã·ã¥å¶åŸ¡ãå¿
èŠãªå Žåã«äœ¿çš
// const { data: post, pending, error } = await useAsyncData(
// `post-${postId}`,
// () => $fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`)
// );
if (error.value) {
// ãšã©ãŒãã³ããªã³ã° (äŸ: ãšã©ãŒããŒãžãžãªãã€ã¬ã¯ã)
console.error('ããŒã¿ååŸãšã©ãŒ:', error.value);
}
</script>
ãµãŒã㌠API ã«ãŒã (server/api/)
`server/api/` ãã£ã¬ã¯ããªå ã«ãã¡ã€ã«ãäœæããããšã§ãAPIãšã³ããã€ã³ããç°¡åã«äœæã§ããŸãããã¡ã€ã«åããã®ãŸãŸãšã³ããã€ã³ãã®ãã¹ã«ãªããŸãïŒäŸ: `server/api/hello.ts` 㯠`/api/hello` ãšã³ããã€ã³ãã«ãªãïŒãNitroãµãŒããŒãšã³ãžã³ã«ãã£ãŠåŠçãããããã¯ãšã³ãã®ããžãã¯ãNuxtãããžã§ã¯ãå ã«å®è£ ã§ããŸãã
// server/api/hello.ts
export default defineEventHandler((event) => {
// event ãªããžã§ã¯ããããªã¯ãšã¹ãæ
å ±ïŒã¯ãšãªãã©ã¡ãŒã¿ãããã£ãªã©ïŒãååŸå¯èœ
// const query = getQuery(event);
return {
message: 'Hello from Nuxt API!'
}
})
ãã®APIã¯ã`useFetch(‘/api/hello’)` ã®ããã«ããŠããã³ããšã³ãããåŒã³åºãããšãã§ããŸãã
6. ã¬ã³ããªã³ã°ã¢ãŒããç解ãã ð¥ïž
Nuxt.jsã¯ãã¢ããªã±ãŒã·ã§ã³ã®èŠä»¶ã«å¿ããŠæ§ã ãªã¬ã³ããªã³ã°ã¢ãŒããéžæã§ããŸããããããã®ã¢ãŒãã«ã¯ã¡ãªãããšãã¡ãªããããããŸãã
ã¢ãŒã | 説æ | ã¡ãªãã | ãã¡ãªãã | èšå® (nuxt.config.ts) |
---|---|---|---|---|
ãŠãããŒãµã« (SSR) (ããã©ã«ã) |
ãªã¯ãšã¹ãããšã«ãµãŒããŒãµã€ãã§ããŒãžãã¬ã³ããªã³ã°ããHTMLãã¯ã©ã€ã¢ã³ãã«éä¿¡ãã¯ã©ã€ã¢ã³ãåŽã§JavaScriptãå®è¡ãããã€ã³ã¿ã©ã¯ãã£ãã«ãªãïŒãã€ãã¬ãŒã·ã§ã³ïŒã | ã»åæ衚瀺ãéã ã»SEOã«åŒ·ã ã»åžžã«ææ°ã®ã³ã³ãã³ãã衚瀺 |
ã»ãµãŒããŒè² è·ãããã ã»ãµãŒããŒç°å¢ãå¿ èŠ ã»ãµãŒããŒ/ã¯ã©ã€ã¢ã³ãäž¡æ¹ã§åäœããã³ãŒããæžãå¿ èŠããã |
{ ssr: true } (ããã©ã«ã) |
éçãµã€ãçæ (SSG) (ããªã¬ã³ããªã³ã°) |
ãã«ãæã«ãã¹ãŠã®ã«ãŒãã«å¯Ÿå¿ããHTMLãã¡ã€ã«ãäºåã«çæããããµãŒããŒã¯éçãã¡ã€ã«ãé ä¿¡ããã ãã | ã»éåžžã«é«é ã»ãµãŒããŒè² è·ãäœã (éçãã¹ãã£ã³ã°ã§OK) ã»SEOã«åŒ·ã ã»ãªãã©ã€ã³ã§ãåäœå¯èœ (PWAãšçµã¿åãããå Žå) |
ã»åçãªã³ã³ãã³ãã®è¡šç€ºã«ã¯å·¥å€«ãå¿
èŠ ã»ããŒãžæ°ãå€ããšãã«ãæéãé·ããªã ã»ã³ã³ãã³ãæŽæ°ããšã«åãã«ããå¿ èŠ |
`npx nuxi generate` ã³ãã³ãã§çæã{ ssr: true } (å
éšçã«SSRãå©çšããŠHTMLãçæ) |
ã¯ã©ã€ã¢ã³ããµã€ãã¬ã³ããªã³ã° (CSR/SPA) | åŸæ¥ã®Vue SPAãšåæ§ãæåã«æå°éã®HTMLãšJavaScriptãèªã¿èŸŒã¿ããã©ãŠã¶åŽã§ããŒãžå šäœãã¬ã³ããªã³ã°ããã | ã»ãµãŒããŒè² è·ãéåžžã«äœã ã»éçºãæ¯èŒç容æ (ãµãŒããŒãµã€ããæèããªã) ã»éçãã¹ãã£ã³ã°ã§OK |
ã»åæ衚瀺ãé
ã (TTI/FCP) ã»SEO察çã«è¿œå ã®å·¥å€«ãå¿ èŠ (ã¯ããŒã©ãŒãJSãå®è¡ããå¿ èŠããã) |
{ ssr: false } |
ãã€ããªããã¬ã³ããªã³ã° | ã«ãŒãããšã«ã¬ã³ããªã³ã°ã¢ãŒãïŒSSR, SSG, CSRïŒããã£ãã·ã¥æŠç¥ãæå®ã§ããã | ã»ããŒãžã®ç¹æ§ã«åãããŠæé©ãªã¢ãŒããéžæå¯èœ ã»æè»æ§ãé«ã |
ã»èšå®ãè€éã«ãªãå¯èœæ§ããã | { routeRules: { ... } } ã§ã«ãŒãããšã«èšå® |
ãšããžãµã€ãã¬ã³ããªã³ã° (ESR) | CDNã®ãšããžãµãŒããŒã§ã³ãŒããå®è¡ãããŠãŒã¶ãŒã«è¿ãå Žæã§ããŒãžãã¬ã³ããªã³ã°ããã | ã»ã°ããŒãã«ã«é«éãªå¿ç ã»SSRã®ã¡ãªããã享åãã€ã€ããªãªãžã³ãµãŒããŒã®è² è·ãè»œæž |
ã»å¯Ÿå¿ããCDNãã©ãããã©ãŒã ãå¿
èŠ ã»æ¯èŒçæ°ããæè¡ |
ãããã€ã¿ãŒã²ããã«äŸå (äŸ: Vercel Edge Functions, Cloudflare Workers) |
ããã©ã«ãã¯ãŠãããŒãµã«ã¬ã³ããªã³ã°(SSR)ã§ããã`nuxt.config.ts` 㧠`ssr: false` ãèšå®ããã°CSRã¢ãŒãã«ã`nuxi generate` ã³ãã³ãã䜿ãã°SSGã¢ãŒãã§ãã«ãã§ããŸãããã€ããªããã¬ã³ããªã³ã°ã¯ `routeRules` ãªãã·ã§ã³ã§ãã现ããå¶åŸ¡ããŸãã
7. ã¡ãªãããšãã¡ãªãã â â
Nuxt.jsãæ¡çšããããšã«ã¯å€ãã®å©ç¹ããããŸãããããã€ãã®èæ ®ç¹ãååšããŸãã
ã¡ãªãã (Advantages)
- éçºå¹çã®åäž: ãã¡ã€ã«ããŒã¹ã«ãŒãã£ã³ã°ãèªåã€ã³ããŒããèŠçŽã«åºã¥ããã£ã¬ã¯ããªæ§é ãªã©ã«ãããå®åçãªèšå®äœæ¥ãå€§å¹ ã«åæžãããéçºè ã¯æ©èœå®è£ ã«éäžã§ããŸãã
- ããã©ãŒãã³ã¹æé©å: SSR/SSGã«ããé«éãªåæ衚瀺ãèªåã³ãŒãåå²ãNitroãšã³ãžã³ã«ããæé©åãªã©ãããã©ãŒãã³ã¹åäžã®ããã®æ©èœãçµã¿èŸŒãŸããŠããŸãã
- SEOãžã®åŒ·ã: SSRãSSGã«ãããæ€çŽ¢ãšã³ãžã³ã®ã¯ããŒã©ãŒãã³ã³ãã³ãã容æã«ã€ã³ããã¯ã¹ã§ããããã«ãªããSEOã«æå©ã§ãã
- æ§é åããããããžã§ã¯ã: èŠçŽã«åºã¥ãããã£ã¬ã¯ããªæ§é ã«ããããããžã§ã¯ãã®èŠéããè¯ããªããããŒã éçºãã¡ã³ããã³ã¹ã容æã«ãªããŸãã
- æè»ãªã¬ã³ããªã³ã°ã¢ãŒã: ã¢ããªã±ãŒã·ã§ã³ã®èŠä»¶ã«åãããŠãSSR, SSG, CSR, Hybrid, ESRãšãã£ãå€æ§ãªã¬ã³ããªã³ã°æŠç¥ãéžæã§ããŸãã
- è±å¯ãªãšã³ã·ã¹ãã : å€ãã®å ¬åŒã»ã³ãã¥ããã£ã¢ãžã¥ãŒã«ãååšããæ©èœæ¡åŒµã容æã§ããVue.jsã®ãšã³ã·ã¹ãã ããã®ãŸãŸæŽ»çšã§ããŸãã
- ãã«ã¹ã¿ãã¯éçºèœå: Nitroãšã³ãžã³ãšãµãŒããŒAPIã«ãŒãã«ãããNuxtãããžã§ã¯ãå ã§ããã¯ãšã³ãAPIãæ§ç¯ã§ããŸãã
- åªããTypeScriptãµããŒã: åå®å šãªéçºã匷åã«æ¯æŽããã³ãŒãå質ãšä¿å®æ§ãé«ããŸãã
ãã¡ãªãã (Disadvantages)
- åŠç¿ã³ã¹ã: Vue.jsã®ç¥èã«å ããŠãNuxtç¬èªã®èŠçŽãæŠå¿µïŒãã£ã¬ã¯ããªæ§é ãã©ã€ããµã€ã¯ã«ãããŒã¿ãã§ããã³ã°æ¹æ³ãªã©ïŒãåŠã¶å¿ èŠããããŸããç¹ã«SSR/SSGã®æŠå¿µã¯åå¿è ã«ã¯å°ãé£ããæãããããããŸããã
- èŠçŽãžã®äŸå: Nuxtã¯ãèŠçŽå€§èšå®ã(Convention over Configuration) ã®ææ³ã§äœãããŠããããããã®èŠçŽã«åŸãããšãæšå¥šãããŸããèŠçŽããå€ããã«ã¹ã¿ãã€ãºãè¡ãããšãããšãããã£ãŠè€éã«ãªãå ŽåããããŸãã
- ãã¬ãŒã ã¯ãŒã¯ã®æœè±¡å: Nuxtãå€ãã®ããšãèªåã§è¡ã£ãŠãããåé¢ãå éšã§äœãèµ·ãã£ãŠããã®ããå®å šã«ç解ããã®ãé£ãããªãããšããããŸããåé¡çºçæã®ãããã°ãè€éã«ãªãå¯èœæ§ããããŸãã
- ãããžã§ã¯ãã®è€éæ§: å°èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³ãåçŽãªUIã³ã³ããŒãã³ãã ããå¿ èŠãªå ŽåãNuxtã®æ©èœã¯éå°ãããããŸããããã®å Žåã¯ãVue.jsåäœããã軜éãªãœãªã¥ãŒã·ã§ã³ã®æ¹ãé©ããŠããå¯èœæ§ããããŸãã
- Nuxtã®ã¢ããããŒãäŸå: ãã¬ãŒã ã¯ãŒã¯ã§ãããããNuxtèªäœã®ããŒãžã§ã³ã¢ãããä»æ§å€æŽã«è¿œåŸããŠããå¿ èŠããããŸãã
8. ãŸãšã ð
Nuxt.jsã¯ãVue.jsãããŒã¹ã«ãã匷åã§æè»ãªWebã¢ããªã±ãŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯ã§ãããµãŒããŒãµã€ãã¬ã³ããªã³ã°ãéçãµã€ãçæããã¡ã€ã«ããŒã¹ã«ãŒãã£ã³ã°ãèªåã€ã³ããŒããšãã£ãå€ãã®äŸ¿å©ãªæ©èœãæäŸããéçºè ã®çç£æ§ãå€§å¹ ã«åäžãããŸãã
ç¹ã«Nuxt 3ã§ã¯ãVue 3ãViteãNitroãšãã£ãææ°æè¡ã®æ¡çšã«ãããããã©ãŒãã³ã¹ãšéçºè äœéšãããã«åäžãããã«ã¹ã¿ãã¯éçºãèŠéã«å ¥ããã¢ãã³ãªWebéçºãå®çŸã§ããŸãã
åŠç¿ã³ã¹ããèŠçŽãžã®äŸåãšãã£ãåŽé¢ããããŸããããã®ã¡ãªããã¯éåžžã«å€§ãããå€ãã®ãããžã§ã¯ãã§æ¡çšãããŠããŸããããããªããVue.jsã䜿ã£ãéçºã次ã®ã¬ãã«ã«é²ããããšèããŠãããªããNuxt.jsã¯ééããªãè©ŠããŠã¿ã䟡å€ã®ãããã¬ãŒã ã¯ãŒã¯ã§ãã
ãã²ãNuxtå ¬åŒãµã€ãã蚪ããŠãå®éã«ãããžã§ã¯ããäœæãããã®ãã¯ãã«ãªæ©èœãäœéšããŠã¿ãŠãã ããïŒð
ã³ã¡ã³ã