masanos note

🔧Use MicroCMS with Nuxt3

2022-11-17
icon

MicroCMS

MicroCMS is a headless CMS, developed in Japan.

install

npm install microcms-js-sdk

package.json

{
  "devDependencies": {
    "nuxt": "3.0.0-rc.8"
  },
  "dependencies": {
    "microcms-js-sdk": "^2.2.1"
  }
}

.env

* is described on the MicroCMS screen.

MICROCMS_API_KEY=*
MICROCMS_SERVICE_DOMAIN=*

nuxt.config.json

const { MICROCMS_SERVICE_DOMAIN, MICROCMS_API_KEY } = process.env;

export default defineNuxtConfig({
  publicRuntimeConfig: {
    apiKey: "prerender" !== 'production' ? MICROCMS_API_KEY : undefined,
    serviceDomain: "prerender" !== 'production' ? MICROCMS_SERVICE_DOMAIN : undefined
  }
})

server/api/client.ts

import { createClient } from 'microcms-js-sdk';
const { MICROCMS_API_KEY, MICROCMS_SERVICE_DOMAIN } = process.env;

const client = createClient({
  serviceDomain: MICROCMS_SERVICE_DOMAIN,
  apiKey: MICROCMS_API_KEY,
});

export default client

server/api/type.ts

type Tag = {
  id: string,
  createdAt: string,
  updatedAt: string,
  publishedAt: string,
  revisedAt: string,
  name: string
}

export type Blog = {
  id: string,
  createdAt: string,
  updatedAt: string,
  publishedAt: string,
  revisedAt: string,
  title: string,
  tags: Tag[],
  description: string
  content: string,
}

server/api/blogs.ts

import type { IncomingMessage, ServerResponse } from 'http'
import client from './client'
import { Blog } from './types'

export default async (req: IncomingMessage, res: ServerResponse) => {
  const queries = {}
  const data = client.getList<Blog>({
    endpoint: 'blogs',
    queries: queries
  })

  return data
}

pages/index.vue

<script setup lang="ts">
const { data } = await useFetch('/api/blogs')
</script>

<template>
  <div class="p-5 text-site">
    {{ data }}
  </div>
</template>

server/api/blog.ts

import type { IncomingMessage, ServerResponse } from 'http'
import client from './client'
import { Blog } from './types'
import * as url from 'url'


export default async (req: IncomingMessage, res: ServerResponse) => {
  const params = url.parse(req.url as string, true).query;
  const slug = params.slug
  const data = client.getListDetail<Blog>({
    endpoint: 'blogs',
    contentId: String(slug)
  })
  return data
}

pages/[slug]/index.vue

<script setup lang="ts">
const route = useRoute()
const slug: string | string[] = route.params.slug;

const { data } = await useFetch(`/api/blog`, {
  params: { slug: String(slug) }
})
</script>


<template>
  <div>
    {{ slug }}
    {{ data }}
  </div>
</template>

If you want to see the communication on the frontend side

<script setup lang="ts">
const route = useRoute()
const slug: string | string[] = route.params.slug;
const { data } = await useFetch(`/api/blog`, {
  params: { slug: String(slug) }
})

fetch(`https://${microcms_name}.microcms.io/api/v1/blogs/${slug}`, {
  headers: {
    "X-MICROCMS-API-KEY": ${your api key}
  }
})
</script>

<template>
  <div>
    {{ data }}
  </div>
</template>

Ref.

https://blog.microcms.io/nuxt3-create-blog/

Related Notessupabase with Vue3 Vue3 with bootstrap-icons[vue3]Install bootstrap5[vue3] install[Nuxt3]The first thing I do when launching a nuxt3 project. (^3.5.2)[Nuxt3][Bootstrap]Use Bootstrap icons with Nuxt3.[Node.js] Storing API results in js🔧[GA4][GTM]Configure GA4 in GTM🔧[Nuxt3]Using Google Tag manager with NUXT3🔧[GA4][BigQuery]Linking GA4 and BigQuery🔧When you want to scrape a SPA site, PhantomJsCloud is solution.🐛Error brew -v | update-reset🔧Use Google Spreadsheet as API with Nuxt3.🔧 Get json from a spreadsheet using GoogleSheetsAPI v4.🔧[Python]Install Python to Mac[Nuxt3]Install stable version of Nuxt 3.0.0. | npx nuxi init nuxt3-appMake Ranking with MySQLwatch & v-model | Vue3 (Nuxt3)window & document | Nuxt3Using custom domain, Hosting to GitHub Pages with Nuxt3GA4 with Nuxt3📝MySQL - Date Function - Tips Use Nuxt3 props🔧Using GoogleFont with Nuxt3📝Error - Deprecation Warning: $weight: Passing a number without unit % (100) is deprecated. - Bootstrap5 📝using sass with nuxt📝Firebase9 Google Authentication with Nuxt3.📝Set favicon in Nuxt3📝Use bootstrap5 with Nuxt3🐛Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement[Nuxt3] How to separate source directoriesmicroCMS & GitHub Actions & Nuxt3Using highlight in Nuxt3.use package.json value🔧frontmatter-markdown-loader & highlight.js🔧Install Font Awesome on Nuxt2 via npm.Github pages with GitHub ActionsCannot find module '~/*/*.vue' or its corresponding type declarations.Vetur(2307)🐛Cannot find module. Consider using '--resolveJsonModule' to import module with '.json' extension.ts(2732)TypeScript Object.🔧Bootstrap5 with Nuxt2processmd with Nuxt2🔧[MySQL]Install MySQL Workbench🔧Convert Markdown to HTML. convert frontmatter to json🔧Install homebrew, nvm, node to Mac🔧[MySQL]Record of installing and starting mysql with homebrew.🎨 Display the photo full screen and overlay the header and footer on top.🔧Set git repository to created project.[Nuxt3] Make Header & Footer
A record of the development is left in a web note.
Masanos
I want to make the world I see happy. Little by little, I am preparing to start a business. Thank you for your support.
Buy Me A Coffee
Copyright© masanos All Rights Reserved.