-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #620
- Loading branch information
Showing
7 changed files
with
80 additions
and
37 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ORIGIN } from '$env/static/private'; | ||
import { error } from '@sveltejs/kit'; | ||
import { PostsSchema } from '$lib/schemas/content'; | ||
|
||
/** | ||
* Create page string for sitemap. | ||
* @param {string} path | ||
* @param {string} [lastmod] | ||
*/ | ||
function createEntry(path, lastmod) { | ||
return ` | ||
<url> | ||
<loc>${new URL(path, ORIGIN).href}</loc> | ||
${lastmod ? `<lastmod>${lastmod}</lastmod>` : ''} | ||
</url> | ||
`; | ||
} | ||
|
||
/** @type {import('./$types').RequestHandler} */ | ||
export async function GET({ fetch }) { | ||
// Create entries for posts. | ||
const response = await fetch('/api/posts', { | ||
method: 'POST', | ||
body: JSON.stringify({}) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw error(500, 'Failed to fetch posts.'); | ||
} | ||
|
||
const result = PostsSchema.safeParse(await response.json()); | ||
|
||
if (!result.success) { | ||
throw error(500, 'Posts failed validation.'); | ||
} | ||
|
||
const posts = result.data.map((post) => createEntry(`/posts/${post.slug}`, post.modified)); | ||
|
||
// Add additional entries to this array. | ||
const pages = [...posts]; | ||
|
||
const sitemap = ` | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${pages.join('\n')} | ||
</urlset> | ||
`; | ||
|
||
return new Response(sitemap, { | ||
headers: { | ||
'Content-Type': 'application/xml' | ||
} | ||
}); | ||
} |
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
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