Skip to content

Commit

Permalink
fix: use system environment variables for robots (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Jul 9, 2024
1 parent 4bf7d16 commit e86f46f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-carrots-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Uses the deployment URL for the robots.txt sitemap field rather than another environment variable.
41 changes: 22 additions & 19 deletions core/app/robots.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import type { MetadataRoute } from 'next';

// Disallow routes that have no SEO value or should not be indexed
const disallow = ['/cart', '/account'];

// Robots.txt config https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots#generate-a-robots-file
const robotsConfig: MetadataRoute.Robots = {
rules: [
{
userAgent: '*',
allow: ['/'],
disallow,
},
],
};
function parseUrl(url?: string): URL {
let incomingUrl = '';
const defaultUrl = new URL('http://localhost:3000/');

// Infer base URL from environment variables
const baseUrl = process.env.PRODUCTION_BASE_URL || process.env.NEXTAUTH_URL;
if (url && !url.startsWith('http')) {
incomingUrl = `https://${url}`;
}

// Set sitemap URL if base URL is defined, as sitemap URL must be an absolute URL
if (baseUrl) {
robotsConfig.sitemap = `${baseUrl}/sitemap.xml`;
return new URL(incomingUrl || defaultUrl);
}

// Disallow routes that have no SEO value or should not be indexed
const disallow = ['/cart', '/account'];

// Robots.txt config https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots#generate-a-robots-file
export default function robots(): MetadataRoute.Robots {
return robotsConfig;
return {
// Infer base URL from environment variables
sitemap: parseUrl(process.env.NEXTAUTH_URL || process.env.VERCEL_URL || '').origin,
rules: [
{
userAgent: '*',
allow: ['/'],
disallow,
},
],
};
}

0 comments on commit e86f46f

Please sign in to comment.