From b8d4748c29d821ba6cb92e622f252a8a9e0c1d38 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 24 Jul 2024 16:30:43 +0200 Subject: [PATCH] Allow for older remix versions to be supported as well by allowing the user to pass in routes --- README.md | 17 ++++------ package-lock.json | 8 +++-- package.json | 2 +- src/remix/sitemap.ts | 31 +++++++++++++------ .../remix-vite/app/routes/sitemap[.]xml.ts | 7 +++-- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5dc8a5c..dc7bda6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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`], }, ]) diff --git a/package-lock.json b/package-lock.json index df58607..8622b80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "seo-tools", + "name": "@forge42/seo-tools", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "seo-tools", + "name": "@forge42/seo-tools", "version": "1.0.0", "license": "MIT", "workspaces": [ @@ -1319,6 +1319,10 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@forge42/seo-tools": { + "resolved": "", + "link": true + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.14", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz", diff --git a/package.json b/package.json index 594cc52..b662a7d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/remix/sitemap.ts b/src/remix/sitemap.ts index a4da66f..060493a 100644 --- a/src/remix/sitemap.ts +++ b/src/remix/sitemap.ts @@ -41,18 +41,24 @@ const createExtendedRoutes = (routes: RouteManifest) => { const generateRemixSitemapRoutes = async ({ domain, sitemapData, + routes, }: { domain: string sitemapData?: unknown + routes?: RouteManifest }) => { - // @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) const transformedRoutes = await Promise.all( extendedRoutes.map(async (route) => { @@ -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 } /** @@ -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 }) } diff --git a/test-apps/remix-vite/app/routes/sitemap[.]xml.ts b/test-apps/remix-vite/app/routes/sitemap[.]xml.ts index d38f469..ab203d9 100644 --- a/test-apps/remix-vite/app/routes/sitemap[.]xml.ts +++ b/test-apps/remix-vite/app/routes/sitemap[.]xml.ts @@ -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, {