当在image:loc中创建网站Map时,当我查看xml时,我在浏览器中将loc设置为undefined。我需要显示博客中的图像。此外,当我console.log时,链接显示在终端中,但在浏览器中显示为undefined。
另外,现在在代码中,我刚刚在loc中放置了一个字符串,只是为了查看结果。一旦这个loc在浏览器中正确显示,我将更改它。
注意事项:我也试过使用自定义的Transform函数(next-sitemap. js,它来自于next-sitemap库,但它仍然有相同的结果,显示image:loc为undefined。
博客网站Mapenter image description here
Blogs对象:(需要数据对象中显示的图像)
{
status: 200,
data: [
{
_id: '6511c2cbc3ae7d1923a19a52',
slug: 'buying-gold-in-toronto:-tips-for-local-investors',
status: 'Publish',
meta: [Object],
image: 'Chapters_GTA-Page.png',
title: 'Buying Gold in Toronto: Tips for Local AU Bullion',
categories: [Array],
createdAt: '2023-09-25T17:26:35.580Z'
},
{
_id: '6511d646c3ae7d1923a19f5f',
slug: 'the-2024-1-oz-gold-kangaroo-coin-from-the-perth-mint',
status: 'Publish',
meta: [Object],
image: '024-kangaroo-gold-coin.jpeg',
title: 'The 2024 1 Oz Gold Kangaroo Coin from The Perth Mint',
categories: [Array],
createdAt: '2023-09-25T18:49:42.943Z'
},
],
totalBlogs: 3
}
字符串
Index.js
import { getServerSideSitemapLegacy } from "next-sitemap";
import { GetServerSideProps } from "next";
import axiosInstance from "../../helper";
import { siteUrl, transform } from "../../next-sitemap.config";
export const getServerSideProps = async (ctx) => {
const response = await fetch(
`${axiosInstance.defaults.baseURL}/getallblogs`,
{ next: { revalidate: 3600 } }
);
const blogs = await response.json();
console.log(blogs);
const fields = blogs.data.map(({ slug }) => {
return {
loc: `${siteUrl}/blogs/${slug}`,
lastmod: new Date().toISOString(),
images: [{ loc: "image.jpg" }],
};
});
return getServerSideSitemapLegacy(ctx, fields);
};
// Default export to prevent next.js errors
export default function Sitemap() {}
型
next-sitemap.config.js
// const siteUrl = "http://localhost:3000";
module.exports = {
siteUrl: process.env.SITE_URL || "http://localhost:3000",
generateRobotsTxt: true,
sitemapSize: 7000,
exclude: [
"/blogs-sitemap.xml",
"/products-sitemap.xml",
"/checkout",
"/cart",
"/account",
"/orderplaced",
"/profile",
],
transform: async (config, path) => {
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
images: [{ loc: "image.jpg" }],
};
},
sitemapSize: 7000,
robotsTxtOptions: {
policies: [
{
userAgent: "*",
allow: "/",
},
{
userAgent: "test-bot",
allow: ["/path", "/path-2"],
},
{
userAgent: "black-listed-bot",
disallow: ["/sub-path-1", "/path-2"],
},
],
},
};
型
你能让我知道如何让图像正确显示在image:loc in sitemap中吗
1条答案
按热度按时间cvxl0en21#
试试这个:
字符串
Next Sitemap Sitemap Builder Source Code