Skip to content

Commit 22cce83

Browse files
fix(nx-dev): make sure only prod is indexed (#33922)
Note: required updating inputs for tasks to make sure env vars are correctly cache busting. Prod <img width="1459" height="724" alt="image" src="https://github.com/user-attachments/assets/1289b745-b68f-4176-9812-ef783d03ba26" /> non prod <img width="1585" height="589" alt="image" src="https://github.com/user-attachments/assets/dd0603b2-6487-4690-8bed-4c3d82644304" />
1 parent e31f815 commit 22cce83

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

astro-docs/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"target": "build"
2424
}
2525
],
26+
"inputs": ["production", "^production", { "env": "NX_DEV_URL" }],
2627
"outputs": [
2728
"{projectRoot}/dist",
2829
"{projectRoot}/.astro",

astro-docs/public/robots.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

astro-docs/src/pages/robots.txt.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { APIRoute } from 'astro';
2+
3+
const siteUrl = import.meta.env.SITE ?? 'https://nx.dev';
4+
const isProductionDomain = siteUrl === 'https://nx.dev';
5+
6+
const productionRobotsTxt = `# https://www.robotstxt.org/robotstxt.html
7+
User-agent: *
8+
Allow: /
9+
10+
# Host
11+
Host: https://nx.dev
12+
13+
# Sitemaps
14+
Sitemap: https://nx.dev/sitemap-index.xml
15+
`;
16+
17+
// Block all crawling on non-production domains (canary, versioned, etc.)
18+
const nonProductionRobotsTxt = `# This is a non-production deployment (canary, versioned, preview, etc.)
19+
# All crawling is disallowed to prevent duplicate content in search results.
20+
User-agent: *
21+
Disallow: /
22+
`;
23+
24+
export const GET: APIRoute = () => {
25+
return new Response(
26+
isProductionDomain ? productionRobotsTxt : nonProductionRobotsTxt,
27+
{
28+
headers: {
29+
'Content-Type': 'text/plain; charset=utf-8',
30+
},
31+
}
32+
);
33+
};
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require('path');
22

3-
const siteUrl = process.env.SITE_URL || 'https://nx.dev';
3+
const siteUrl = process.env.NX_DEV_URL || 'https://nx.dev';
4+
const noIndex = process.env.NEXT_PUBLIC_NO_INDEX === 'true';
5+
46
/**
57
* @type {import('next-sitemap').IConfig}
68
**/
@@ -10,7 +12,11 @@ module.exports = {
1012
exclude: [],
1113
sourceDir: path.resolve(__dirname, '../../dist/nx-dev/nx-dev/.next'),
1214
outDir: path.resolve(__dirname, '../../dist/nx-dev/nx-dev/public'),
13-
robotsTxtOptions: {
14-
additionalSitemaps: [`${siteUrl}/docs/sitemap-index.xml`],
15-
},
15+
robotsTxtOptions: noIndex
16+
? {
17+
policies: [{ userAgent: '*', disallow: '/' }],
18+
}
19+
: {
20+
additionalSitemaps: [`${siteUrl}/docs/sitemap-index.xml`],
21+
},
1622
};

nx-dev/nx-dev/project.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
},
4545
"sitemap": {
4646
"executor": "nx:run-commands",
47+
"inputs": [
48+
"{workspaceRoot}/nx-dev/nx-dev/next-sitemap.config.js",
49+
{ "env": "NX_DEV_URL" },
50+
{ "env": "NEXT_PUBLIC_NO_INDEX" }
51+
],
4752
"outputs": ["{workspaceRoot}/dist/nx-dev/nx-dev/public"],
4853
"options": {
4954
"command": "pnpm next-sitemap --config ./nx-dev/nx-dev/next-sitemap.config.js"

0 commit comments

Comments
 (0)