Skip to content

Commit

Permalink
✔ Fix Site Map 🥰
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Jul 7, 2023
1 parent a2b8cf7 commit ceeed32
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1688711343460,
"timestamp": 1688722551831,
"index": "/index.html",
"assetGroups": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

69 changes: 36 additions & 33 deletions src/api/scheduler/sitemap-tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export class SitemapService {
untrackedUrlsListFansub = [];
untrackedUrlsListBerkas = [];

urlsListToRemoveBerkas = [];

xmlOpt = {
compact: true,
spaces: 2
Expand All @@ -43,37 +41,52 @@ export class SitemapService {
async getNewsUrl(): Promise<void> {
const news = await this.newsRepo.find({
order: {
created_at: 'ASC'
created_at: 'DESC'
}
});
for (const n of news) {
this.untrackedUrlsListNews.push(`${environment.baseUrl}/news/${n.id}`);
this.untrackedUrlsListNews.push({
url: `${environment.baseUrl}/news/${n.id}`,
lastmod: n.updated_at
});
}
}

async getFansubUrl(): Promise<void> {
const fansub = await this.fansubRepo.find({
order: {
created_at: 'ASC'
created_at: 'DESC'
}
});
for (const f of fansub) {
this.untrackedUrlsListFansub.push(`${environment.baseUrl}/fansub/${f.slug}`);
this.untrackedUrlsListFansub.push({
url: `${environment.baseUrl}/fansub/${f.slug}`,
lastmod: f.updated_at
});
}
}

async getBerkasUrl(): Promise<void> {
const berkas = await this.berkasRepo.find({
where: [
{
private: false,
user_: {
private: false
}
}
],
order: {
created_at: 'ASC'
}
created_at: 'DESC'
},
relations: ['user_'],
take: 500
});
for (const b of berkas) {
if (b.private) {
this.urlsListToRemoveBerkas.push(`${environment.baseUrl}/berkas/${b.id}`);
} else {
this.untrackedUrlsListBerkas.push(`${environment.baseUrl}/berkas/${b.id}`);
}
this.untrackedUrlsListBerkas.push({
url: `${environment.baseUrl}/berkas/${b.id}`,
lastmod: b.updated_at
});
}
}

Expand All @@ -91,31 +104,21 @@ export class SitemapService {
await this.getFansubUrl();
await this.getBerkasUrl();
const untrackedUrlsList = [...this.untrackedUrlsListNews, ...this.untrackedUrlsListFansub, ...this.untrackedUrlsListBerkas];
const fileExist = existsSync(`${environment.viewFolder}/sitemap.xml`);
const filePath = fileExist ? '/sitemap.xml' : '/sitemap.template.xml';
const contentString = readFileSync(`${environment.viewFolder}/${filePath}`, 'utf8');
const contentString = readFileSync(`${environment.viewFolder}/sitemap.template.xml`, 'utf8');
const objString = xml2json(contentString, this.xmlOpt);
const existingSitemapList = JSON.parse(objString);
for (const u of untrackedUrlsList) {
if (!existingSitemapList.urlset.url.find((e) => e.loc._text === u)) {
existingSitemapList.urlset.url.push({
loc: {
_text: u
},
lastmod: {
_text: new Date().toISOString()
}
});
}
}
for (const u of this.urlsListToRemoveBerkas) {
const index = existingSitemapList.urlset.url.findIndex((e) => e.loc._text === u);
if (index >= 0) {
existingSitemapList.urlset.url.splice(index, 1);
}
existingSitemapList.urlset.url.push({
loc: {
_text: u.url
},
lastmod: {
_text: new Date(u.lastmod).toISOString()
}
});
}
const xmlString = json2xml(existingSitemapList, this.xmlOpt);
if (fileExist) {
if (existsSync(`${environment.viewFolder}/sitemap.xml`)) {
unlinkSync(`${environment.viewFolder}/sitemap.xml`);
}
writeFileSync(`${environment.viewFolder}/sitemap.xml`, xmlString, 'utf8');
Expand Down

0 comments on commit ceeed32

Please sign in to comment.