Skip to content

Commit

Permalink
feat(sitemap): sitemap posts and tags is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
yussan committed Sep 2, 2023
1 parent 0e8cf9e commit 7bf8e19
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 39 deletions.
5 changes: 2 additions & 3 deletions components/commons/cards/CardPost/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<q-card class="q-mb-lg" flat bordered>
<NuxtLink :to="`/post/${toSlug(`${data.nospace_title} ${data._id}`)}`">
<NuxtLink :to="`/post/${`${data.nospace_title}-${data._id}`}`">
<q-img height="200px" :alt="data.title" :src="data.image['600']" />
</NuxtLink>
<q-card-section>
Expand Down Expand Up @@ -57,7 +57,7 @@
</div>
</template>
<script>
import { toSlug, stripTags, truncate } from "@helpers/stringManager";
import { stripTags, truncate } from "@helpers/stringManager";
import dayJS from "@helpers/dateTime";
export default {
Expand All @@ -73,7 +73,6 @@ export default {
methods: {
stripTags,
truncate,
toSlug,
dayJS,
},
};
Expand Down
6 changes: 3 additions & 3 deletions components/commons/lists/ListPostsSidebar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<q-list>
<q-item v-for="n in data.result" class="no-padding q-mb-lg">
<q-item-section>
<NuxtLink :to="`/post/${toSlug(`${n.nospace_title} ${n._id}`)}`">
<NuxtLink :to="`/post/${`${n.nospace_title}-${n._id}`}`">
<q-item-label class="text-bold">{{ n.title }}</q-item-label>
<q-item-label caption lines="2"
>{{ truncate(stripTags(n.content), 150, "...") }}.</q-item-label
Expand All @@ -12,7 +12,7 @@
</q-item-section>

<q-item-section side top>
<NuxtLink :to="`/post/${toSlug(`${n.nospace_title} ${n._id}`)}`">
<NuxtLink :to="`/post/${`${n.nospace_title}-${n._id}`}`">
<q-item-label caption>{{
dayJS(n.updated_on * 1000).fromNow()
}}</q-item-label>
Expand All @@ -29,7 +29,7 @@
import Spinner from "@components/commons/loaders/GlobalSpiner";
// helpers
import { toSlug, stripTags, truncate } from "@helpers/stringManager";
import { stripTags, truncate } from "@helpers/stringManager";
import dayJS from "@helpers/dateTime";
// define props
Expand Down
11 changes: 5 additions & 6 deletions helpers/XMLGenerator/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const { MCDG_WEB_HOST } = useRuntimeConfig().public;

export const sitemapGenerator = (links = []) => {
const sitemaps = "";
links.map((n) => {
export const sitemapGenerator = (data = []) => {
let sitemaps = "";
data.map((n) => {
sitemaps += `<url>
<loc>${MCDG_WEB_HOST}${n.link}</loc>
<loc>${MCDG_WEB_HOST}${n.path}</loc>
<lastmod>${n.lastmod || "2023-10-01"}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>`;
});
return sitemaps;
return `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${sitemaps}</urlset>`;
};
2 changes: 1 addition & 1 deletion helpers/clientApiCaller/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SealMiddleware from "@helpers/sealMiddleware";
import SealMiddleware from "../../helpers/sealMiddleware";
import { baseApiCaller } from "../baseApiCaller";
import { RESPONSE_GENERAL_ERROR } from "~/consts/responses/responses";

Expand Down
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default defineNuxtConfig({
runtimeConfig: {
public: {
APP_KEY: process.env.APP_KEY,
MCDG_WEB_HOST: process.env.MCDG_WEB_HOST,
},
},
});
34 changes: 12 additions & 22 deletions server/api/[...endpoint]/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import SealMiddleware from "../../../helpers/sealMiddleware";
// import SealMiddleware from "../../../helpers/sealMiddleware";
import { objToQuery } from "../../../helpers/stringManager";
import { serverApiCaller } from "../../../helpers/serverApiCaller";

const Seal = new SealMiddleware(process.env.APP_KEY, 10000);
// const Seal = new SealMiddleware(process.env.APP_KEY, 10000);

export default defineEventHandler(async (event) => {
const headers = getRequestHeaders(event);
const { seal } = headers;
const { is_valid } = Seal.validate(seal || "-");

if (process.env.NODE_ENV !== "development" && !is_valid) {
// res.setHeader("Content-Type", "application/json");
const responseJSON = { status: "403", message: "No access here" };
return responseJSON;
} else {
const headers = getRequestHeaders(event);

const { seal } = headers;
const query = getQuery(event) || {};
const endpoint = getRouterParams(event, "endpoint") || {};
const reqParams = {
baseURL: process.env.MCDG_BE_HOST,
endpoint: `/api/${endpoint.endpoint}?${objToQuery(query)}`,
headers: { seal },
};
const Res = await serverApiCaller(reqParams);
return Res;
}
const { seal } = headers;
const query = getQuery(event) || {};
const endpoint = getRouterParams(event, "endpoint") || {};
const reqParams = {
baseURL: process.env.MCDG_BE_HOST,
endpoint: `/api/${endpoint.endpoint}?${objToQuery(query)}`,
headers: { seal },
};
const Res = await serverApiCaller(reqParams);
return Res;
});
29 changes: 25 additions & 4 deletions server/api/sitemap/posts/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
// import { sitemapGenerator } from "../../../helpers/XMLGenerator";
import { sitemapGenerator } from "../../../../helpers/XMLGenerator";
import { sealGenerator } from "../../../../helpers/clientApiCaller";
import { serverApiCaller } from "../../../../helpers/serverApiCaller";

export default defineEventHandler(async (event) => {
const params = [{ link: "/232" }, { link: "/232" }, { link: "/122" }];
// return sitemapGenerator(params);
return params;
const params = [];

// start req to api
const reqParams = {
baseURL: process.env.MCDG_BE_HOST,
endpoint: `/api/posts?limit=50&draft=false`,
headers: { seal: sealGenerator() },
};
const Res = await serverApiCaller(reqParams);

if (Res.status === 200) {
console.log("results", Res.result);
Res.result.map((n) =>
params.push({
path: `/post/${n.nospace_title}-${n._id}`,
})
);
}

// ref: https://dev.to/rafaelmagalhaes/creating-rss-feed-with-nuxt-3-5oj
event.node.res.setHeader("content-type", "text/xml"); // we need to tell nitro to return this as a xml file
event.node.res.end(sitemapGenerator(params)); // send the HTTP response
});
25 changes: 25 additions & 0 deletions server/api/sitemap/tags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { sitemapGenerator } from "../../../../helpers/XMLGenerator";

const tags = [
"reactjs",
"vuejs",
"go",
"javascript",
"flask",
"echo",
"python",
"nuxtjs",
"express",
];

export default defineEventHandler(async (event) => {
const params = [];

tags.map((n) => {
params.push({ path: `/tag/${n}` });
});

// ref: https://dev.to/rafaelmagalhaes/creating-rss-feed-with-nuxt-3-5oj
event.node.res.setHeader("content-type", "text/xml"); // we need to tell nitro to return this as a xml file
event.node.res.end(sitemapGenerator(params)); // send the HTTP response
});

0 comments on commit 7bf8e19

Please sign in to comment.