sitemap bugfixing

This commit is contained in:
MuslemRahimi 2025-01-17 18:12:16 +01:00
parent 8ca1b417c4
commit cfa298a59d
4 changed files with 36 additions and 32 deletions

View File

@ -19,6 +19,21 @@ type FlyAndScaleParams = {
};
export function convertToSlug(title) {
// Remove punctuation, hyphens, and special characters
const cleanedTitle = title
.replace(/[-.,?!:"]/g, "") // Remove punctuation and hyphens
.replace(/\s+/g, " ") // Replace multiple spaces with a single space
.trim(); // Remove leading and trailing spaces
// Convert to lowercase, split by spaces, and join with hyphens
const words = cleanedTitle.toLowerCase().split(" ");
const truncatedWords = words;
// Join with hyphens
const slug = truncatedWords.join("-");
return slug;
}
export function isPWAInstalled() {

View File

@ -1,26 +1,10 @@
<script lang="ts">
import { numberOfUnreadNotification } from "$lib/store";
import { getImageURL } from "$lib/utils";
import { getImageURL, convertToSlug } from "$lib/utils";
export let data;
let allBlogPosts = data?.getAllBlogPost;
function convertToSlug(title) {
// Remove punctuation and special characters
const cleanedTitle = title
.replace(/[.,?!:"]/g, "") // Remove punctuation
.replace(/\s+/g, " ") // Replace multiple spaces with a single space
.trim(); // Remove leading and trailing spaces
// Convert to lowercase, split by spaces, and join with hyphens
const words = cleanedTitle.toLowerCase().split(" ");
const truncatedWords = words;
// Join with hyphens and add ellipsis if necessary
const slug = truncatedWords.join("-");
return slug;
}
</script>
<svelte:head>

View File

@ -1,15 +1,4 @@
function convertToSlug(title) {
// Remove punctuation and special characters
const cleanedTitle = title
.replace(/[.,?!:"]/g, "") // Remove punctuation
.replace(/\s+/g, " ") // Replace multiple spaces with a single space
.trim(); // Remove leading and trailing spaces
// Convert to lowercase, split by spaces, and join with hyphens
const words = cleanedTitle.toLowerCase().split(" ");
const slug = words.join("-");
return slug;
}
import { convertToSlug } from "$lib/utils";
export const load = async ({ locals, params }) => {
const { pb } = locals;

View File

@ -1,3 +1,5 @@
import { convertToSlug } from "$lib/utils";
const pages = [
{ title: "/" },
{ title: "/cramer-tracker" },
@ -66,13 +68,12 @@ const pages = [
{ title: "/analysts/top-stocks" },
];
const site = "https://stocknear.com";
const website = "https://stocknear.com";
/** @type {import('./$types').RequestHandler} */
export async function GET({ locals }) {
//get all posts;
const { apiKey, apiURL } = locals;
const { apiKey, apiURL, pb } = locals;
@ -90,7 +91,12 @@ export async function GET({ locals }) {
type: item?.type,
}));
const body = sitemap(stocks, pages);
const articles = await pb.collection("articles").getFullList({
sort: "-created",
});
const body = sitemap(stocks, articles, pages);
const response = new Response(body);
response.headers.set("Cache-Control", "max-age=0, s-maxage=3600");
response.headers.set("Content-Type", "application/xml");
@ -100,6 +106,7 @@ export async function GET({ locals }) {
// Modified sitemap function
const sitemap = (
stocks,
articles,
pages,
) => `<?xml version="1.0" encoding="UTF-8" ?>
<urlset
@ -116,6 +123,15 @@ const sitemap = (
<url>
<loc>${website}${page.title}</loc>
</url>
`,
)
.join("")}
${articles
.map(
(item) => `
<url>
<loc>${website}/blog/${convertToSlug(item?.title)}</loc>
</url>
`,
)
.join("")}