কম্পিউটার টিউটোরিয়াল

Next.js (ReactJS)-এ JSON-LD ডেটা রেন্ডার করার পদ্ধতি

Next.js এবং অন্যান্য ReactJS প্রজেক্টে JSON-LD (application/ld+json) স্কিমা কিভাবে কাজ করাবে তা শিখুন।

Next.js (বা যেকোনো React অ্যাপ) এ JSON-LD ডেটা রেন্ডার করতে আপনাকে নিচের موارد ব্যবহার করতে হবে:

  • <script> এলিমেন্ট।
  • dangerouslySetInnerHTML অ্যাট্রিবিউট।
  • JSON.stringify মেথড (সিরিয়ালাইজেশনের জন্য)।

JSON-LD অর্থ JavaScript Object Notation for Linked Data। এটি Schema.org ডেটা সার্চ ইঞ্জিনকে আপনার ওয়েবসাইটের কন্টেন্ট সম্পর্কে জানাতে একটি হালকা পদ্ধতি।

একটি উদাহরণ দেখি।

মান লইয়া আপনার একটি HTML JSON-LD স্কিমা আছে যেটির কাঠামো নিচের মতো:

<script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "Product", "name": "Name of service", "image": "https://somewebsite.com/static/images/some-image.jpg", "description": " I seek the means to fight injustice. To turn fear against those who prey on the fearful. Someone like you. Someone who'll rattle the cages. My anger outweighs my guilt.", "brand": "Company Name", "review": { "@type": "Review", "name": "Company Name ", "reviewBody": "It was a dog. It was a big dog. It's not who I am underneath but what I do that defines me. Well, you see... I'm buying this hotel and setting some new rules about the pool area.", "reviewRating": { "@type": "Rating", "ratingValue": "5" }, "datePublished": "2020-04-06", "author": {"@type": "Person", "name": "Emma"} } } </script>

Next.js-এ এই JSON-LD স্কিমা কাজ করাতে, প্রথমে HTML <script> ট্যাগগুলো সরিয়ে ফেলুন যাতে আপনার কাছে একটি JSON-LD অবজেক্ট থাকে।

তারপর JSON-LD অবজেক্টটি একটি ভেরিয়েবলে এসাইন করুন, নিচের মতো:

const schemaData = { "@context": "https://schema.org/", "@type": "Product", "name": "Name of service", "image": "https://somewebsite.com/static/images/some-image.jpg", "description": "I seek the means to fight injustice. To turn fear against those who prey on the fearful. Someone like you. Someone who'll rattle the cages. My anger outweighs my guilt.", "brand": "Company Name", "review": { "@type": "Review", "name": "Company Name ", "reviewBody": "It was a dog. It was a big dog. It's not who I am underneath but what I do that defines me. Well, you see... I'm buying this hotel and setting some new rules about the pool area.", "reviewRating": { "@type": "Rating", "ratingValue": "5" }, "datePublished": "2020-04-06", "author": {"@type": "Person", "name": "Emma"} } }

এখন আপনার schemaData ভেরিয়েবলটি JSON.stringify() দিয়ে সিরিয়ালাইজ করুন:

JSON.stringify(schemaData)

শেষে JSON.stringify(schemaData) কে dangerouslySetInnerHTML অ্যাট্রিবিউটের অবজেক্ট ভ্যালু হিসেবে একটি <script> এলিমেন্টের ভিতরে যোগ করুন ব্রাউজারে রেন্ডার করতে:

<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaData) }} />

যদি বিভ্রান্ত হন, তবে নিচে সম্পূর্ণ পেজ উদাহরণ দেওয়া হলো, যা সকল Next.js/React ওয়েবসাইটে কাজ করবে:

import React from 'react'; const schemaData = { "@context": "https://schema.org/", "@type": "Product", "name": "Name of service", "image": "https://somewebsite.com/static/images/some-image.jpg", "description": "I seek the means to fight injustice. To turn fear against those who prey on the fearful. Someone like you. Someone who'll rattle the cages. My anger outweighs my guilt.", "brand": "Company Name", "review": { "@type": "Review", "name": "Company Name ", "reviewBody": "It was a dog. It was a big dog. It's not who I am underneath but what I do that defines me. Well, you see... I'm buying this hotel and setting some new rules about the pool area.", "reviewRating": { "@type": "Rating", "ratingValue": "5" }, "datePublished": "2020-04-06", "author": {"@type": "Person", "name": "Emma"} } } const SomePage = () => { return ( <> <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(structuredData) }} /> <div>Your content</div> </> ); }; export default SomePage;
  1. jQuery get() মেথড কীভাবে ব্যবহার করবেন

  2. মাইক্রোসফট এক্সেল গ্রাফ বা চার্টে ডেটা সিরিজের নাম পরিবর্তন করার পদ্ধতি

  3. মাস্টার ডেটা ব্যাকআপ: ফাইল রক্ষার প্রমাণিত কৌশল

  4. Android Data Binding 습득: সুস্পষ্ট UI ডেটা ইন্টিগ্রেশন