Skip to content

Commit

Permalink
deal with search engines indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsam committed Mar 29, 2024
1 parent 3235416 commit 75a3946
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 11 additions & 4 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
import { NavBar } from "#app/components/navbar/index.tsx";
import { Footer } from "#app/components/footer.tsx";
import clsx from "clsx";
import { publicEnv, forceEnvValidation } from "#app/utils/env.server.ts";
import {
publicEnv,
forceEnvValidation,
type PublicEnv,
} from "#app/utils/env.server.ts";
import { FaviconMeta, faviconLinks } from "#app/utils/favicon.tsx";
import { useNonce } from "./utils/nonce-provider.tsx";
import { ClientHintsCheck, getHints } from "./utils/client-hints.tsx";
Expand Down Expand Up @@ -92,19 +96,22 @@ function Document({
children,
nonce,
theme = "light",
env = {},
env,
}: {
children: React.ReactNode;
nonce: string;
theme?: Theme;
env?: Record<string, string>;
env?: PublicEnv;
}) {
return (
<html lang="en" className={clsx(theme, "relative")}>
<head>
<ClientHintsCheck nonce={nonce} />
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
{!env?.ALLOW_INDEXING && (
<meta name="robots" content="noindex, nofollow" />
)}
<FaviconMeta />

<Meta />
Expand All @@ -118,7 +125,7 @@ function Document({
<script
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `window.ENV = ${JSON.stringify(env)}`,
__html: `window.ENV = ${JSON.stringify(env ?? {})}`,
}}
/>
<Scripts nonce={nonce} />
Expand Down
10 changes: 9 additions & 1 deletion app/utils/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const envSchema = z
.object({
NODE_ENV: z.enum(["development", "production", "test"]),

ALLOW_INDEXING: z
.enum(["true", "false"])
.optional()
.transform((s) => s === "true"),

SESSION_SECRET: z.string(),
CSRF_SECRET: z.string().optional(),

Expand Down Expand Up @@ -34,7 +39,10 @@ if (!parsedEnv.success) {

export const env = parsedEnv.data;

const PUBLIC_ENV = ["NODE_ENV"] as const satisfies (keyof Env)[];
const PUBLIC_ENV = [
"NODE_ENV",
"ALLOW_INDEXING",
] as const satisfies (keyof Env)[];

export type PublicEnv = typeof publicEnv;
export const publicEnv = pick(PUBLIC_ENV, env);
Expand Down
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import chalk from "chalk";
import { printUrls } from "./server-utils.js";

const MODE = process.env.NODE_ENV ?? "development";
const ALLOW_INDEXING = process.env.ALLOW_INDEXING === "true";

sourceMapSupport.install();
installGlobals();
Expand Down Expand Up @@ -75,6 +76,13 @@ app.use(
}),
);

if (!ALLOW_INDEXING) {
app.use((_, res, next) => {
res.set("X-Robots-Tag", "noindex, nofollow");
next();
});
}

// handle asset requests
if (viteDevServer) {
app.use(viteDevServer.middlewares);
Expand Down

0 comments on commit 75a3946

Please sign in to comment.