Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JAMstack_kotohajime.pdf

 JAMstack_kotohajime.pdf

IMANISHI TOSHIFUMI

December 07, 2019
Tweet

More Decks by IMANISHI TOSHIFUMI

Other Decks in Technology

Transcript

  1. JOEFYWVF export default { data () { return { posts:

    [] } }, … async asyncData({ $axios }) { const { data } = await $axios.get('/posts?_embed') return { posts: data } }, } BTZOD%BUB͸/VYUKTͰఏڙ͢Δಠࣗʹ֦ுͨ͠7VFKTͷίϯϙʔωϯτΦϓγϣϯ ඇಉظʹσʔλΛऔಘ͢ΔͨΊͷϝιουͰɺ7VFKTͷEBUBʹϚʔδ͞Ε·͢ OVYUDPOpHKTͰఆٛͨ͠CBTF63-͕ࢀর͞Ε·͢ @FNCFEύϥϝʔλ͸ɺΞΠΩϟονը૾΍ίϝϯτͳͲͷ৘ใΛऔಘͰ͖·͢
  2. JOEFYWVF <template> … <article v-for="post in posts" :key=“post.id"> <n-link :to="{

    path: '/posts/' + post.id }"> <h2>{{ post.title.rendered }}</h2> <div v-html="post.excerpt.rendered"></div> </n-link> </article> … </template> ݻఆϖʔδͷϦϯΫऔಘ ݻఆϖʔδͷλΠτϧऔಘ ݻఆϖʔδͷ಺༰ൈਮ
  3. export default { … generate: { routes: async () =>

    { const { data } = await axios.get(`${apiUrl}/posts?_embed`) return data.map(post => { return { route: `/posts/${post.id}`, payload: post } }) } } … } OVYUDPOpHKT HFOFSBUFίϚϯυͰ͸ɺಈతͳϧʔςΟϯά͸ແࢹ͞Ε·͢ɻ ಈతͳύϥϝʔλʔΛ༻͍ͨϧʔτΛੜ੒͍ͤͨ͞৔߹͸ɺಈతͳϧʔςΟϯάͷ഑ྻΛηοτ͢Δඞཁ͕͋Γ·͢ɻ QBZMPBE͸ɺಈతϧʔςΟϯάੜ੒ͷߴ଎ԽͷͨΊͰ͢ɻ
  4. @JEWVF <template> <div> <h2>{{ post.title.rendered }}</h2> <div v-html="post.content.rendered"></div> </div> </template>

    … export default { data () { return { post: {} } }, async asyncData({ $axios, params, error, payload }) { if (payload) { return { post: payload } } else { const { data } = await $axios.get(`/posts/${params.id}?_embed`) return { post: data } } } }
  5. HBUTCZDPOpHKT require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) module.exports = { … plugins:

    [ … { resolve: `gatsby-source-contentful`, options: { spaceId: `${process.env.CONTENTFUL_SPACE_ID}`, accessToken: `${process.env.CONTENTFUL_ACCESS_TOKEN}`, } }, `gatsby-transformer-remark` … ], } (BUTCZ+4͸ͭͷ؀ڥΛαϙʔτ͠·͢ɻ w HBUTCZEFWFMPQΛ࣮ߦ͢Δͱʮ։ൃʯ؀ڥʹͳΓ·͢ɻ w HBUTCZCVJMEɺ·ͨ͸ɺHBUTCZTFSWFΛ࣮ߦ͢Δͱʮຊ൪ʯ؀ڥʹͳΓ·͢ɻ HBUTCZUSBOTGPSNFSSFNBSL͸(BUTCZ+4Ͱ.BSLEPXOϑΝΠϧΛѻ͏ͨΊͷ5SBOTGPSNFSϓϥάΠϯͰ͢ɻ
  6. TSDQBHFTJOEFYKT import React from "react" import { Link, graphql }

    from "gatsby" const IndexPage = ({ data }) => ( <div> <ul> { data.allContentfulCourse.edges.map(edge => { return ( <li key={edge.node.slug}> <Link to={ edge.node.slug }><h3>{ edge.node.title }</h3></Link> <div>{ edge.node.description.childMarkdownRemark.excerpt }</div> </li> ) }) } </ul> </div> ) export default IndexPage export const pageQuery = graphql` …
  7. HBUTCZOPEFKT const path = require(`path`) exports.createPages = async ({ graphql,

    actions: { createPage } }) => { const sources = await graphql(` … `) if (sources.errors) return const posts = sources.data.allContentfulCourse.edges posts.forEach(({ node }) => { createPage({ path: `${node.slug}`, component: path.resolve(`src/templates/blog-post.js`), context: { slug: node.slug } }) }) } औಘ͍ͨ͠$POUFOU5ZQFΛࢦఆ͠·͢ɻ w QBUIϖʔδΛग़ྗ͢Δύεͷࢦఆ w DPNQPOFOUϖʔδΛੜ੒͢Δࡍʹ࢖͏ςϯ ϓϨʔτͷύεΛࢦఆ w DPOUFYUQBHF2VFSZͷҾ਺ͱςϯϓϨʔτ ͷQSPQTʹ౉͢஋Λࢦఆ σʔλΛऔಘ͢ΔͨΊͷ(SBQI2-ΫΤϦ ϖʔδੜ੒ͷॲཧ
  8. UFNQMBUFTCMPHQPTUKT import React from 'react' import { graphql } from

    'gatsby' export const pageQuery = graphql` … ` export default (props) => { const { title, description } = props.data.contentfulCourse return ( <div> <h1>{ title }</h1> <div dangerouslySetInnerHTML={{ __html: description.childMarkdownRemark.html }} /> </div> ) } QBHF2VFSZ͸ϖʔδʹඞཁͳσʔλΛ(SBQI2-ͷRVFSZʹΑͬͯऔಘ͢ΔͨΊͷ΋ͷͰɺ औಘͨ͠σʔλΛίϯϙʔωϯτʹQSPQTͰ౉ͯ͠ϖʔδΛੜ੒͠·͢ɻ औಘ͍ͨ͠$POUFOU5ZQFΛࢦఆ͠·͢ɻ