{"id":11485,"date":"2024-02-29T03:58:08","date_gmt":"2024-02-29T03:58:08","guid":{"rendered":"https:\/\/myprojects.advchaweb.com\/?p=11485"},"modified":"2024-02-29T04:47:59","modified_gmt":"2024-02-29T04:47:59","slug":"wordpress-headless-with-vercel","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2024\/02\/29\/wordpress-headless-with-vercel\/","title":{"rendered":"WordPress Headless with Vercel"},"content":{"rendered":"<p>Create a vercel account if you don&#8217;t have any.<br \/>\nThen create a new project with CMS WordPress<br \/>\nConnect it with your github<br \/>\nIt&#8217;d create the project like this: https:\/\/github.com\/advcha\/isr-blog-nextjs-wordpress<br \/>\nA WordPress backend with NextJS frontend<br \/>\nRead the github project README<\/p>\n<p>PREPARE THE WP BACKEND<br \/>\nPrepare your local WP site. Mine is http:\/\/wp-commerce.test\/<br \/>\nThen on the WP admin, install a new plugin WPGraphQL<br \/>\nThen on GraphQL settings, look at the GraphQL Endpoint value. The default value is http:\/\/wp-commerce.test\/graphql<br \/>\nTHIS WOULD BE THE &#8216;WORDPRESS_API_URL&#8217; VALUE<\/p>\n<p>PREPARE THE NEXTJS FRONTEND<br \/>\nClone the above github (https:\/\/github.com\/advcha\/isr-blog-nextjs-wordpress) to your computer<br \/>\nThen copy .env.local.example to .env.local<br \/>\nModify WORDPRESS_API_URL value TO http:\/\/wp-commerce.test\/<br \/>\nAlso need to modify next.config.js file TO add a domain &#8216;0.gravatar.com&#8217; like this<\/p>\n<pre class=\"lang:default decode:true\">module.exports = {\r\n  images: {\r\n    remotePatterns: [\r\n      {\r\n        protocol: protocol.slice(0, -1),\r\n        hostname,\r\n        port,\r\n        pathname: `${pathname}\/**`,\r\n      },\r\n    ],\r\n    domains: ['0.gravatar.com','secure.gravatar.com'],\r\n  },\r\n};<\/pre>\n<p>I need to do this (add the domain for showing up the images above), because previously I got this error on the frontend<\/p>\n<pre class=\"lang:default decode:true \">Server Error\r\nError: Invalid src prop (http:\/\/0.gravatar.com\/avatar\/033d77f8305477da85f8eaac6c8a600d?s=96&amp;d=mm&amp;r=g) on `next\/image`, hostname \"0.gravatar.com\" is not configured under images in your `next.config.js`\r\nSee more info: https:\/\/nextjs.org\/docs\/messages\/next-image-unconfigured-host<\/pre>\n<p>Also I need to update my &#8216;node&#8217; to &gt; 18&#8230; version. I update it to version 20&#8230;<br \/>\nRun the npm or yarn on the project root (\/home\/satria\/Documents\/projects\/isr-blog-nextjs-wordpress\/). I prefer yarn<\/p>\n<pre class=\"lang:default decode:true\">yarn install\r\nyarn dev<\/pre>\n<p>If there is no error, open it on the browser: http:\/\/localhost:3000\/<\/p>\n<p>DEPLOY IT VIA VERCEL<br \/>\nLogin to your vercel account then go to your project. Mine is isr-blog-nextjs-wordpress<br \/>\nThen go to Settings -&gt; Environment Variables -&gt; Create a new one &#8216;WORDPRESS_API_URL&#8217; or modify it<br \/>\nThe value should be http:\/\/wp-commerce.test\/graphql<br \/>\nThen Save<\/p>\n<p>Push the changes on the github. but when you want to deploy on the vercel (https:\/\/vercel.com\/satria-faesthas-projects\/isr-blog-nextjs-wordpress\/deployments), you&#8217;d get this error and deploy is failed<\/p>\n<pre class=\"lang:default decode:true\">Add to your environment variables WORDPRESS_API_URL.<\/pre>\n<p>Make sure the variable WORDPRESS_API_URL value in .env.local file is same with in the vercel project setting.<br \/>\nThat&#8217;s because my WordPress is still on my local computer. So use your live WordPress site and install again WPGraphQL plugin !<br \/>\nAnother error on build deploy via vercel<\/p>\n<pre class=\"lang:default decode:true \">SyntaxError: Unexpected token &lt; in JSON at position 0<\/pre>\n<p>THIS IS BECAUSE THE NEXT JS (FRONTEND) NOT LIVE YET?<br \/>\nREAD: https:\/\/github.com\/vercel\/next.js\/issues\/47554<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a vercel account if you don&#8217;t have any. Then create a new project with CMS WordPress Connect it with your github It&#8217;d create the project like this: https:\/\/github.com\/advcha\/isr-blog-nextjs-wordpress A WordPress backend with NextJS frontend Read the github project README PREPARE THE WP BACKEND Prepare your local WP site. Mine is http:\/\/wp-commerce.test\/ Then on the &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2024\/02\/29\/wordpress-headless-with-vercel\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;WordPress Headless with Vercel&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60,107],"tags":[],"class_list":["post-11485","post","type-post","status-publish","format-standard","hentry","category-node","category-wordpress-2"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/11485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=11485"}],"version-history":[{"count":7,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/11485\/revisions"}],"predecessor-version":[{"id":11492,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/11485\/revisions\/11492"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=11485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=11485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=11485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}