-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sitemap 생성시 axios가 아닌 fetch 사용 (10.07)
sitemap 생성시 axios가 아닌 fetch 사용 (10.07)
- Loading branch information
Showing
3 changed files
with
27 additions
and
16 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
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; |
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