Skip to content

Commit

Permalink
Allow for older remix versions to be supported as well by allowing th…
Browse files Browse the repository at this point in the history
…e user to pass in routes
  • Loading branch information
AlemTuzlak committed Jul 24, 2024
1 parent d9f479b commit b8d4748
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,15 @@ If you want to generate different sitemaps based on the language you can use the
// routes/sitemap.$lang[.]xml.ts
import type { LoaderFunctionArgs } from "@remix-run/node"
import { generateRemixSitemap } from "seo-tools/remix/sitemap"

// Optionally import routes from the remix build to be consumed by the sitemap generator if the default one throws an error
import { routes } from "virtual:remix/server-build";
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const domain = `${new URL(request.url).origin}`

const sitemap = await generateRemixSitemap({
// Domain to append urls to
domain,
routes,
// Ignores all dashboard routes
ignore: ["/status"],
// Transforms the url before adding it to the sitemap
Expand Down Expand Up @@ -417,16 +419,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
const domain = new URL(request.url).origin
const robotsTxt = generateRobotsTxt([
{
type: "User-agent",
value: "*",
},
{
type: isProductionDeployment ? "Allow" : "Disallow",
value: "/",
},
{
type: "Sitemap",
value: `${domain}/sitemap-index.xml`,
userAgent: "*",
[isProductionDeployment ? "disallow": "allow"]:["/"],
sitemap: [`${domain}/sitemap-index.xml`],
},
])

Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@forge42/seo-tools",
"version": "1.0.0",
"version": "1.1.0",
"private": false,
"keywords": ["seo", "remix-seo", "seo-tools", "structured-data", "sitemap", "robots", "canonical", "seo-alternate"],
"description": "Set of helpers designed to help you create, maintain and develop your SEO",
Expand Down
31 changes: 21 additions & 10 deletions src/remix/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,24 @@ const createExtendedRoutes = (routes: RouteManifest<ServerRoute>) => {
const generateRemixSitemapRoutes = async ({
domain,
sitemapData,
routes,
}: {
domain: string
sitemapData?: unknown
routes?: RouteManifest<ServerRoute>
}) => {
// @ts-expect-error - This import exists but is not picked up by the typescript compiler because it's a remix internal
const { routes } = await import("virtual:remix/server-build").catch(() => {
throw new Error(
"Could not find the remix server build. Make sure you have Remix running on Vite and not in SPA mode. Otherwise use the generateSitemap utility."
)
})
let finalRoutes = routes
if (!finalRoutes) {
// @ts-expect-error - This import exists but is not picked up by the typescript compiler because it's a remix internal
const { routes } = await import("virtual:remix/server-build").catch(() => {
throw new Error(
"Could not find the remix server build. Make sure you have Remix running on Vite and not in SPA mode. Otherwise use the generateSitemap utility."
)
})
finalRoutes = routes
}
// Add the url to each route
const extendedRoutes = createExtendedRoutes(routes)
const extendedRoutes = createExtendedRoutes(finalRoutes as unknown as RouteManifest<ServerRoute>)

const transformedRoutes = await Promise.all(
extendedRoutes.map(async (route) => {
Expand Down Expand Up @@ -97,6 +103,11 @@ export interface RemixSitemapInfo {
* @example (url) => url.replace(/\/$/, "")
*/
urlTransformer?: (url: string) => string

/**
* The routes object from the remix server build. If not provided, the utility will try to import it.
*/
routes?: RouteManifest<ServerRoute>
}

/**
Expand All @@ -110,7 +121,7 @@ export interface RemixSitemapInfo {
* @returns Sitemap string to be passed back to the response.
*/
export const generateRemixSitemap = async (sitemapInfo: RemixSitemapInfo) => {
const { domain, sitemapData, ignore, urlTransformer } = sitemapInfo
const routes = await generateRemixSitemapRoutes({ domain, sitemapData })
return generateSitemap({ domain, routes, ignore, urlTransformer })
const { domain, sitemapData, ignore, urlTransformer, routes } = sitemapInfo
const finalRoutes = await generateRemixSitemapRoutes({ domain, sitemapData, routes })
return generateSitemap({ domain, routes: finalRoutes, ignore, urlTransformer })
}
7 changes: 4 additions & 3 deletions test-apps/remix-vite/app/routes/sitemap[.]xml.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { generateRemixSitemap } from "seo-tools/remix/sitemap"

import { generateRemixSitemap } from "@forge42/seo-tools/remix/sitemap"
// @ts-expect-error
import { routes } from "virtual:remix/server-build"
export const loader = async() => {
const sitemap = await generateRemixSitemap({
domain: "https://example.com",

routes
})

return new Response(sitemap, {
Expand Down

0 comments on commit b8d4748

Please sign in to comment.