0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jamstack Docker + Nuxt + Contentful + Vuetify + Netfliyでポートフォリオ作成②

Posted at

前回の続きです。
https://qiita.com/knitbow/items/89478226da7bd365b9cd

やりたいこと

  • Nuxtでフロントを作成
  • Dockerで環境構築
  • Contentfulでポートフォリオ情報の取得
  • Vuetifyで見た目整形
  • Netfliyで公開

NuxtとContentfulの連携部分

  • Contentfulにて、SpaceIDとトークンの確認
    Setting - API kyes
    image.png
    Add API Keyをクリック

Space IDとContent Delivery API - access tokenを確認

.envの作成

.env

CTF_SPACE_ID=ContentfulのspaceID
CTF_CDA_ACCESS_TOKEN=Contentfulのaccess token

環境ファイルはgit保存しないように、ignore設定

lib/config.js

require('dotenv').config()

function getValidConfig(configEnv, keys) {
  let { config, missingKeys } = keys.reduce(
    (acc, key) => {
      if (!configEnv[key]) {
        acc.missingKeys.push(key)
      } else {
        acc.config[key] = configEnv[key]
      }
      return acc
    },
    { config: {}, missingKeys: [] }
  )

  if (missingKeys.length) {
    throw new Error(`Contentful key is missing : ${missingKeys.join(', ')}`)
  }
  return config
}

module.exports = {
  getConfigForKeys(keys) {
    const configEnv = {
      CTF_SPACE_ID: process.env.CTF_SPACE_ID,
      CTF_CDA_ACCESS_TOKEN: process.env.CTF_CDA_ACCESS_TOKEN
    }
    return getValidConfig(configEnv, keys)
  }
}

キー管理のConfigを用意

plugins/contentful.js

const contentful = require('contentful')
const defaultConfig = {
  CTF_SPACE_ID: process.env.CTF_SPACE_ID,
  CTF_CDA_ACCESS_TOKEN: process.env.CTF_CDA_ACCESS_TOKEN
}

module.exports = {
  createClient(config = defaultConfig) {
    return contentful.createClient({
      space: config.CTF_SPACE_ID,
      accessToken: config.CTF_CDA_ACCESS_TOKEN
    })
  }
}

Contentfulを利用するためのjsを用意

nuxt.config.js

import colors from 'vuetify/es5/util/colors'
import {createClient} from './plugins/contentful.js'

const pkg = require('./package')
const {getConfigForKeys} = require('./lib/config.js')
const ctfConfig = getConfigForKeys([
  'CTF_SPACE_ID',
  'CTF_CDA_ACCESS_TOKEN'
])

const {createClient} = require('./plugins/contentful')
const cdaClient = createClient(ctfConfig)

export default {
  mode: 'universal',

略・・・
}

nuxt.config.jsを設定

参考にしたサイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?