...und mehr auf der Liste
Gatsby installieren
> npm install -g gatsby-cli
neues Projekt anlegen
> gatsby new catfan-goodies
Development Server starten
> cd catfan-goodies> gatsby develop
und Seite zum ersten Mal aufrufen:
http://localhost:8000> npm install tailwindcss \
gatsby-plugin-postcss --save-dev> ./node_modules/.bin/tailwind init
module.exports = () => ({
plugins: [require("tailwindcss")],
})
gatsby-config.js bekannt machen:plugins: [`gatsby-plugin-postcss`],src/components/layout.css den gesamten css code durch folgendes ersetzen:@tailwind base;
@tailwind components;
@tailwind utilities;npm i gatsby-plugin-purgecssgatsy-config.js nach allen css/postcss Plugins einfügen:plugins: [ ...
{
resolve: `gatsby-plugin-purgecss`,
options: {
printRejected: false,
develop: false,
tailwind: true,
},/src/data/ablegen.
> npm i --save gatsby-transformer-csvgatsby-config.js bekannt machen:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `data`,
path: `${__dirname}/src/data`,
},
},
{
resolve: `gatsby-transformer-csv`,
options: {
delimiter: [";"],
},
},allCatfanshopCsv enthält die Daten der catfanshop.csv und sie stehen zum Erzeugen der Seiten bereit.
Gatsby sagen welche Seiten wie generiert werden sollen in: gatsby-node.js
Beispielcode in gatsby.node.js für Detailseiten:
const path = require(`path`)
exports.createPages = async ({ graphql, actions }) => { // async (input) => { const graphql = input.graphql ...
const { createPage } = actions // const createPage = actions.createPage
// Create Detailpages from CSV
const resultPages = await graphql (`
query {
allCatfanshopCsv {
nodes {
Artikelnummer
Bezeichnung
Beschreibung
Bild {
relativePath
}
Warengruppe
Preis
}
}
}
`)
resultPages.data.allCatfanshopCsv.nodes.forEach( node => {
const category = node.Warengruppe.toLowerCase()
createPage({
// Path for this page — required
path: category +'/' + node.Artikelnummer + '/',
component: path.resolve(`./src/templates/catalog-detailpage.js`),
context: {
ArtNr: node.Artikelnummer,
name: node.Bezeichnung,
image: node.Bild.relativePath,
description: node.Beschreibung,
group: node.Warengruppe,
price: node.Preis
}
})
})
}
/src/templates/
catalog-detailpage.js, siehe gatsby-node.js) anlegencatalog-detailpage.js
import React from "react"
import { graphql, Link } from "gatsby"
import Img from "gatsby-image"
import Layout from "../components/layout"
import SEO from "../components/seo"
export default ({ data, pageContext }) => {
return (
{ pageContext.name }
Artikelnr.: { pageContext.ArtNr }
Preis: EUR {pageContext.price}
Home
)
}
export const query = graphql`
query($image: String!) {
bild: file(relativePath: { eq: $image }) {
childImageSharp {
fluid(maxWidth: 800) {
...GatsbyImageSharpFluid
}
}
}
}
`
/src/components/navCategories.js
import React from "react"
import { useStaticQuery, graphql, Link } from "gatsby"
import "./layout.css"
const NavCategories = () => {
const data = useStaticQuery(graphql`
query {
allCatfanshopCsv {
distinct(field: Warengruppe)
}
}
`)
const categories = data.allCatfanshopCsv.distinct
return (
{ categories.map((category) => (
{ {category} }
)) }
)
}
export default NavCategories
<NavCategories /> in index.js einfügen.
> gatsby build> gatsby serve/publicenthält erstellte Seiten, die auf den Server geladen werden können
now.json anlegen:
{
"routes": [
{ "handle": "filesystem" },
{ "src": "/.*", "status": 404, "dest": "/404" }
]
}
Code auf GitHub: https://github.com/pointout/cas-gatsbyjs
Diese Folien: https://slides.pointout.at/gatsbyjs-online-katalog/