-
Notifications
You must be signed in to change notification settings - Fork 639
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
Showing
9 changed files
with
708 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
fixtures/webstudio-remix-vercel/app/__generated__/$resources.sitemap.xml.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml]._index.server.tsx
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml]._index.tsx
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
91 changes: 68 additions & 23 deletions
91
fixtures/webstudio-remix-vercel/app/routes/[sitemap.xml]._index.tsx
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 |
---|---|---|
@@ -1,34 +1,79 @@ | ||
import type { LoaderFunctionArgs } from "@remix-run/server-runtime"; | ||
import { renderToString } from "react-dom/server"; | ||
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime"; | ||
import { isLocalResource, loadResources } from "@webstudio-is/sdk"; | ||
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime"; | ||
import { Page } from "../__generated__/[sitemap.xml]._index"; | ||
import { | ||
getPageMeta, | ||
getRemixParams, | ||
getResources, | ||
} from "../__generated__/[sitemap.xml]._index.server"; | ||
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs"; | ||
import { sitemap } from "../__generated__/$resources.sitemap.xml"; | ||
|
||
export const loader = (arg: LoaderFunctionArgs) => { | ||
const customFetch: typeof fetch = (input, init) => { | ||
if (typeof input !== "string") { | ||
return fetch(input, init); | ||
} | ||
|
||
if (isLocalResource(input, "sitemap.xml")) { | ||
// @todo: dynamic import sitemap ??? | ||
const response = new Response(JSON.stringify(sitemap)); | ||
response.headers.set("content-type", "application/json; charset=utf-8"); | ||
return Promise.resolve(response); | ||
} | ||
|
||
return fetch(input, init); | ||
}; | ||
|
||
export const loader = async (arg: LoaderFunctionArgs) => { | ||
const url = new URL(arg.request.url); | ||
const host = | ||
arg.request.headers.get("x-forwarded-host") || | ||
arg.request.headers.get("host") || | ||
""; | ||
url.host = host; | ||
url.protocol = "https"; | ||
|
||
const urls = sitemap.map((page) => { | ||
const url = new URL(`https://${host}${page.path}`); | ||
const params = getRemixParams(arg.params); | ||
|
||
return ` | ||
<url> | ||
<loc>${url.href}</loc> | ||
<lastmod>${page.lastModified.split("T")[0]}</lastmod> | ||
</url> | ||
`; | ||
}); | ||
const system = { | ||
params, | ||
search: Object.fromEntries(url.searchParams), | ||
origin: url.origin, | ||
}; | ||
|
||
return new Response( | ||
`<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${urls.join("")} | ||
</urlset> | ||
`, | ||
{ | ||
headers: { | ||
"Content-Type": "application/xml", | ||
}, | ||
status: 200, | ||
} | ||
const resources = await loadResources( | ||
customFetch, | ||
getResources({ system }).data | ||
); | ||
const pageMeta = getPageMeta({ system, resources }); | ||
|
||
if (pageMeta.redirect) { | ||
const status = | ||
pageMeta.status === 301 || pageMeta.status === 302 | ||
? pageMeta.status | ||
: 302; | ||
return redirect(pageMeta.redirect, status); | ||
} | ||
|
||
// typecheck | ||
arg.context.EXCLUDE_FROM_SEARCH satisfies boolean; | ||
|
||
const text = renderToString( | ||
<ReactSdkContext.Provider | ||
value={{ | ||
imageLoader, | ||
assetBaseUrl, | ||
imageBaseUrl, | ||
resources, | ||
}} | ||
> | ||
<Page system={system} /> | ||
</ReactSdkContext.Provider> | ||
); | ||
|
||
return new Response(`<?xml version="1.0" encoding="UTF-8"?>\n${text}`, { | ||
headers: { "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
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