Skip to content

Commit b637a2b

Browse files
authored
add control over indexability (#679)
1 parent 800022d commit b637a2b

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ SENTRY_DSN="your-dsn"
1313
GITHUB_CLIENT_ID="MOCK_GITHUB_CLIENT_ID"
1414
GITHUB_CLIENT_SECRET="MOCK_GITHUB_CLIENT_SECRET"
1515
GITHUB_TOKEN="MOCK_GITHUB_TOKEN"
16+
17+
# set this to false to prevent search engines from indexing the website
18+
# default to allow indexing for seo safety
19+
ALLOW_INDEXING="true"

app/root.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ function Document({
181181
nonce,
182182
theme = 'light',
183183
env = {},
184+
allowIndexing = true,
184185
}: {
185186
children: React.ReactNode
186187
nonce: string
187188
theme?: Theme
188189
env?: Record<string, string>
190+
allowIndexing?: boolean
189191
}) {
190192
return (
191193
<html lang="en" className={`${theme} h-full overflow-x-hidden`}>
@@ -194,6 +196,9 @@ function Document({
194196
<Meta />
195197
<meta charSet="utf-8" />
196198
<meta name="viewport" content="width=device-width,initial-scale=1" />
199+
{allowIndexing ? null : (
200+
<meta name="robots" content="noindex, nofollow" />
201+
)}
197202
<Links />
198203
</head>
199204
<body className="bg-background text-foreground">
@@ -219,10 +224,16 @@ function App() {
219224
const matches = useMatches()
220225
const isOnSearchPage = matches.find(m => m.id === 'routes/users+/index')
221226
const searchBar = isOnSearchPage ? null : <SearchBar status="idle" />
227+
const allowIndexing = data.ENV.ALLOW_INDEXING !== 'false'
222228
useToast(data.toast)
223229

224230
return (
225-
<Document nonce={nonce} theme={theme} env={data.ENV}>
231+
<Document
232+
nonce={nonce}
233+
theme={theme}
234+
allowIndexing={allowIndexing}
235+
env={data.ENV}
236+
>
226237
<div className="flex h-screen flex-col justify-between">
227238
<header className="container py-6">
228239
<nav className="flex flex-wrap items-center justify-between gap-4 sm:flex-nowrap md:gap-8">

app/utils/env.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const schema = z.object({
1616
GITHUB_CLIENT_ID: z.string().default('MOCK_GITHUB_CLIENT_ID'),
1717
GITHUB_CLIENT_SECRET: z.string().default('MOCK_GITHUB_CLIENT_SECRET'),
1818
GITHUB_TOKEN: z.string().default('MOCK_GITHUB_TOKEN'),
19+
ALLOW_INDEXING: z.enum(['true', 'false']).optional(),
1920
})
2021

2122
declare global {
@@ -50,6 +51,7 @@ export function getEnv() {
5051
return {
5152
MODE: process.env.NODE_ENV,
5253
SENTRY_DSN: process.env.SENTRY_DSN,
54+
ALLOW_INDEXING: process.env.ALLOW_INDEXING,
5355
}
5456
}
5557

docs/deployment.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ Prior to your first deployment, you'll need to do a few things:
6767
> [1Password](https://1password.com/password-generator) to generate a random
6868
> secret, just replace `$(openssl rand -hex 32)` with the generated secret.
6969
70+
- Add a `ALLOW_INDEXING` with `false` value to your non-production fly app
71+
secrets, this is to prevent duplicate content from being indexed multiple
72+
times by search engines. To do this you can run the following commands:
73+
74+
```sh
75+
fly secrets set ALLOW_INDEXING=false --app [YOUR_APP_NAME]-staging
76+
```
77+
7078
6. Create production database:
7179

7280
Create a persistent volume for the sqlite database for both your staging and

remix.init/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async function setupDeployment({ rootDirectory }) {
160160
await $I`fly apps create ${APP_NAME}`
161161

162162
console.log(`🤫 Setting secrets in apps`)
163-
await $I`fly secrets set SESSION_SECRET=${getRandomString32()} INTERNAL_COMMAND_TOKEN=${getRandomString32()} HONEYPOT_SECRET=${getRandomString32()} --app ${APP_NAME}-staging`
163+
await $I`fly secrets set SESSION_SECRET=${getRandomString32()} INTERNAL_COMMAND_TOKEN=${getRandomString32()} HONEYPOT_SECRET=${getRandomString32()} ALLOW_INDEXING=false --app ${APP_NAME}-staging`
164164
await $I`fly secrets set SESSION_SECRET=${getRandomString32()} INTERNAL_COMMAND_TOKEN=${getRandomString32()} HONEYPOT_SECRET=${getRandomString32()} --app ${APP_NAME}`
165165

166166
console.log(

server/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ installGlobals()
1616

1717
const MODE = process.env.NODE_ENV ?? 'development'
1818
const IS_PROD = MODE === 'production'
19-
const IS_DEV = MODE === "development"
19+
const IS_DEV = MODE === 'development'
20+
const ALLOW_INDEXING = process.env.ALLOW_INDEXING !== 'false'
2021

2122
const createRequestHandler = IS_PROD
2223
? Sentry.wrapExpressCreateRequestHandler(_createRequestHandler)
@@ -205,6 +206,13 @@ async function getBuild() {
205206
return build as unknown as ServerBuild
206207
}
207208

209+
if (!ALLOW_INDEXING) {
210+
app.use((_, res, next) => {
211+
res.set('X-Robots-Tag', 'noindex, nofollow')
212+
next()
213+
})
214+
}
215+
208216
app.all(
209217
'*',
210218
createRequestHandler({

0 commit comments

Comments
 (0)