From bb2fc00e1f399be919d7b777dd0ae17edd6b2a2e Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Fri, 25 Oct 2024 10:04:34 +0700 Subject: sitemap categories brand --- src/pages/api/shop/url-category_brand.js | 7 +++-- src/pages/shop/find/[slug].jsx | 3 +- src/pages/sitemap/categories-brand.xml.js | 35 ++++++++++++++++++++++ src/pages/sitemap/categories-brand/[page].js | 43 ++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/pages/sitemap/categories-brand.xml.js create mode 100644 src/pages/sitemap/categories-brand/[page].js (limited to 'src/pages') diff --git a/src/pages/api/shop/url-category_brand.js b/src/pages/api/shop/url-category_brand.js index cfcc7d73..160aa166 100644 --- a/src/pages/api/shop/url-category_brand.js +++ b/src/pages/api/shop/url-category_brand.js @@ -1,9 +1,12 @@ import axios from 'axios'; export default async function handler(req, res) { - const { url = '' } = req.query; + const { url = '', page = 1, limit = 30 } = req.query; + + let offset = (page - 1) * limit; - const params = [`q.op=AND`, `q=url_s:"${url}"`, `indent=true`]; + const params = [`q.op=AND`, `q=${url ? `"${url}"` : '*'}`, `indent=true`, `rows=${limit}`, `start=${offset}`]; + try { let result = await axios( process.env.SOLR_HOST + diff --git a/src/pages/shop/find/[slug].jsx b/src/pages/shop/find/[slug].jsx index 50797153..85e780ff 100644 --- a/src/pages/shop/find/[slug].jsx +++ b/src/pages/shop/find/[slug].jsx @@ -16,11 +16,12 @@ const ProductSearch = dynamic(() => const BASE_URL = 'https://indoteknik.com'; export default function FindPage() { const route = useRouter(); + const qSlug = route?.query?.slug || null; const url = BASE_URL + route.asPath; const [result, setResult] = useState(null); const [query, setQuery] = useState(null); - const slug = getNameFromSlug( route.query.slug) +' '+ getIdFromSlug( route.query.slug) + const slug = qSlug ? getNameFromSlug(route?.query?.slug) +' '+ getIdFromSlug(route?.query?.slug) : ''; const getUrls = async (url) => { try { let response = await axios( diff --git a/src/pages/sitemap/categories-brand.xml.js b/src/pages/sitemap/categories-brand.xml.js new file mode 100644 index 00000000..b23363e9 --- /dev/null +++ b/src/pages/sitemap/categories-brand.xml.js @@ -0,0 +1,35 @@ +import productSearchApi from '@/lib/product/api/productSearchApi' +import { create } from 'xmlbuilder' +import _ from 'lodash-contrib' +import axios from 'axios' + +export async function getServerSideProps({ res }) { + const baseUrl = process.env.SELF_HOST + '/sitemap/categories-brand' + const limit = 500 + const categories = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/url-category_brand?limit=${limit}` + ) + const pageCount = Math.ceil(categories.data.response.numFound / limit) + const pages = Array.from({ length: pageCount }, (_, i) => i + 1) + const sitemapIndex = create('sitemapindex', { encoding: 'UTF-8' }).att( + 'xmlns', + 'http://www.sitemaps.org/schemas/sitemap/0.9' + ) + + const date = new Date() + pages.forEach((page) => { + const sitemap = sitemapIndex.ele('sitemap') + sitemap.ele('loc', `${baseUrl}/${page}.xml`) + sitemap.ele('lastmod', date.toISOString().slice(0, 10)) + }) + + res.setHeader('Content-Type', 'text/xml') + res.write(sitemapIndex.end()) + res.end() + + return { props: {} } +} + +export default function SitemapProducts() { + return null +} diff --git a/src/pages/sitemap/categories-brand/[page].js b/src/pages/sitemap/categories-brand/[page].js new file mode 100644 index 00000000..6b55e426 --- /dev/null +++ b/src/pages/sitemap/categories-brand/[page].js @@ -0,0 +1,43 @@ + +import productSearchApi from '@/lib/product/api/productSearchApi' +import { create } from 'xmlbuilder' +import _ from 'lodash-contrib' +import { createSlug } from '@/core/utils/slug' +import axios from 'axios' + +export async function getServerSideProps({ query, res }) { + const baseUrl = process.env.SELF_HOST + '/shop/product/' + const { page } = query + const limit = 500 + const categories = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/url-category_brand?limit=${limit}&page=${page.replace( + '.xml', + '' + )}` + ) + + const sitemap = create('urlset', { encoding: 'utf-8' }).att( + 'xmlns', + 'http://www.sitemaps.org/schemas/sitemap/0.9' + ) + + const date = new Date() + categories.data.response.docs.forEach((product) => { + const url = sitemap.ele('url') + const loc = product.url_s; + url.ele('loc', loc) + url.ele('lastmod', date.toISOString().slice(0, 10)) + url.ele('changefreq', 'daily') + url.ele('priority', '0.8') + }) + + res.setHeader('Content-Type', 'text/xml') + res.write(sitemap.end()) + res.end() + + return { props: {} } +} + +export default function SitemapProducts() { + return null +} -- cgit v1.2.3