ããã°ã«ããŒã¯ãŒãæ€çŽ¢æ©èœãè¿œå ããŸãã
ã¿ã€ãã«ã®ãŸããŸã§ããå³äžã«ããè«çŒé¡ãã¿ã³ãã¯ãªãã¯ããã°ããã¹ãããã¯ã¹ãçŸããããŒã¯ãŒãã«ããèšäºã®çµã蟌ã¿æ€çŽ¢ãã§ããŸãã
çµã蟌ã¿æ€çŽ¢ã¯ã€ã³ã¯ãªã¡ã³ã¿ã«ãµãŒããšãåŒã°ããããŒãžé·ç§»ã䌎ãããšãªã段éçã«èšäºãçµã蟌ãã§ãããŸãã
ãgããšå ¥åããå ŽåïŒ10件ïŒã
ãgitããšå ¥åããå ŽåïŒ4件ïŒã
ãgit logããšå ¥åããå ŽåïŒ3件ïŒã
ãã®ããã°ã¯æ¢ã«ã·ãªãŒãºæ€çŽ¢ãšã¿ã°æ€çŽ¢ã®æ©èœãæã£ãŠãããã以äžã®æ€çŽ¢æ©èœã¯äžèŠããšæã£ãã®ã§ããã幎æ«å¹Žå§ãéåžžã«æã ã£ãããšãããå®è£ ããŸããã
ä»åã¯ãã®æ€çŽ¢æ©èœã«ã€ããŠããããŠãã®å®è£ æ¹æ³ã解説ããŸãã
JSONãã¡ã€ã«ããæ€çŽ¢ãã
ããŠãéçãµã€ãã§ã®æ€çŽ¢æ©èœãšããã°algoliaãçã£å ã«æãæµ®ãã³ãŸãããç§ãå©çšããããšæã£ãŠè²ã 調ã¹ãŠã¿ãã®ã§ããã
- ð€ algoliaã«é¢ããç¥èãå šããªã
- ð€ æéãããããããããªã
- ð€ Saasãªã©ã®å€éšãµãŒãã¹ãå©çšããã®ã¯å€§ãã
- ð€ å šææ€çŽ¢ã¯å¿ èŠãªã
ãªã©ã®çç±ããå°å ¥ã¯ããªãããšã«ããŸãããä»åã¯ãGatsbyã®ãã«ãããã»ã¹æã«åèšäºã®frontmatterããJSONãã¡ã€ã«ãäœæããã³ã³ããŒãã³ãããJSONãã¡ã€ã«ãæ€çŽ¢ããããšããè³æ¥µã·ã³ãã«ãªæ¹æ³ããšãããšã«ããŸããã
frontmatterã«keywordsãè¿œå ãã
次ã«ãæ€çŽ¢å¯Ÿè±¡ãã©ããããããšèããŸãããã±ã£ãšæãã€ãã®ã¯
- ð èšäºã®ã¿ã€ãã«
- ð ã·ãªãŒãºå
- ð ã¿ã°
- ð description
- ð èšäºæ¬æïŒå šææ€çŽ¢ïŒ
ãããã§ãããå šãŠã察象ã«ãããšããžãã¯ãè€éã«ãªãããJSONãã¡ã€ã«ãè¥å€§åããã®ã§ãããŸãããèšäºæ¬æãJSONãã¡ã€ã«ã®è¥å€§åããããŠãããŸã§å¿ èŠãªããšããçç±ã§åŽäžãã·ãªãŒãºåãã¿ã°åã«ããæ€çŽ¢ãã»ãŒåãæ©èœãæ¢ã«ããã®ã§åŽäžãã¿ã€ãã«ãdescriptionã¯æ€çŽ¢å¯Ÿè±¡ãšããŠã¯æ å ±éãäžè¶³ãããšããããšã§åŽäžã
ããã€ãã®èŠçŽ ãçµã¿åããããããšãæããŸããããã°ã¿ã°ã¿èããããããªãfrontmatterã«æ°ããé ç®ãè¿œå ããŠããŸãããšããããšã§ããkeywordsããšããé ç®ãè¿œå ããŸãããããŒã¯ãŒããšãªãåèªãé åã«æ ŒçŽããŠãããŸãã
è¿œå ããèšèã¯ãã¿ã°ãããç²åºŠã®çŽ°ããèšèãã«ããŸãããã¿ã°ã«ããã»ã©ã§ã¯ãªããã©æ¬æã«äœåãåºçŸããèšèã§ãããã¿ã°ãšæ¬æã®äžéãããã®ã€ã¡ãŒãžã§ãã
ãŸãããBlogããããã°ããªã©ã®æ¥æ¬èª/è±èªã®ããããSSGããStatic Site Generatorããªã©ã®ç¥ã/ç¥ããªããšããããã¯ã¿ã°æ€çŽ¢ã§ã¯å®çŸãã«ããã§ããæ©èœãè¿œå ããå©ç¹ãã¢ãããŒã·ã§ã³ã«ãªããšæããé¢åèãã§ãã现ãã«èšè¿°ããŠãããŸãã
---
# äŸãã°ãããªæã
keywords: ["Gatsby", "Blog", "ããã°", "SSG", "Static Site Generator"]
---
èŠä»¶å®çŸ©
ããã§ãç°¡åã«èŠä»¶å®çŸ©ãããŠãããŸãã
- âïž å ¥åããæååããããŒã¯ãŒãããšããŠæã£ãŠããèšäºãæ€çŽ¢ãããã®èšäºãžã®ãªã³ã¯ããªã¹ãã¢ãããã
- âïž ããŒã¯ãŒãã¯ãåèšäºã®frontmatterãšããŠå®çŸ©ãã
- âïž åè§ã¹ããŒã¹ãçšããŠè€æ°ã®åèªãå ¥åå¯èœã«ãã
- âïž è€æ°ã®æååãå ¥åãããå Žåã¯ANDæ€çŽ¢ãè¡ãïŒAAA BBBãšå ¥åãããã°ããã®äž¡æ¹ãæã£ãŠããèšäºããªã¹ãã¢ããããïŒ
- âïž è±èªãæ¥æ¬èªãå ¥å察象ãšãã
- âïž ã¢ã«ãã¡ãããã¯ã±ãŒã¹ã»ã€ã³ã»ã³ã·ãã£ãã«ããïŒæ€çŽ¢æã«å€§æåãå éšçã«å°æåã«å€æããŠæ¯èŒãè¡ãïŒ
- âïž ããŒãžé·ç§»ãªãã®çµã蟌ã¿æ€çŽ¢ãè¡ã
ããããå ã«æ©èœãå®è£ ããŠãããŸãã
ãã«ãæã«JSONãã¡ã€ã«ãæžãåºã
åèšäºã«keywords
ãçšæããããJSONãã¡ã€ã«ãæžãåºãæ©èœãå®è£
ããŸããGatsbyã®ãã«ãããã»ã¹æã«JSONãã¡ã€ã«ãçæããã®ã§ãç·šéããã®ã¯gatsby-node.js
ã§ããã
ãŸãã¯ãå
šãŠã®èšäºã®frontmatter
ã®keywords
é
ç®ãååŸããGraphQLã¯ãšãªãŒãè¿œå ããŸãã
const createPages = async ({ graphql, actions, reporter }) => {
const queryResult = await graphql(
`
...ç¥
{
# å
šãŠã®èšäºãååŸ(æ€çŽ¢çš)
allArticlesForSearching: allMarkdownRemark(
sort: {fields: frontmatter___postdate, order: DESC}
) {
edges {
node {
fields {
slug
}
frontmatter {
keywords
title
}
}
}
}
}
`
JSONãã¡ã€ã«ã®æžãåºãã¯ä»¥äžã®ããã«è¡ããŸãããã¡ã€ã«ã®ä¿åå
ã¯ã«ãŒãçŽäžã®static
ããã¡ã€ã«åã¯keywordSearch.json
ã«ããŸããã
const keywords = queryResult.data.allArticlesForSearching.edges.map(({node}) => {
return {
slug: node.fields.slug,
title: node.frontmatter.title,
keywords: node.frontmatter.keywords,
}
})
fs.writeFileSync('./static/keywordSearch.json', JSON.stringify(keywords, null, 2))
ããã§gatsby develop
ãããšãstatic/keywordSearch.json
ãçæãããŸãããŸãããã®å
容ã¯ä»¥äžã®ããã«ãªã£ãŠããã¯ãã§ãã
[
{
"slug": "/Others/01/",
"title": "ãããã·ç°å¢ã§Kali Linuxã䜿ã",
"keywords": [
"Kali Linux",
"WSL2",
"Proxy",
"ãããã·",
"apt",
"wget",
"curl"
]
},
//...ç¥
]
gatsby-transformer-jsonãã€ã³ã¹ããŒã«ãã
JSONãã¡ã€ã«ãçæã§ããã®ã§ã次ã¯ãããã³ã³ããŒãã³ãããååŸããŸããGatsbyã«ã¯gatsby-transformer-json
ãšããããã±ãŒãžãçšæãããŠãããããã䜿ãããšã§GraphQLã¯ãšãªãŒã§JSONãã¡ã€ã«ãååŸã§ããŸããæ©éã€ã³ã¹ããŒã«ããŸãããã
ãªããgatsby-transformer-json
ã¯ã2022幎1æçŸåš^4.4.0
ãææ°ãªããã§ãããGatsby v3ã䜿çšããŠããç°å¢ã ãšãwarn Plugin gatsby-transformer-json is not compatible with your gatsby version 3.12.1 - It requires gatsby@^4.0.0-nextãã¿ãããªãšã©ãŒãåºãã¯ãã§ãã
Gatsby v4ã䜿çšããŠããã®ã§ããã°åé¡ãããŸããããGatsby v3ã䜿çšäžã®æ¹ã¯gatsby-transformer-json@^3.0.0
ãã€ã³ã¹ããŒã«ããŸãããã
$ yarn add gatsby-transformer-json@^3.0.0
ã€ã³ã¹ããŒã«ã§ãããããŸãã¯gatsby-config.js
ã®ãã©ã°ã€ã³ã®éšåã«è¿œèšããŸãã
module.exports = {
plugins: [
`gatsby-transformer-json`,
// ...ç¥
]
ãŸããstatic
ã®äžã«ãããã¡ã€ã«ãæ±ãããã§ããããgatsby-source-filesystem
ã«ãããŠstatic
ãžã®èšå®ãããŠããå¿
èŠããããŸããèšå®ãåºæ¥ãŠããªãå Žåã¯ä»¥äžã®ããã«è¿œèšããŸãã
// è¿œèš
{
resolve: `gatsby-source-filesystem`,
options: {
// ãã©ã«ããŒãæå®
path: `static`,
// ä»»æã®ååãä»ãã
name: `keywordSearch`,
},
},
ããã§å床gatsby develop
ãè¡ããlocalhost:8000/___graphql
ã«ã¢ã¯ã»ã¹ããGraphiQLã§Graphqlã¯ãšãªãŒãçºè¡ããJSONãã¡ã€ã«ãååŸã§ããããã¹ãããŠã¿ãŸãããã
JSONãã¡ã€ã«ãååŸããã¯ãšãªãŒã®ååã§ãããä¿åããŠããJSONãã¡ã€ã«ã®ãã¡ã€ã«åãèžè¥²ãããŸããä»åã¯keywordSearch.json
ãšããååã§JSONãã¡ã€ã«ãååšããŠããã®ã§ãallKeywordSearchJson
ãªããkeywordSearchJson
ãšããã¯ãšãªãŒãçšæãããŠããã¯ãã§ãã
GraphQLã¯ãšãªãŒã¯ä»¥äžã®ããã«æããŸããååŸãããã£ãŒã«ãã§ãããèå¿ã®keywords
ãšãæ€çŽ¢ã§ãããããèšäºãžã®ãªã³ã¯ãäœæããããã«slug
ãštitle
ãå¿
èŠã§ãã
query MyQuery {
allKeywordSearchJson {
edges {
node {
keywords
slug
title
}
}
}
}
ã¯ãšãªãŒãå®è¡ããŠå³åŽã®ãã€ã³ã«ãšã©ãŒãªãçµæã衚瀺ãããã°OKã§ãã以äžã¯çè ã®ç°å¢ã§ã®ã¯ãšãªãŒã®çµæã§ãã
{
"data": {
"allKeywordSearchJson": {
"edges": [
{
"node": {
"keywords": [
"Kali Linux",
"WSL2",
"Proxy",
"ãããã·ãŒ",
"apt",
"wget",
"curl"
],
"slug": "/Others/01/",
"title": "ãããã·ç°å¢ã§Kali Linuxã䜿ã"
}
},
// ...以äžãåæ§ã«ç¶ã
]
}
},
}
ã³ã³ããŒãã³ããäœæãã
ãããŸã§ããã°ç®çã®å€§åã¯éæãããåç¶ã§ããèšäºãæ€çŽ¢ããã³ã³ããŒãã³ããäœæããŸããããsrc/components/keywordSearch.jsx
ãçšæããŸãããŸãã¯ä»¥äžã®ããã«èšè¿°ããŠãããŸãã
import React from "react"
export const KeywordSearch = () => {
return (
<p>This is a search component</p>
)
}
次ã«ãGraphQLã¯ãšãªãŒãèšè¿°ããŸããã³ã³ããŒãã³ãããã¯ãšãªãŒãæããããã§ãããuseStaticQuery
ãå©çšããŸããuseStaticQuery
ãšgraphql
ãã€ã³ããŒããã以äžã®ããã«èšè¿°ããŸããé©åœãªæã«console.log(allKeywordSearchJson)
ãä»èŸŒã¿ãçµæã確èªã§ããããã«ããŠãããŸãã
import React from "react"
import {useStaticQuery, graphql} from "gatsby"
export const KeywordSearch = () => {
const {allKeywordSearchJson} = useStaticQuery(
graphql`
{
allKeywordSearchJson {
edges {
node {
keywords
slug
title
}
}
}
}
`
)
console.log(allKeywordSearchJson)
return (
<p>This is a search component</p>
)
}
ãã¡ã€ã«ãã§ããããé©å®src/components/layout.jsx
ãªã©ã«ã³ã³ããŒãã³ããè¿œèšããŸãã
import React, { ReactNode } from "react"
import { KeywordSearch } from "./keywordSearch"
const Layout = ({children}) => (
<>
<KeywordSrarch />
{children}
</>
)
export default Layout
gatsby develop
ã§ããŒã«ã«ãµãŒããŒãèµ·åããããŒãžã«ã¢ã¯ã»ã¹ãã³ã³ãœãŒã«ã§çµæã確èªããŸãã
ä»åãUIã®äœæã«ã¯useState
ãšuseEffect
ã䜿çšããŸãããŸãã¯useState
ã§å
¥åãããæååãä¿æããStateãšãæ¡ä»¶ã«ãã£ãŠçµã蟌ãŸããèšäºãã¹ãŠãä¿æããStateãçšæããŸãã
ã€ãã§ã«å ¥åãã©ãŒã ãæžããŠãããŸãããã
import React, {useState, useEffect} from "react"
import { useStaticQuery, graphql } from "gatsby"
export const KeywordSearch = () => {
// ãã©ãŒã ã«å
¥åãããæååãä¿æããState
const [inputtedWords, setInputtedWords] = useState("")
// æ¡ä»¶ã«ãã£ãŠçµã蟌ãŸããèšäºãä¿æããState
const [filteredPosts, setFilteredPosts] = useState(null)
const { allKeywordSearchJson } = useStaticQuery(
graphql`
{
allKeywordSearchJson(skip: 3) {
edges {
node {
keywords
slug
title
}
}
}
}
`
)
return (
<input type="text" />
)
}
ä»åã¯ã€ã³ã¯ãªã¡ã³ã¿ã«ãµãŒãã§ããããå
¥åããã¯ã¹ã«1æåå
¥åããããã³ã«GraphQLã¯ãšãªãŒã®çµæãå
¥ã£ãŠããallSearchJson
ãªããžã§ã¯ããèµ°æ»ããŠãçµæãæžãæããå¿
èŠããããŸãã
ãŸãã¯inputèŠçŽ ã«onChange
å±æ§ãå®çŸ©ããå
¥åãããæååãsetInputtedKeywords
ã«æž¡ãããã«ããŸãã
return (
<input
type="text"
// å
¥åãããæååã§inputtedKeywordsãæŽæ°ãã
onChange={(e) => setInputtedWords(e.target.value)}
/>
)
ç¶ããŠãuseEffect
ãå®çŸ©ãã第äºåŒæ°ã«inputtedWords
ãæž¡ããŸããããã§å
¥åãã©ãŒã ã«1æåæžã蟌ãŸãããã³ã«useEffect
ãå®è¡ãããç¶æ
ã«ãªããŸãããuseEffect
ã«ã¯console.log(inputtedWords)
ãªã©ãšèšè¿°ãããã©ãŒã ã«1æåå
¥åããããã³ã«ã³ã³ãœãŒã«åºåãããããšã確èªããŠãã ããã
export const KeywordSearch = () => {
//...ç¥
// ãã©ãŒã ã«æååãå
¥åããããã³ã«å®è¡ããã
useEffect(() => {
console.log(inputtedWords)
// ããã«inputtedWordsã®äžèº«ã䜿ã£ãŠJSONãã¡ã€ã«ãèµ°æ»ãã
// filteredPostsãæžãæããåŠçãæžã
}, [inputtedWords])
ããã§ã¯useEffect
ã®åŠçãèšè¿°ããŸãã
ä»åãã¢ã«ãã¡ãããã®æ€çŽ¢ã¯ã±ãŒã¹ã»ã€ã³ã»ã³ã·ãã£ãã«å®è£ ããã®ã§ãå ¥åãããæååãå šãŠå°æåã«å€æããå¿ èŠããããŸãã
以äžã®ããã«ãå
¥åãããæååãå
šãŠå°æåã«å€æãlowerCaseWords
ãšãã£ãå€æ°ã«é
åãšããŠä¿ç®¡ããŸãã
è€æ°ã®æåãå ¥åãããå Žåã«ANDæ€çŽ¢ãè¡ãå¿ èŠããããããåãåããããããããã«é åã«ããŠããŸãã
useEffect(() => {
// å
¥åãããããŒã¯ãŒããå°æåã«å€æãã
const lowerCaseWords = inputtedWords
.trim()
.toLocaleLowerCase()
.match(/[^\s]+/g)
console.log(lowerCaseWords)
}, [inputtedWords])
以äžã®ç»åã¯ãHello World
ãšå
¥åããæã®ã³ã³ãœãŒã«åºåã®æ§åã§ããã¢ã«ãã¡ããããå°æåã«å€æãããåèªããšã«é
åã«æ ŒçŽãããŠããããšãããããŸãã
// ãããããèšäºãããã«æ ŒçŽããã
const searchedResult = // æ€çŽ¢åŠçãæžã
èå¿ã®ããããããã©ãããå€å®ããéšåã§ãããç§ã¯ä»¥äžã®ããã«æžããŸããã
// ãããããèšäºãããã«æ ŒçŽããã
const searchedResult = allKeywordSearchJson.edges.filter(({node}) => {
return lowerCaseWords?.every((word) => {
return node?.keywords?.toString().toLocaleLowerCase().includes(word)
})
})
// çµæ確èªçš
console.log(lowerCaseWords, searchedResult)
ããã§1æåæã€ãã³ã«æ€çŽ¢ãè¡ãããçµæãã³ã³ãœãŒã«åºåãããŸãã
ãã®searchedResult
ããæ€çŽ¢çµæãä¿æããstateã§ããfilteredPosts
ã«ä»£å
¥ããŸãã
// çµã蟌ãŸããèšäºäžèŠ§ã§æŽæ°ãã
setFilteredPosts(searchedResult.length ? searchedResult : null)
æåŸã«ããããããèšäºäžèŠ§ã衚瀺ããéšåãæžãã°å®æã§ããããã§ã¯ul
èŠçŽ ãšli
èŠçŽ ãaèŠçŽ
ã䜿çšããããšã«ããŸãã
ã²ãšãŸãåºåçµæã確èªãããã®ã§ã以äžã®ããã«æžããŠã¿ãŸãããã
return (
<>
<input
type="text"
onChange={(e) => setInputtedWords(e.target.value)}
/>
<ul>
{filteredPosts && filteredPosts.map((post) => {
return (
<li>
{post.node.title}
</li>
)
})}
</ul>
</>
)
æ£ããèšäºãªã¹ããåºåãããã°OKã§ãã
åŸã¯Link
ãã€ã³ããŒãããŠã
import React, { useState, useEffect } from "react"
import { useStaticQuery, graphql, Link } from "gatsby"
// ...ç¥
slug
ã䜿ã£ãŠãã¹ãèšè¿°ããããã«æžãæããŸãã
return (
<>
<input
type="text"
onChange={(e) => setInputtedWords(e.target.value)}
/>
<ul>
{filteredPosts && filteredPosts.map((post) => {
return (
<li style={{"fontSize": "140%"}}>
<Link
to={post.node.slug}
key={post.node.slug}
>
{post.node.title}
</Link>
</li>
)
})}
</ul>
</>
)
ã¡ãããšãªã³ã¯ã衚瀺ãããæ©èœããããšã確èªããŸãã
ããã§åºæ¬æ©èœã¯å®æã§ããUIãäœããã£ããããããªãã§ããããããããŒã¹ã«ã«ã¹ã¿ãã€ãºããŠããã°å®çšçãªæ€çŽ¢æ©èœãšããŠäœ¿ããã¯ãã§ãã
src/components/keywordSearch.jsx
ã®ã³ãŒãå
šäœã眮ããŠãããŸãã
import React, { useState, useEffect } from "react"
import { useStaticQuery, graphql, Link } from "gatsby"
export const KeywordSearch = () => {
// ãã©ãŒã ã«å
¥åãããæååãä¿æããState
const [inputtedWords, setInputtedWords] = useState("")
// æ¡ä»¶ã«ãã£ãŠçµã蟌ãŸããèšäºãä¿æããState
const [filteredPosts, setFilteredPosts] = useState(null)
// ãã©ãŒã ã«æååãå
¥åããããã³ã«å®è¡ããã
useEffect(() => {
// å
¥åãããããŒã¯ãŒããå°æåã«å€æãã
const lowerCaseWords = inputtedWords
.trim()
.toLocaleLowerCase()
.match(/[^\s]+/g)
// ãããããèšäºãããã«æ ŒçŽããã
const searchedResult = allKeywordSearchJson.edges.filter(({node}) => {
return lowerCaseWords?.every((word) => {
return node?.keywords?.toString().toLocaleLowerCase().includes(word)
})
})
// çµã蟌ãŸããèšäºäžèŠ§ã§æŽæ°ãã
setFilteredPosts(searchedResult.length ? searchedResult : null)
}, [inputtedWords])
const { allKeywordSearchJson } = useStaticQuery(
graphql`
{
allKeywordSearchJson(skip: 3) {
edges {
node {
keywords
slug
title
}
}
}
}
`
)
return (
<>
<input
type="text"
onChange={(e) => setInputtedWords(e.target.value)}
/>
<ul>
{filteredPosts && filteredPosts.map((post) => {
return (
<li style={{"fontSize": "140%"}}>
<Link
to={post.node.slug}
key={post.node.slug}
>
{post.node.title}
</Link>
</li>
)
})}
</ul>
</>
)
}
ããŒã¯ããŠã³ã®ç·šéã§gatsby develop
ãæ»ã¬
ããæãããgatsby develop
äžã«ããŒã¯ããŠã³ãç·šéãããšããã»ã¹ãæ»ã¬ããã«ãªããŸããã
info changed file at C:\github\GatsbyBlog\static\keywordSearch.json
success extract queries from components - 0.108s
success write out requires - 0.002s
success Writing page-data.json files to public directory - 0.000s - 0/44
119144.33/s
ERROR
Panicking because nodes appear to be being changed every time we run queries.
This would cause the site to recompile infinitely.
Check custom resolvers to see if they are unconditionally creating or mutating
nodes on every query.
This may happen if they create nodes with a field that is different every time,
such as a timestamp or unique id.
åè¿°ã®ãšãããgatsby-node.js
ã§ããŒã¯ããŠã³ãã¡ã€ã«ãåºã«JSONãã¡ã€ã«ãçæããŠããããã§ãããããŒã¯ããŠã³ãã¡ã€ã«ã®æŽæ°ããã£ããã«ããããªããŒããèµ°ããJSONãã¡ã€ã«ã®çæãç¡éã«ãŒãããŠããŸã£ãŠããããã§ããåœåã¯ãããªããšèµ·ããªãã£ããšæããã§ããããã€ããããããªã£ãŠããŸããŸããðïŒè¡æ¶ïŒã
ãã ãæ¬çªç°å¢ã§èµ·ããé¡ã®ãã®ã§ã¯ãªããããŸã§ã¯ãªãã£ã«ã«ãªåé¡ã§ã¯ãªãã£ãããšãæãã§ãã
ç°å¢å€æ°process.env.NODE_ENV
ãå©çšããã°ãGatsbyãã©ã®ã¢ãŒãã§å®è¡ãããŠããããåãåºããŸããgatsby develop
ãªãdevelopment
ãgatsby build
ãªãproduction
ãæ ŒçŽãããŸãã
ä»åã¯gatsby-node.js
ã以äžã®ããã«æžãæããæ¬çªç°å¢æïŒgatsby build
ãå®è¡ãããæïŒã®ã¿JSONãã¡ã€ã«ã®çæãè¡ãããã«ããŸããã
// æ¬çªç°å¢ã®ã¿JSONãã¡ã€ã«ã®çæãè¡ã
if(process.env.NODE_ENV === 'production') {
const keywords = queryResult.data.allArticlesForSearching.edges.map(({node}) => {
return {
slug: node.fields.slug,
title: node.frontmatter.title,
keywords: node.frontmatter.keywords,
}
})
fs.writeFileSync('./static/keywordSearch.json', JSON.stringify(keywords, null , 2))
}
ããŒã«ã«ã§gatsby develop
ããŠããæã«ã¯frontmatterã®keywords
ãæžãæããé »åºŠã¯å€ããªãã§ãããæžãæãããšããŠãäžåºŠgatsby build
ããã°JSONãã¡ã€ã«ãææ°ã«ã§ããŸãã
ãããã€ããæã«ã¯gatsby build
ãè¡ãããŸãã®ã§ãæ¬çªç°å¢ã§ã¯JSONãã¡ã€ã«ã¯ã¡ãããšææ°ã«ãªã£ãŠããŸãã
æŽæ°å±¥æŽ
- 2022幎12æ05æ¥ : ã·ãªãŒãºããæ¥èšãããããã®ä»ãã«å€æŽã