-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sitemap): sitemap posts and tags is ready
- Loading branch information
Showing
8 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |