Files
webstudio/apps/builder/app/shared/$resources/sitemap.xml.server.ts
T
Oleg Isonen cc6dc6481b fix: Fix sitemap urls when using folders (#3281)
## Description

1. What is this PR about (link the issue and add a short description)

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env.example`
and the `builder/env-check.js` if mandatory
2024-05-02 14:10:44 +02:00

53 lines
1.6 KiB
TypeScript

import { json } from "@remix-run/server-runtime";
import { prisma } from "@webstudio-is/prisma-client";
import { parsePages } from "@webstudio-is/project-build/index.server";
import { getStaticSiteMapXml } from "@webstudio-is/sdk";
/**
* This should be a route in SvelteKit, as it can be fetched server-side without an actual HTTP request.
* Consider moving it to routes if Remix supports similar functionality in the future.
* Note: Fetching own routes using request.origin is prohibited on Cloudflare Workers.
* Note: We are not moving this to routes to avoid generating an additional 30MB function on deploy.
*/
export const loader = async ({ request }: { request: Request }) => {
const referer = request.headers.get("referer");
if (referer == null) {
throw json({ message: "No referer" }, { status: 400 });
}
const segments = new URL(referer).pathname.slice(1).split("/");
if (segments.length !== 2) {
throw json({ message: "Invalid referer" }, { status: 400 });
}
if (segments[0] !== "builder") {
throw json({ message: "Invalid referer" }, { status: 400 });
}
const projectId = segments[1];
// get pages from the database
const build = await prisma.build.findFirst({
where: {
projectId,
deployment: null,
},
select: {
pages: true,
updatedAt: true,
},
});
if (build === null) {
throw json({ message: "Build not found" }, { status: 404 });
}
const pages = parsePages(build.pages);
const siteMap = getStaticSiteMapXml(pages, build.updatedAt.toISOString());
return json(siteMap);
};