Skip to content

Commit bd6a00a

Browse files
Server code refactoring and minor docs updates (#668)
Co-authored-by: Kent C. Dodds <[email protected]>
1 parent 05d92de commit bd6a00a

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

docs/monitoring.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ under "Tokens", and copy that to secure location (this becomes
4545
organization slug (`SENTRY_ORG`), and the slug name for your project too
4646
(`SENTRY_PROJECT`).
4747

48-
Uncomment the sentry-vite plugin in `vite.config` and the build command in the
49-
'build' section of the Dockerfile, which shows how to pass the
50-
`SENTRY_AUTH_TOKEN` (below) as a docker secret, so it is available to the vite
51-
config when `npm run build` is run (you will need to do the same for
52-
`SENTRY_ORG` and `SENTRY_PROJECT`). Note that these do not need to be added to
53-
the `env.server` env vars schema, as they are only used during the build and not
54-
the runtime.
55-
56-
The plugin will create sentry releases for you and automatically associate
57-
commits during the vite build once you set the `SENTRY_AUTH_TOKEN` in
58-
[your GitHub action secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions).
48+
In the 'build' section of the [Dockerfile](../other/Dockerfile), there is an
49+
example of how to pass (mount) the `SENTRY_AUTH_TOKEN` as a docker secret, so it
50+
is available to Vite when `npm run build` is run. You can do the same for
51+
`SENTRY_ORG` and `SENTRY_PROJECT` or actually any other secret (environment
52+
variable) you need at build time, just make sure those secrets (variables) are
53+
available on the CI runner: see the 'deploy' job from
54+
[`deploy`](../.github/workflows/deploy.yml) workflow. Note that these do not
55+
need to be added to the [`env.server`](../app/utils/env.server.ts) env vars
56+
schema, as they are only used during the build and not the runtime.
57+
58+
The Sentry Vite plugin in [`vite.config.ts`](../vite.config.ts) will create
59+
sentry releases for you and automatically associate commits during the vite
60+
build once the `SENTRY_AUTH_TOKEN` is set, which in our set-up is done via
61+
[GitHub Actions secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions).
5962
In this setup we have utilized a simple strategy for naming releases of using
6063
the commit sha, passed in as a build arg via the GitHub action workflow.

index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dotenv/config'
22
import * as fs from 'fs'
3-
import { installGlobals } from '@remix-run/node'
43
import chalk from 'chalk'
54
import closeWithGrace from 'close-with-grace'
65
import sourceMapSupport from 'source-map-support'
@@ -19,8 +18,6 @@ sourceMapSupport.install({
1918
},
2019
})
2120

22-
installGlobals()
23-
2421
closeWithGrace(async ({ err }) => {
2522
if (err) {
2623
console.error(chalk.red(err))

server/index.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ import morgan from 'morgan'
1515
installGlobals()
1616

1717
const MODE = process.env.NODE_ENV ?? 'development'
18+
const IS_PROD = MODE === 'production'
1819

19-
const createRequestHandler =
20-
MODE === 'production'
21-
? Sentry.wrapExpressCreateRequestHandler(_createRequestHandler)
22-
: _createRequestHandler
20+
const createRequestHandler = IS_PROD
21+
? Sentry.wrapExpressCreateRequestHandler(_createRequestHandler)
22+
: _createRequestHandler
2323

24-
const viteDevServer =
25-
MODE === 'production'
26-
? undefined
27-
: await import('vite').then(vite =>
28-
vite.createServer({
29-
server: { middlewareMode: true },
30-
}),
31-
)
24+
const viteDevServer = IS_PROD
25+
? undefined
26+
: await import('vite').then(vite =>
27+
vite.createServer({
28+
server: { middlewareMode: true },
29+
}),
30+
)
3231

3332
const app = express()
3433

@@ -84,7 +83,7 @@ if (viteDevServer) {
8483
app.use(express.static('build/client', { maxAge: '1h' }))
8584
}
8685

87-
app.get(['/img/*', '/favicons/*'], (req, res) => {
86+
app.get(['/img/*', '/favicons/*'], (_req, res) => {
8887
// if we made it past the express.static for these, then we're missing something.
8988
// So we'll just send a 404 and won't bother calling other middleware.
9089
return res.status(404).send('Not found')
@@ -108,6 +107,7 @@ app.use((_, res, next) => {
108107

109108
app.use(
110109
helmet({
110+
xPoweredBy: false,
111111
referrerPolicy: { policy: 'same-origin' },
112112
crossOriginEmbedderPolicy: false,
113113
contentSecurityPolicy: {
@@ -142,7 +142,7 @@ app.use(
142142
// rate limiting because playwright tests are very fast and we don't want to
143143
// have to wait for the rate limit to reset between tests.
144144
const maxMultiple =
145-
MODE !== 'production' || process.env.PLAYWRIGHT_TEST_BASE_URL ? 10_000 : 1
145+
!IS_PROD || process.env.PLAYWRIGHT_TEST_BASE_URL ? 10_000 : 1
146146
const rateLimitDefault = {
147147
windowMs: 60 * 1000,
148148
max: 1000 * maxMultiple,
@@ -212,8 +212,7 @@ app.all(
212212
serverBuild: getBuild(),
213213
}),
214214
mode: MODE,
215-
// @sentry/remix needs to be updated to handle the function signature
216-
build: MODE === 'production' ? await getBuild() : getBuild,
215+
build: getBuild,
217216
}),
218217
)
219218

@@ -224,29 +223,25 @@ const portToUse = await getPort({
224223

225224
const server = app.listen(portToUse, () => {
226225
const addy = server.address()
227-
const portUsed =
228-
desiredPort === portToUse
229-
? desiredPort
230-
: addy && typeof addy === 'object'
231-
? addy.port
232-
: 0
226+
const portActuallyUsed =
227+
addy === null || typeof addy === 'string' ? 0 : addy.port
233228

234-
if (portUsed !== desiredPort) {
229+
if (portActuallyUsed !== desiredPort) {
235230
console.warn(
236231
chalk.yellow(
237-
`⚠️ Port ${desiredPort} is not available, using ${portUsed} instead.`,
232+
`⚠️ Port ${desiredPort} is not available, using ${portActuallyUsed} instead.`,
238233
),
239234
)
240235
}
241236
console.log(`🚀 We have liftoff!`)
242-
const localUrl = `http://localhost:${portUsed}`
237+
const localUrl = `http://localhost:${portActuallyUsed}`
243238
let lanUrl: string | null = null
244239
const localIp = ipAddress() ?? 'Unknown'
245240
// Check if the address is a private ip
246241
// https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
247242
// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-dev-utils/WebpackDevServerUtils.js#LL48C9-L54C10
248243
if (/^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(localIp)) {
249-
lanUrl = `http://${localIp}:${portUsed}`
244+
lanUrl = `http://${localIp}:${portActuallyUsed}`
250245
}
251246

252247
console.log(

0 commit comments

Comments
 (0)