Journey of 5 Years with Headless WordPress

301 Views

March 18, 24

スライド概要

Presentation deck of WordCamp Asia 2024

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

(ダウンロード不可)

関連スライド

各ページのテキスト
2.

Hidetaka Okamoto (@hidetaka_dev) - WordCamp Kyoto 2017 WordCamp Kansai 2024 Stripe DevRel AWS Samurai 2017 Alexa Champions https://hidetaka.dev

3.

Submit my homework after 9 years.

4.

My first Flagship WordCamp

5.

From “State of the Word 2015” by Matt Mullenweg https://wordpress.tv/2015/12/07/matt-mullenweg-state-of-the-word-2015/

6.

2014 WP API(v1) https://github.com/WP-API/WP-API/releases/tag/1.0

7.

Journey of 5 Years with Headless WordPress Evolution, Challenges, and Opportunities WordCamp Asia 2024 Hidetaka Okamoto

8.

Journey of 59 Years with Headless WordPress Evolution, Challenges, and Opportunities WordCamp Asia 2024 Hidetaka Okamoto

9.

Journey of 9 Years

10.

2015

11.

2015 Start to learn WP API

12.

Published 1st WP API plugin - Understand how we can call WP API https://wordpress.org/plugins/wpapi-shortcode-and-widgets/

13.

Run the dev workshop - Understand how we can call WP API - Start to learn JavaScript (jQuery) https://www.docswell.com/s/hideokamoto/5DEN8R-wpapino-php

14.

2016

15.

2016 Create 1st WP API theme

16.

WordPress theme + React app - Build React by gulp - Enqueue build JS file - Only place two PHP file - index.php functions.php https://github.com/hideokamoto/react-wordpress-template

17.

1st impression: "Want to write PHP !"

18.

React app on WordPress Theme is... Unable to leverage the advantages of either

19.

Why did it make a disadvantage? ● ● WordPress ○ No short code / widgets ○ No template hierarchy ○ No WordPress functions ○ No Theme / Plugins React app ○ No standalone app ○ No server side JS ( hydration ) ○ No dev server

20.
[beta]
import { loadStripe } from "@stripe/stripe-js";
window.onload = async () => {
try {
const mountTargetElement =
document.getElementById("payment-element");
const clientSecret =
mountTargetElement.getAttribute('data-client-secret');
const stripe = await loadStripe('pk_test_xxxxxx');
const elements = stripe.elements({
clientSecret,
appearance: {
theme: "stripe"
}
});
const paymentElement = elements.create("payment", {
layout: {
type: 'tabs'
}
});
paymentElement.mount("#payment-element");
} catch (e) {
console.log(e)
}
}

Looking back from 2024

- Use “ViewScript” instead
- Use WP API from
Dynamic-Client Side
Custom block

https://developer.wordpress.org/block-editor/reference-guides/bloc
k-api/block-metadata/#view-script

21.

2017 Omnichannel publishing

22.

Voice User Interface - Amazon Alexa / Siri / etc.. Voice -> Text -> App App -> Text -> Voice https://developer.amazon.com/en-US/alexa/alexa-skills-kit

23.

Use WordPress for VUI CMS - @WordCamp Singapore https://speakerdeck.com/hideokamoto/wordpress-make-easier-t o-create-alexa-skills?slide=4

24.

Looking back from 2024 - How we can “localize” our content for each channel? - LLM may helps us to modify content for VUI https://openai.com/

25.

2018 Static Site Hosting (SPA)

26.

Replace frontend to React SP - Get the data from WP API - Fetch from React - Use prerender for SEO https://www.netlify.com/

27.

Fetch WP API in browser class ContainerListWPPosts extends Component { componentWillMount () { this.props.dispatch(listWpPosts(lang)) } componentWillUnmount () { this.props.dispatch(unsetWpPosts()) } render () { const { items, url } = this.props return ( <Container> <Item.Group divided relaxed> {items.map((item, key) => ( <WpArchiveItem key={key} item={item} url={url} /> ))} </Item.Group> </Container> ) } } export default connect(mapStateToProp)((state) => { const { isFetching, items } = state.posts.list return { isFetching, items } })

28.

Prerender React component for SEO / OGP import React from 'react' export const prerenderReady = () => { window.prerenderReady = true } export function prerenderStaticPage (PageComponent) { return class HOC extends React.Component { componentWillMount () { prerenderReady() } render () { return <PageComponent {...this.props} /> } } }

29.
[beta]
Or run the Node.js server for SSR
function renderFullPage(renderedContent) {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,500' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="container">${renderedContent}</div>
</body>
</html>`;
}
export default λ( (e, ctx, cb) => {
return fetchData( 'http://wp-kyoto.net/wp-json/' )
.then( res =>

res.json())

.then( data => {
const renderedContent = renderToString( <App data={data}/>);
return renderFullPage( renderedContent );
});
});

30.

Looking back from 2024 - SPA is good for Native app - Ionic / Capacitor - React Native - Flutter https://getshifter.io/create-a-universal-application-using-ionic-and-w ordpress-with-capacitor/

31.

2019 - 2020 Framework for Headless WP

32.

https://www.gatsbyjs.com/ https://nextjs.org/

33.

https://www.gatsbyjs.com/plugins/gatsby-source-wordpress/ https://faustjs.org/ (since 2020)

34.

Gatsby - WordPress - Pull the post data into the internal data store Query data through by GraphQL Easy to mash up multiple data source Next.js - WordPress - Fetch the post data on the SSG or SSR rendering process Use content as React props Simply integration

35.

Looking back from 2024 - Framework depends on Hosting - Gatsby: Netlify Next.js: Vercel - Choose framework by the business requirement - Speed, Dev / Ops team staff, SSR or SSG, etc..

36.

2021-2022 SaaS / FaaS

38.
[beta]
Authentication

export const MypageIndex: NextPage = () => {
const { push } = useRouter()
const { user } = useAuthenticator((context) =>
[context.user])
const username = user?.username
useEffect(() => {
if (!username) return
push('/mypage/home/')
}, [username, push])
return (
<Authenticator signUpAttributes={signUpAttributes}>
<MyPages />
</Authenticator>
)
}

Payment /
Subscription
const handler: NextApiHandler = async (request, response) => {
try {
switch (event.type) {
case 'checkout.session.completed': {
await completeCheckoutSession(event.data, response)
break
}
case 'customer.subscription.updated': {
await updateCustomerSubscription(event.data, response)
break
}
await cancelSubscription(event.data, response)
break
}
default:
break
}
return response.status(200).end()
} catch (e) {
console.log(e)
return response.status(500).json(e)
}
}

39.

Split DB for the user management Writer WP_User Amazon Cognito Okta CIC Supabase Visitor ( Member )

40.

Looking back from 2024 - Use IDP is good for user management - SaaS / FaaS empower your site more functionally

41.

2023 AI / LLM

42.

https://platform.openai.com/

43.

Use Large Language Model as an API service

44.

https://www.langchain.com/

46.

Write from WP, Read from Vector Store WordPress Vector Store Post to Vector OpenAI embedding JavaScript App Node.js API Question to Vector Generate answer OpenAI GPT-4

47.
[beta]
const embeddings = new OpenAIEmbeddings({
openAIApiKey: c.env.OPENAI_API_KEY
})
const store = new CloudflareVectorizeStore(embeddings, {
index: c.env.VECTORIZE_INDEX,
});
const fetchResult = await fetch('https://example.com/wp-json/wp/v2/posts?per_page=100')
const posts = await fetchResult.json()
const documents= []
const documentIndex = []
posts.forEach((post) => {
documents.push({
pageContent: `Title:\n${post.title.rendered}\nContent:\n${post.excerpt.rendered}`,
metadata: {
post_id: post.id.toString(),
post_title: post.title.rendered,
post_url: post.link
}
})
documentIndex.push(post.id.toString())
})
await store.addDocuments(documents, { ids: documentIndex })

48.

Demo: WordCamp x LLM - https://github.com/hideokamoto/wordcamp-asi a-2024-rag https://wordcamp-asia-rag.wp-kyoto.workers.d ev/

49.

Personal dev blog -> Knowledge base - QA bot - Contextual search - Solution based content

50.

2024 Blog -> Knowledge base (?)

51.

CQRS (Command Query Responsibility Segregation) Writer Full text search Web app Vector store Native app NoSQL Chat app RDBMS Voice app WordPress wp-admin Hook Webhook

52.

CQRS can… Optimize for each use case by selecting the best DB type Writer Full text search Custom search Vector store Recommend NoSQL High traffic RDBMS Relational data WordPress wp-admin Hook Webhook

53.

+: Omnichannel content distribution Web app Native app Writer WordPress wp-admin wp-json Chat app Voice app

54.

++: Use realworld content for renewing site ver 2023 ver 2024 Writer WordPress wp-admin wp-json Staging Local dev

55.

3 more Useful usecase of Headless WordPress

56.

Where / Why we use WordPress as Headless CMS? 1. Omnichannel content distribution 2. Minimize the impact of server failures 3. Next generation “full-scratched site development” 4. Microservice application

57.

Basic WordPress site Writer WordPress Visitor Headless WordPress site Writer WordPress Next.js app Visitor

58.

Basic WordPress site Writer WordPress Visitor Headless WordPress site Writer WordPress Next.js app Visitor

59.

Basic WordPress site Writer WordPress Visitor Headless WordPress site Writer WordPress Next.js app Visitor

60.

Basic WordPress site Writer WordPress Headless WordPress site Writer WordPress Visitor If no-cache, content is not shown But page and site will not down Next.js app Visitor

61.

Difference between Cache and Headless frontend Cache (CDN / Nginx / etc..) ● ● ● Save as Static file Depends on origin behavior When Cache has expired, the page will be down. But easy and simple Headless frontend (Next.js / etc…) ● ● ● Insert the API response Can run standalone When API Cache has expired the page will be partially down ( Page is worked, but no content ) But head to build

62.

Where / Why we use WordPress as Headless CMS? 1. Omnichannel content distribution 2. Minimize the impact of server failures 3. Next generation “full-scratched site development” 4. Microservice application

63.

https://www.figma.com/blog/introducing-figma-to-react/

64.

Common Theme development Design data (psd / Figma / etc..) Static HTML WordPress Theme Design data (psd / Figma / etc..) Starter Theme /w Theme Unit test CSS / JS / PHP + Content Data https://speakerdeck.com/torounit/2023-11-05-kansai-wordpress-meetup

65.

Common Theme development Design data (psd / Figma / etc..) Static HTML WordPress Theme Design data (psd / Figma / etc..) Starter Theme /w Theme Unit test CSS / JS / PHP + Content Data https://speakerdeck.com/torounit/2023-11-05-kansai-wordpress-meetup

66.

Common Theme development Design data (psd / Figma / etc..) Static HTML WordPress Theme Does this HTML file support... ● Template tag ● Default CSS ● Default block -> How many custom block should we made? https://speakerdeck.com/torounit/2023-11-05-kansai-wordpress-meetup

67.

https://astro.build/

68.
[beta]
--import { loadWPPosts } from '@libs/wordpress';
import Card from '@ui/Card.astro';
import CardTitle from '@ui/CardTitle.astro';
const posts = await loadWPPosts()
---

<div class="flex flex-col gap-16">
{posts.map((article) => (
<Card class="md:col-span-3">
<CardTitle href={`${article.href}`}>
{article.title}
</CardTitle>
<CardCta>Read article</CardCta>
</Card>
))}
</div>

Similar coding style
to WordPress theme

69.

Block theme development Design data (psd / Figma / etc..) Starter Theme /w Theme Unit test CSS / JS / PHP + Content Data HTML oriented development Design data (psd / Figma / etc..) Static HTML Astro Static Site https://speakerdeck.com/torounit/2023-11-05-kansai-wordpress-meetup

70.

Where / Why we use WordPress as Headless CMS? 1. Omnichannel content distribution 2. Minimize the impact of server failures 3. Next generation “full-scratched site development” 4. Microservice application

71.

Recap: CQRS for WordPress Writer Full text search Custom search Vector store Recommend NoSQL High traffic RDBMS Relational data WordPress wp-admin Hook Webhook

72.

Team structure oriented architecture ( Conway's law ) Marketer Marketing Content Service Client / Customer Testimonial Service Developer Developer Guide Service WordPress Sanity microCMS Document Generator They can update their Content Service anytime they want

73.

Team structure oriented architecture ( Conway's law ) Marketer Marketing Content Service Client / Customer Testimonial Service Developer Developer Guide Service WordPress They can update their Content Service anytime they want Sanity microCMS Document Generator => Each team need to get staff and resource for dev their system.

74.

Lite weight example - Blog: WordPress Product/Books: microCMS OSS: npm/WordPress.org Bio/profile: Gravatar and Frontend is Astro https://hidetaka.dev

75.

Important things

76.

From “State of the Word 2015” by Matt Mullenweg https://wordpress.tv/2015/12/07/matt-mullenweg-state-of-the-word-2015/

77.

Should NOT use Headless WordPress 1. Small budget project 2. No JavaScript experts 3. Early stage Startup 4. Small development / operating team 5. Have a strong WordPress experts

78.

Should NOT choose Headless WordPress 1. Small budget project 2. No JavaScript experts 3. Early stage Startup Try Block theme at first 4. Small development / operating team 5. Have a strong WordPress experts And focus on your main business

79.

Conclution ● I finally submitted my homework "Learn JavaScript deeply" for 9 years ago ● Headless WP may help you for using your content more effective ○ ● Omnichannel / Reduce server down effect / CQRS / microservice architecture Until clear the benefit of Headless WP, choose Block theme at first.

80.

One more things

81.

Learning new technology has given me new horizons.

82.

Themes Plugins WordPress Writing more…

83.

Themes Plugins WordPress Writing more… WP API

84.

Themes Next.js Astro Plugins Microservice WordPress WP API Writing idp / payment more… LLM / RAG

85.

“Connecting the dots” - Steve Jobs https://www.youtube.com/watch?v=UF8uR6Z6KLc&t=1s

86.

Cat React Plugins Trip Block Tea Blog Next.js “Connecting the dots” WordPress WP API LLM AWS - Steve Jobs Event Sourcing Themes Vector Store micro services https://www.youtube.com/watch?v=UF8uR6Z6KLc&t=1s

87.

Thank you! - x.com/@hidetaka_dev - wp.org/hideokamoto - hidetaka.dev