-
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.
- Loading branch information
1 parent
44c1dc2
commit 78b8f06
Showing
2 changed files
with
52 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { APIRoute } from 'astro'; | ||
import { handleUnexpectedError, json } from './api/_utils'; | ||
import { buildSitemapUrls as cmaResourceEndpointUrls } from './docs/content-management-api/resources/[entitySlug]/[endpointRel]/_graphql'; | ||
import { buildSitemapUrls as cmaResourceUrls } from './docs/content-management-api/resources/[entitySlug]/_graphql'; | ||
|
||
const allAstroFiles = import.meta.glob<string>('../pages/**/*.astro', { | ||
query: '?raw', | ||
import: 'default', | ||
eager: false, | ||
}); | ||
|
||
export type BuildSitemapUrlsFn = (ctx: { | ||
request: Request; | ||
responseHeaders: Headers; | ||
}) => Promise<string[]>; | ||
|
||
export const fetchHardcodedRoutes = async () => { | ||
let urlsPromises: Array<Promise<string[]>> = []; | ||
|
||
for (const astroFilePath of Object.keys(allAstroFiles)) { | ||
if (astroFilePath.includes('_')) { | ||
continue; | ||
} | ||
|
||
if (!astroFilePath.includes('[')) { | ||
const url = astroFilePath.replace('./', '/').replace('.astro', '').replace('/index', ''); | ||
urlsPromises.push(Promise.resolve([url || '/'])); | ||
} | ||
} | ||
|
||
return (await Promise.all(urlsPromises)).flat(); | ||
}; | ||
|
||
export const GET: APIRoute = async ({ request }) => { | ||
try { | ||
const responseHeaders = new Headers(); | ||
|
||
const urls = await Promise.all([ | ||
fetchHardcodedRoutes(), | ||
cmaResourceUrls({ request, responseHeaders }), | ||
cmaResourceEndpointUrls({ request, responseHeaders }), | ||
]); | ||
|
||
return json(urls.flat(), { headers: responseHeaders }); | ||
} catch (error) { | ||
return handleUnexpectedError(request, error); | ||
} | ||
}; |