Skip to content

Commit

Permalink
sitemap 생성시 axios가 아닌 fetch 사용 (10.07)
Browse files Browse the repository at this point in the history
sitemap 생성시 axios가 아닌 fetch 사용 (10.07)
  • Loading branch information
seoko97 authored Oct 6, 2023
2 parents 0514d2b + 55fa48a commit f122737
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/app/about/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { siteMetadata } from "@utils/constant/metadata";

export const generateMetadata = (): Metadata => {
const { openGraph, ...rest } = generateDefaultMetadata({
title: "About",
title: "안녕하세요! 개발자 지석호입니다.",
description: siteMetadata.description,
url: `${siteMetadata.siteUrl}/about`,
});
Expand Down
39 changes: 25 additions & 14 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { HOST } from "@utils/constant/env";
import { getPosts } from "@/apis/post";
import { MetadataRoute } from "next";

const ROUTES = ["", "/project", "/series", "/about"];
import { API_URL, HOST } from "@utils/constant/env";
import { IPost } from "@/types";

const sitemap = async () => {
const posts = (await getPosts({ sort: 1, limit: 9999 })).map(({ nid, createdAt }) => ({
url: `${HOST}/post/${nid}`,
lastModified: createdAt,
}));
async function sitemap(): Promise<MetadataRoute.Sitemap> {
const query = new URLSearchParams({ sort: "1", limit: "9999" }).toString();
const url = `${API_URL}/posts?${query}`;

const routes = ROUTES.map((route) => ({
url: `${HOST}${route}`,
lastModified: new Date().toISOString(),
}));
try {
const res = await fetch(url, { method: "GET", cache: "no-store" });

return [...routes, ...posts];
};
const posts = (await res.json()) as IPost[];

const postsSiteMap = posts.map(({ nid, createdAt }) => ({
url: `${HOST}/post/${nid}`,
lastModified: createdAt,
}));

const routes = ["", "/project", "/series", "/about"].map((route) => ({
url: `${HOST}${route}`,
lastModified: new Date().toISOString(),
}));

return [...routes, ...postsSiteMap];
} catch (e) {
return [];
}
}

export default sitemap;
2 changes: 1 addition & 1 deletion src/utils/constant/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const isProd = process.env.NODE_ENV === "production";

const TOKEN_EXPIRED = "jwt expired";
const API_URL = process.env.API_URL as string;
const API_URL = process.env.NEXT_PUBLIC_API_URL as string;
const HOST = process.env.HOST as string;
const GOOGLE_SITE_VERIFICATION = isProd ? process.env.GOOGLE_SITE_VERIFICATION : "";
const GA_TRACKING_ID = isProd ? process.env.NEXT_PUBLIC_GA_ID : "";
Expand Down

0 comments on commit f122737

Please sign in to comment.