Skip to content

Commit

Permalink
do not allow indexing for staging
Browse files Browse the repository at this point in the history
  • Loading branch information
goodeats committed May 22, 2024
1 parent 0f35cc5 commit 8b726f8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ GITHUB_CLIENT_SECRET="MOCK_GITHUB_CLIENT_SECRET"
GITHUB_TOKEN="MOCK_GITHUB_TOKEN"

FLY_APP_NAME="fly-app-name-1234"

# set this to false to prevent search engines from indexing the website
# default to allow indexing for seo safety
# set false for staging via fly secrets
# https://github.com/epicweb-dev/epic-stack/blob/main/docs/deployment.md
ALLOW_INDEXING="true"
15 changes: 13 additions & 2 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
where: { id: userId },
}),
{ timings, type: 'find user', desc: 'find user in root' },
)
)
: null
if (userId && !user) {
console.info('something weird happened')
Expand Down Expand Up @@ -179,11 +179,13 @@ function Document({
children,
nonce,
theme = 'light',
allowIndexing = true,
env = {},
}: {
children: React.ReactNode
nonce: string
theme?: Theme
allowIndexing?: boolean
env?: Record<string, string>
}) {
return (
Expand All @@ -193,6 +195,9 @@ function Document({
<Meta />
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
{allowIndexing ? null : (
<meta name="robots" content="noindex, nofollow" />
)}
<Links />
</head>
<body className="bg-background text-foreground">
Expand Down Expand Up @@ -240,10 +245,16 @@ function App() {
const data = useLoaderData<typeof loader>()
const nonce = useNonce()
const theme = useTheme()
const allowIndexing = data.ENV.ALLOW_INDEXING !== 'false'
useToast(data.toast)

return (
<Document nonce={nonce} theme={theme} env={data.ENV}>
<Document
nonce={nonce}
theme={theme}
allowIndexing={allowIndexing}
env={data.ENV}
>
<AppBody />
<EpicToaster closeButton position="top-center" theme={theme} />
<EpicProgress />
Expand Down
2 changes: 2 additions & 0 deletions app/utils/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const schema = z.object({
GITHUB_CLIENT_ID: z.string().default('MOCK_GITHUB_CLIENT_ID'),
GITHUB_CLIENT_SECRET: z.string().default('MOCK_GITHUB_CLIENT_SECRET'),
GITHUB_TOKEN: z.string().default('MOCK_GITHUB_TOKEN'),
ALLOW_INDEXING: z.enum(['true', 'false']).optional(),
})

declare global {
Expand Down Expand Up @@ -50,6 +51,7 @@ export function getEnv() {
return {
MODE: process.env.NODE_ENV,
SENTRY_DSN: process.env.SENTRY_DSN,
ALLOW_INDEXING: process.env.ALLOW_INDEXING,
}
}

Expand Down
8 changes: 8 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ installGlobals()
const MODE = process.env.NODE_ENV ?? 'development'
const IS_PROD = MODE === 'production'
const IS_DEV = MODE === 'development'
const ALLOW_INDEXING = process.env.ALLOW_INDEXING !== 'false'

const createRequestHandler = IS_PROD
? Sentry.wrapExpressCreateRequestHandler(_createRequestHandler)
Expand Down Expand Up @@ -208,6 +209,13 @@ async function getBuild() {
return build as unknown as ServerBuild
}

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

app.all(
'*',
createRequestHandler({
Expand Down

0 comments on commit 8b726f8

Please sign in to comment.