-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sitemap): fix sitemap format and update timestamp dynamically (#24)
fix(sitemap): fix sitemap format and update timestamp dynamically
- Loading branch information
Showing
4 changed files
with
72 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* eslint-disable-next-line import/no-unresolved */ | ||
import timestamp from '../data/timestamp.json' | ||
|
||
export type Timestamp = { | ||
readonly lastUpdated: string | ||
} | ||
|
||
export const lastUpdated = (timestamp as unknown as Timestamp).lastUpdated |
This file was deleted.
Oops, something went wrong.
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,55 @@ | ||
import { categories } from '../model/categories' | ||
import { lastUpdated } from '../model/timestamp' | ||
|
||
const composeCategoryURL = ({ | ||
domain, | ||
slug, | ||
}: { | ||
domain: string | ||
slug: string | ||
}) => ` | ||
<url> | ||
<loc>${domain}/activities/${slug}</loc> | ||
<lastmod>${lastUpdated}</lastmod> | ||
<changefreq>weekly</changefreq> | ||
</url> | ||
` | ||
|
||
export const loader = async ({ request }) => { | ||
const host = | ||
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host') | ||
if (!host) { | ||
throw new Error('Could not determine domain URL.') | ||
} | ||
|
||
const protocol = host.includes('localhost') ? 'http' : 'https' | ||
const domain = `${protocol}://${host}` | ||
|
||
// TODO: updated the lastModified logic | ||
const sitemapString = ` | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>${domain}</loc> | ||
<lastmod>${lastUpdated}</lastmod> | ||
</url> | ||
<url> | ||
<loc>${domain}/tentang-kami</loc> | ||
<lastmod>${lastUpdated}</lastmod> | ||
</url> | ||
<url> | ||
<loc>${domain}/activities</loc> | ||
<lastmod>${lastUpdated}</lastmod> | ||
</url> | ||
${categories | ||
.map(({ slug }) => composeCategoryURL({ domain, slug })) | ||
.reduce((acc, curr) => acc + curr)} | ||
</urlset> | ||
`.trim() | ||
return new Response(sitemapString, { | ||
headers: { | ||
'Cache-Control': `public, max-age=${60 * 10}, s-maxage=${60 * 60 * 24}`, | ||
'Content-Type': 'application/xml', | ||
}, | ||
}) | ||
} |
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