-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
start-server.ts
443 lines (392 loc) · 13.6 KB
/
start-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import webpackHotMiddleware from "webpack-hot-middleware"
import webpackDevMiddleware, {
WebpackDevMiddleware,
} from "webpack-dev-middleware"
import got from "got"
import webpack from "webpack"
import express from "express"
import compression from "compression"
import graphqlHTTP from "express-graphql"
import graphqlPlayground from "graphql-playground-middleware-express"
import graphiqlExplorer from "gatsby-graphiql-explorer"
import { formatError } from "graphql"
import http from "http"
import https from "https"
import cors from "cors"
import telemetry from "gatsby-telemetry"
import launchEditor from "react-dev-utils/launchEditor"
import { isCI } from "gatsby-core-utils"
import { withBasePath } from "../utils/path"
import webpackConfig from "../utils/webpack.config"
import { store, emitter } from "../redux"
import report from "gatsby-cli/lib/reporter"
import * as WorkerPool from "../utils/worker/pool"
import {
showExperimentNoticeAfterTimeout,
CancelExperimentNoticeCallbackOrUndefined,
} from "../utils/show-experiment-notice"
import { developStatic } from "../commands/develop-static"
import withResolverContext from "../schema/context"
import { websocketManager, WebsocketManager } from "../utils/websocket-manager"
import {
reverseFixedPagePath,
readPageData,
IPageDataWithQueryResult,
} from "./page-data"
import { getPageData as getPageDataExperimental } from "./get-page-data"
import { findPageByPath } from "./find-page-by-path"
import apiRunnerNode from "../utils/api-runner-node"
import { Express } from "express"
import * as path from "path"
import { Stage, IProgram } from "../commands/types"
import JestWorker from "jest-worker"
type ActivityTracker = any // TODO: Replace this with proper type once reporter is typed
interface IServer {
compiler: webpack.Compiler
listener: http.Server | https.Server
webpackActivity: ActivityTracker
cancelDevJSNotice: CancelExperimentNoticeCallbackOrUndefined
websocketManager: WebsocketManager
workerPool: JestWorker
webpackWatching: IWebpackWatchingPauseResume
}
export interface IWebpackWatchingPauseResume extends webpack.Watching {
suspend: () => void
resume: () => void
}
// context seems to be public, but not documented API
// see https://github.com/webpack/webpack-dev-middleware/issues/656
type PatchedWebpackDevMiddleware = WebpackDevMiddleware &
express.RequestHandler & {
context: {
watching: IWebpackWatchingPauseResume
}
}
export async function startServer(
program: IProgram,
app: Express,
workerPool: JestWorker = WorkerPool.create()
): Promise<IServer> {
const directory = program.directory
const webpackActivity = report.activityTimer(`Building development bundle`, {
id: `webpack-develop`,
})
webpackActivity.start()
// Remove the following when merging GATSBY_EXPERIMENTAL_DEV_SSR
const directoryPath = withBasePath(directory)
const { buildHTML } = require(`../commands/build-html`)
const createIndexHtml = async (activity: ActivityTracker): Promise<void> => {
try {
await buildHTML({
program,
stage: Stage.DevelopHTML,
pagePaths: [`/`],
workerPool,
activity,
})
} catch (err) {
if (err.name !== `WebpackError`) {
report.panic(err)
return
}
report.panic(
report.stripIndent`
There was an error compiling the html.js component for the development server.
See our docs page on debugging HTML builds for help https://gatsby.dev/debug-html
`,
err
)
}
}
const indexHTMLActivity = report.phantomActivity(`building index.html`, {})
if (process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
const { buildRenderer } = require(`../commands/build-html`)
await buildRenderer(program, Stage.DevelopHTML)
const { initDevWorkerPool } = require(`./dev-ssr/render-dev-html`)
initDevWorkerPool()
} else {
indexHTMLActivity.start()
await createIndexHtml(indexHTMLActivity)
indexHTMLActivity.end()
}
const TWENTY_SECONDS = 20 * 1000
let cancelDevJSNotice: CancelExperimentNoticeCallbackOrUndefined
if (
process.env.gatsby_executing_command === `develop` &&
!process.env.GATSBY_EXPERIMENT_DEVJS_LAZY &&
!isCI()
) {
cancelDevJSNotice = showExperimentNoticeAfterTimeout(
`LAZY_DEVJS`,
report.stripIndent(`
Your local development experience is about to get better, faster, and stronger!
Your friendly Gatsby maintainers detected your site takes longer than ideal to bundle your JavaScript. We're working right now to improve this.
If you're interested in trialing out one of these future improvements *today* which should make your local development experience faster, go ahead and run your site with LAZY_DEVJS enabled.
GATSBY_EXPERIMENT_DEVJS_LAZY=true gatsby develop
Please do let us know how it goes (good, bad, or otherwise) at https://gatsby.dev/lazy-devjs-umbrella
`),
TWENTY_SECONDS
)
}
const devConfig = await webpackConfig(
program,
directory,
`develop`,
program.port,
{ parentSpan: webpackActivity.span }
)
const compiler = webpack(devConfig)
/**
* Set up the express app.
**/
app.use(compression())
app.use(telemetry.expressMiddleware(`DEVELOP`))
app.use(
webpackHotMiddleware(compiler, {
log: false,
path: `/__webpack_hmr`,
heartbeat: 10 * 1000,
})
)
app.use(cors())
/**
* Pattern matching all endpoints with graphql or graphiql with 1 or more leading underscores
*/
const graphqlEndpoint = `/_+graphi?ql`
if (process.env.GATSBY_GRAPHQL_IDE === `playground`) {
app.get(
graphqlEndpoint,
graphqlPlayground({
endpoint: `/___graphql`,
}),
() => {}
)
} else {
graphiqlExplorer(app, {
graphqlEndpoint,
})
}
app.use(
graphqlEndpoint,
graphqlHTTP(
(): graphqlHTTP.OptionsData => {
const { schema, schemaCustomization } = store.getState()
if (!schemaCustomization.composer) {
throw new Error(
`A schema composer was not created in time. This is likely a gatsby bug. If you experienced this please create an issue.`
)
}
return {
schema,
graphiql: false,
extensions(): { [key: string]: unknown } {
return {
enableRefresh: process.env.ENABLE_GATSBY_REFRESH_ENDPOINT,
refreshToken: process.env.GATSBY_REFRESH_TOKEN,
}
},
context: withResolverContext({
schema,
schemaComposer: schemaCustomization.composer,
context: {},
customContext: schemaCustomization.context,
}),
customFormatErrorFn(err): unknown {
return {
...formatError(err),
stack: err.stack ? err.stack.split(`\n`) : [],
}
},
}
}
)
)
/**
* Refresh external data sources.
* This behavior is disabled by default, but the ENABLE_GATSBY_REFRESH_ENDPOINT env var enables it
* If no GATSBY_REFRESH_TOKEN env var is available, then no Authorization header is required
**/
const REFRESH_ENDPOINT = `/__refresh`
const refresh = async (req: express.Request): Promise<void> => {
emitter.emit(`WEBHOOK_RECEIVED`, {
webhookBody: req.body,
})
}
app.use(REFRESH_ENDPOINT, express.json())
app.post(REFRESH_ENDPOINT, (req, res) => {
const enableRefresh = process.env.ENABLE_GATSBY_REFRESH_ENDPOINT
const refreshToken = process.env.GATSBY_REFRESH_TOKEN
const authorizedRefresh =
!refreshToken || req.headers.authorization === refreshToken
if (enableRefresh && authorizedRefresh) {
refresh(req)
}
res.end()
})
app.get(`/__open-stack-frame-in-editor`, (req, res) => {
launchEditor(req.query.fileName, req.query.lineNumber)
res.end()
})
app.get(
`/page-data/:pagePath(*)/page-data.json`,
async (req, res, next): Promise<void> => {
const requestedPagePath = req.params.pagePath
if (!requestedPagePath) {
next()
return
}
const potentialPagePath = reverseFixedPagePath(requestedPagePath)
const page = findPageByPath(store.getState(), potentialPagePath, false)
if (page) {
try {
const pageData: IPageDataWithQueryResult = process.env
.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
? await getPageDataExperimental(page.path)
: await readPageData(
path.join(store.getState().program.directory, `public`),
page.path
)
res.status(200).send(pageData)
return
} catch (e) {
report.error(
`Error loading a result for the page query in "${requestedPagePath}" / "${potentialPagePath}". Query was not run and no cached result was found.`,
e
)
}
}
res.status(404).send({
path: potentialPagePath,
})
}
)
// Disable directory indexing i.e. serving index.html from a directory.
// This can lead to serving stale html files during development.
//
// We serve by default an empty index.html that sets up the dev environment.
app.use(developStatic(`public`, { index: false }))
const webpackDevMiddlewareInstance = (webpackDevMiddleware(compiler, {
logLevel: `silent`,
publicPath: devConfig.output.publicPath,
watchOptions: devConfig.devServer ? devConfig.devServer.watchOptions : null,
stats: `errors-only`,
}) as unknown) as PatchedWebpackDevMiddleware
app.use(webpackDevMiddlewareInstance)
// Expose access to app for advanced use cases
const { developMiddleware } = store.getState().config
if (developMiddleware) {
developMiddleware(app, program)
}
// Set up API proxy.
const { proxy } = store.getState().config
if (proxy) {
proxy.forEach(({ prefix, url }) => {
app.use(`${prefix}/*`, (req, res) => {
const proxiedUrl = url + req.originalUrl
const {
// remove `host` from copied headers
// eslint-disable-next-line @typescript-eslint/no-unused-vars
headers: { host, ...headers },
method,
} = req
req
.pipe(
got
.stream(proxiedUrl, { headers, method, decompress: false })
.on(`response`, response =>
res.writeHead(response.statusCode || 200, response.headers)
)
.on(`error`, (err, _, response) => {
if (response) {
res.writeHead(response.statusCode || 400, response.headers)
} else {
const message = `Error when trying to proxy request "${req.originalUrl}" to "${proxiedUrl}"`
report.error(message, err)
res.sendStatus(500)
}
})
)
.pipe(res)
})
}, cors())
}
await apiRunnerNode(`onCreateDevServer`, { app, deferNodeMutation: true })
// In case nothing before handled hot-update - send 404.
// This fixes "Unexpected token < in JSON at position 0" runtime
// errors after restarting development server and
// cause automatic hard refresh in the browser.
app.use(/.*\.hot-update\.json$/i, (_, res) => {
res.status(404).end()
})
// Render an HTML page and serve it.
if (process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
// Setup HTML route.
const { route } = require(`./dev-ssr/develop-html-route`)
route({ app, program, store })
}
app.use(async (req, res) => {
const fullUrl = req.protocol + `://` + req.get(`host`) + req.originalUrl
// This isn't used in development.
if (fullUrl.endsWith(`app-data.json`)) {
res.json({ webpackCompilationHash: `123` })
// If this gets here, it's a non-existant file so just send back 404.
} else if (fullUrl.endsWith(`.json`)) {
res.json({}).status(404)
} else {
if (process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
try {
const { renderDevHTML } = require(`./dev-ssr/render-dev-html`)
const renderResponse = await renderDevHTML({
path: `/dev-404-page/`,
// Let renderDevHTML figure it out.
page: undefined,
store,
htmlComponentRendererPath: `${program.directory}/public/render-page.js`,
directory: program.directory,
})
const status = process.env.GATSBY_EXPERIMENTAL_DEV_SSR ? 404 : 200
res.status(status).send(renderResponse)
} catch (e) {
report.error(e)
res.send(e).status(500)
}
} else {
res.sendFile(directoryPath(`public/index.html`), err => {
if (err) {
res.status(500).end()
}
})
}
}
})
/**
* Set up the HTTP server and socket.io.
**/
const server = new http.Server(app)
const socket = websocketManager.init({ server })
// hardcoded `localhost`, because host should match `target` we set
// in http proxy in `develop-proxy`
const listener = server.listen(program.port, `localhost`)
if (!process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
const chokidar = require(`chokidar`)
const { slash } = require(`gatsby-core-utils`)
// Register watcher that rebuilds index.html every time html.js changes.
const watchGlobs = [`src/html.js`, `plugins/**/gatsby-ssr.js`].map(path =>
slash(directoryPath(path))
)
chokidar.watch(watchGlobs).on(`change`, async () => {
await createIndexHtml(indexHTMLActivity)
// eslint-disable-next-line no-unused-expressions
socket?.to(`clients`).emit(`reload`)
})
}
return {
compiler,
listener,
webpackActivity,
cancelDevJSNotice,
websocketManager,
workerPool,
webpackWatching: webpackDevMiddlewareInstance.context.watching,
}
}