Skip to content

Commit

Permalink
Merge pull request #422 from sharemindteam/feat/sitemap_421
Browse files Browse the repository at this point in the history
[Feat] sitemap 작성 스크립트 구현 및 자동화
  • Loading branch information
kyuhho authored Dec 5, 2024
2 parents c6f7bcc + 5a8877b commit 2d62165
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 11 deletions.
99 changes: 99 additions & 0 deletions generateSitemap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import fs from 'fs';
import { SitemapStream } from 'sitemap';
import axios from 'axios';
import { configDotenv } from 'dotenv';

// load env file
configDotenv();

const generateSitemap = async () => {
const hostname = 'https://sharemindapp.com';

// 정적 경로
const staticRoutes = [
{ url: '/', changefreq: 'monthly', priority: 0.3 },
{ url: '/share', changefreq: 'hourly', priority: 1.0 },
{ url: '/open-consult', changefreq: 'always', priority: 1.0 },
{ url: '/open-consult/likes', changefreq: 'always', priority: 0.8 },
{ url: '/open-consult/recents', changefreq: 'always', priority: 0.8 },
{ url: '/categorySearch', changefreq: 'daily', priority: 0.7 },
{ url: '/service', changefreq: 'never', priority: 0.8 },
{ url: '/service-unavailable', changefreq: 'monthly', priority: 0.5 },
];

// 동적 경로
const dynamicRoutes = [];

try {
const counselorAllResponse = await axios.patch(
process.env.REACT_APP_API_URL + 'counselors/all?sortType=POPULARITY',
{
index: 0,
},
{
headers: {
'Content-Type': 'application/json',
},
},
);

const profileDynamicRoutes = counselorAllResponse.data.map((counselor) => ({
url: `/profile/${counselor.counselorId}`,
changefreq: 'daily',
priority: 0.7,
}));

dynamicRoutes.push(...profileDynamicRoutes);
} catch (error) {
console.log(error);
}

try {
const likesResponse = await axios.get(
process.env.REACT_APP_API_URL +
'posts/customers/public/likes?postId=0&finishedAt=' +
new Date().toISOString().slice(0, 19),
{
headers: {
'Content-Type': 'application/json',
},
},
);

const likesDynamicRoutes = likesResponse.data.map((post) => ({
url: `/open-consult/${post.postId}`,
changefreq: 'daily',
priority: 0.7,
}));

dynamicRoutes.push(...likesDynamicRoutes);
} catch (error) {
console.log(error);
}

// Sitemap 생성
const sitemap = new SitemapStream({ hostname });
const writeStream = fs.createWriteStream('./public/sitemap.xml');

sitemap.pipe(writeStream);

// 정적 경로 추가
staticRoutes.forEach((route) => sitemap.write(route));

// 동적 경로 추가
dynamicRoutes.forEach((route) => sitemap.write(route));

// 종료
sitemap.end();

// 파일 쓰기 완료 이벤트
writeStream.on('finish', () => {
console.log('Sitemap successfully written to file!');
});

writeStream.on('error', (err) => {
console.error('Error writing sitemap:', err);
});
};

generateSitemap();
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"react-use": "^17.5.0",
"recoil": "^0.7.7",
"recoil-persist": "^5.1.0",
"sitemap": "^8.0.0",
"sockjs-client": "^1.6.1",
"styled-components": "^6.1.8",
"styled-normalize": "^8.1.0",
Expand All @@ -39,7 +40,7 @@
"scripts": {
"start": "env-cmd -f .env.development react-scripts start",
"start:prod": "env-cmd -f .env react-scripts start",
"build": "react-scripts build",
"build": "node generateSitemap.mjs && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Expand Down
7 changes: 7 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
/>

<!-- google search console -->
<meta
name="google-site-verification"
content="01t1KJ8zo4Pwtg4VaJOjJp4l86IS7b_HHKhX749vM7Q"
/>

<meta name="theme-color" content="#ffffff" />
<meta
name="description"
Expand Down
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

Sitemap: https://sharemindapp.com/sitemap.xml
1 change: 1 addition & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"><url><loc>https://sharemindapp.com/</loc><changefreq>monthly</changefreq><priority>0.3</priority></url><url><loc>https://sharemindapp.com/share</loc><changefreq>hourly</changefreq><priority>1.0</priority></url><url><loc>https://sharemindapp.com/open-consult</loc><changefreq>always</changefreq><priority>1.0</priority></url><url><loc>https://sharemindapp.com/open-consult/likes</loc><changefreq>always</changefreq><priority>0.8</priority></url><url><loc>https://sharemindapp.com/open-consult/recents</loc><changefreq>always</changefreq><priority>0.8</priority></url><url><loc>https://sharemindapp.com/categorySearch</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/service</loc><changefreq>never</changefreq><priority>0.8</priority></url><url><loc>https://sharemindapp.com/service-unavailable</loc><changefreq>monthly</changefreq><priority>0.5</priority></url><url><loc>https://sharemindapp.com/profile/34</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/profile/38</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/profile/43</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/profile/73</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/open-consult/2</loc><changefreq>daily</changefreq><priority>0.7</priority></url><url><loc>https://sharemindapp.com/open-consult/1</loc><changefreq>daily</changefreq><priority>0.7</priority></url></urlset>
6 changes: 3 additions & 3 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ import SellerCalculateManagement from 'pages/Seller/SellerCalculateManagement';
const Router = () => {
return (
<Routes>
{/* admin */}
<Route
{/* Moved to Admin Page ✅ */}
{/* <Route
path={`/${process.env.REACT_APP_ADMIN_UUID_URL}`}
element={<Admin />}
/>
/> */}
{/* minder(buyer) */}
<Route path="/share" element={<BuyerHome />} />
<Route path="/" element={<Navigate to="/share" />} />
Expand Down
8 changes: 7 additions & 1 deletion src/api/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ export const patchAuthSignOut = async (body: any) =>

//Counselor Controller
//카테고리/들준마 상담사 리스트 반환
export const patchCounselorsAllDeprecated = async (
sortType: string,
body: any,
) => await patchPublicInstance(`/counselors/all?sortType=${sortType}`, body);

export const patchCounselorsAll = async (sortType: string, body: any) =>
await patchPublicInstance(`/counselors/all?sortType=${sortType}`, body);
await axiosPatch(`/counselors/all?sortType=${sortType}`, body);

//LetterMessage Controller
//Message 최초 생성
export const patchLetterMessage = async (body: any) =>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Buyer/BuyerAvailCounselor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { sortList } from 'utils/constant';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { isSortModalOpenState, scrollLockState } from 'utils/atom';
import { SearchResultData } from 'utils/type';
import { patchCounselorsAll } from 'api/patch';
import { patchCounselorsAllDeprecated } from 'api/patch';
import { ConverSortType } from 'utils/convertSortType';
import { AvailCounselorSearchResults } from 'components/Buyer/BuyerAvailCounselor/AvailCounselorSearchResult';
import useIntersectionObserver from 'hooks/useIntersectionObserver';
Expand Down Expand Up @@ -61,7 +61,7 @@ export const BuyerAvailCounselor = () => {
index: pageIndex,
};
const sortTypeString: string = ConverSortType(sortType);
const res: any = await patchCounselorsAll(sortTypeString, body);
const res: any = await patchCounselorsAllDeprecated(sortTypeString, body);
if (res.status === 200) {
if (res.data.length !== 0) {
if (pageIndex === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Buyer/BuyerCategoryResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
searchKeywordState,
} from 'utils/atom';
import { ConverSortType } from 'utils/convertSortType';
import { patchCounselorsAll } from 'api/patch';
import { patchCounselorsAllDeprecated } from 'api/patch';
import { SearchResultData } from 'utils/type';

import { CategorySearchResults } from 'components/Buyer/BuyerCategoryResult/CategorySearchResult';
Expand Down Expand Up @@ -68,7 +68,7 @@ export const BuyerCategoryResult = () => {
index: pageIndex,
};
const sortTypeString: string = ConverSortType(sortType);
const res: any = await patchCounselorsAll(sortTypeString, body);
const res: any = await patchCounselorsAllDeprecated(sortTypeString, body);
if (res.status === 200) {
if (res.data.length !== 0) {
if (pageIndex === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Buyer/BuyerHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import styled from 'styled-components';
import HomeAboutFooterSection from 'components/Common/HomeAboutFooterSection';
import { SearchResultData } from 'utils/type';
import { useEffect, useState } from 'react';
import { patchCounselorsAll } from 'api/patch';
import { patchCounselorsAllDeprecated } from 'api/patch';
import { getRandomCounselors } from 'api/get';

//
Expand All @@ -30,7 +30,7 @@ export const BuyerHome = () => {
const body = {
index: 0,
};
const res: any = await patchCounselorsAll('POPULARITY', body);
const res: any = await patchCounselorsAllDeprecated('POPULARITY', body);
if (res.status === 200) {
if (res.data.length > 0) {
setSearchData(res.data);
Expand Down

0 comments on commit 2d62165

Please sign in to comment.