diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pages/api/shop/searchkey.js | 9 | ||||
| -rw-r--r-- | src/pages/sitemap/searchkey.xml.js | 37 | ||||
| -rw-r--r-- | src/pages/sitemap/searchkey/[page].js | 42 |
3 files changed, 87 insertions, 1 deletions
diff --git a/src/pages/api/shop/searchkey.js b/src/pages/api/shop/searchkey.js index e8b535e7..2735e72c 100644 --- a/src/pages/api/shop/searchkey.js +++ b/src/pages/api/shop/searchkey.js @@ -3,11 +3,18 @@ import axios from 'axios'; export default async function handler(req, res) { const { url = '', page = 1, limit = 30 } = req.query; + let q = '*:*'; + + if (!req.query.all) { + const url = (req.query.q || '').trim(); + q = `keywords_s:"${url}"`; + } let offset = (page - 1) * limit; const params = [ `q.op=AND`, - `q=keywords_s:"${url}"`, + // `q=keywords_s:"${url}"`, + `q=${q}`, `indent=true`, `rows=${limit}`, `start=${offset}`, diff --git a/src/pages/sitemap/searchkey.xml.js b/src/pages/sitemap/searchkey.xml.js new file mode 100644 index 00000000..488337d3 --- /dev/null +++ b/src/pages/sitemap/searchkey.xml.js @@ -0,0 +1,37 @@ +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/searchkey'; + const limit = 500; + const keywords = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?limit=${limit}&all=1` + ); + // console.log(keywords); + const pageCount = Math.ceil(keywords.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(); + // const date = '2025-10-30'; + 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/searchkey/[page].js b/src/pages/sitemap/searchkey/[page].js new file mode 100644 index 00000000..cee53f5e --- /dev/null +++ b/src/pages/sitemap/searchkey/[page].js @@ -0,0 +1,42 @@ +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 keywords = await axios( + `${ + process.env.NEXT_PUBLIC_SELF_HOST + }/api/shop/searchkey?limit=${limit}&page=${page.replace('.xml', '')}&all=1` + ); + + const sitemap = create('urlset', { encoding: 'utf-8' }).att( + 'xmlns', + 'http://www.sitemaps.org/schemas/sitemap/0.9' + ); + + const date = new Date(); + // const date = '2025-10-30'; + keywords.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; +} |
