Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(refresh-endpoint): call refresh while refreshing #24887

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe(`When the refresh endpoint is hit multiple times`, () => {
afterEach(() => {
waitUntilServerRespondsSucessfully()
});
it(`should put new requests with unique body in a queue`, () => {
cy.request(`POST`, `/__refresh`).then(response => {
expect(response.body).to.be.equal(``)
})
cy.request(`POST`, `/__refresh`).then(response => {
expect(response.body).to.include(`queued`)
})
cy.request(`POST`, `/__refresh`).then(response => {
expect(response.body).to.include(`already queued`)
})
cy.request(`POST`, `/__refresh`, `otherBody`).then(response => {
expect(response.body).to.include(`queued`)
})
cy.request(`POST`, `/__refresh`, `otherBody`).then(response => {
expect(response.body).to.include(`already queued`)
})
})

})

function waitUntilServerRespondsSucessfully() {
cy.waitUntil(() => cy.request("/").then((response) => {
return response.status == 200
}), {
timeout: 20000,
interval: 1000
})
}
1 change: 1 addition & 0 deletions e2e-tests/development-runtime/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "@testing-library/cypress/add-commands"
import 'cypress-wait-until';

Cypress.Commands.add(`lifecycleCallCount`, action =>
cy
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/development-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@testing-library/cypress": "^4.0.4",
"cross-env": "^5.2.0",
"cypress": "3.4.1",
"cypress-wait-until": "1.7.1",
"fs-extra": "^7.0.1",
"gatsby-cypress": "^0.1.7",
"is-ci": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ module.exports = async (program: IProgram): Promise<void> => {
proxy.serveSite()
io.emit(`develop:started`)
}

if (msg.type === `REFRESH` && msg.action === `FINISHED`) {
proxy.refreshEnded()
}
}

io.on(`connection`, socket => {
Expand Down
75 changes: 75 additions & 0 deletions packages/gatsby/src/utils/develop-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IProgram } from "../commands/types"
interface IProxyControls {
serveRestartingScreen: () => void
serveSite: () => void
refreshEnded: () => void
server: any
}

Expand All @@ -30,6 +31,12 @@ export const startDevelopProxy = (input: {
program: IProgram
}): IProxyControls => {
let shouldServeRestartingScreen = false
let isRefreshing = false
const refreshQueue: {
res: http.ServerResponse
req: http.IncomingMessage
body: string
}[] = []

const proxy = httpProxy.createProxyServer({
target: `http://localhost:${input.targetPort}`,
Expand Down Expand Up @@ -67,6 +74,52 @@ export const startDevelopProxy = (input: {
return
}

/**
* 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`
if (req.url === REFRESH_ENDPOINT) {
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) {
return
}

readRequestBody(req).then((body: string) => {
const isRequestWithSameBodyQueued = !!refreshQueue.filter(
queued => queued.body === body
).length

if (!isRequestWithSameBodyQueued) {
refreshQueue.push({ req, res, body })
}

if (isRefreshing) {
if (isRequestWithSameBodyQueued) {
res.end(
`already queued ${JSON.stringify(refreshQueue.map(q => q.body))}`
)
return
}
res.end(`queued ${JSON.stringify(refreshQueue.map(q => q.body))}`)
return
}

isRefreshing = true
const queued = refreshQueue.shift()
if (queued) {
proxy.web(queued.req, queued.res)
}
})

return
}

if (
shouldServeRestartingScreen ||
req.url === `/___debug-restarting-screen`
Expand All @@ -78,6 +131,18 @@ export const startDevelopProxy = (input: {
proxy.web(req, res)
}

function readRequestBody(request: http.IncomingMessage): Promise<string> {
return new Promise(resolve => {
let body = ``
request.on(`data`, chunk => {
body += chunk.toString()
})
request.on(`end`, () => {
resolve(body)
})
})
}

const server = input.program.ssl
? https.createServer(input.program.ssl, app)
: http.createServer(app)
Expand All @@ -96,5 +161,15 @@ export const startDevelopProxy = (input: {
serveSite: (): void => {
shouldServeRestartingScreen = false
},
refreshEnded: (): void => {
if (!refreshQueue.length) {
isRefreshing = false
} else {
const queued = refreshQueue.shift()
if (queued) {
proxy.web(queued.req, queued.res)
}
}
},
}
}
23 changes: 15 additions & 8 deletions packages/gatsby/src/utils/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export async function startServer(
**/
const REFRESH_ENDPOINT = `/__refresh`
const refresh = async (req: express.Request): Promise<void> => {
if (process.send) {
process.send({
type: `REFRESH`,
action: `STARTED`,
})
}

stopSchemaHotReloader()
let activity = report.activityTimer(`createSchemaCustomization`, {})
activity.start()
Expand All @@ -195,17 +202,17 @@ export async function startServer(
await rebuildSchema({ parentSpan: activity })
activity.end()
startSchemaHotReloader()

if (process.send) {
process.send({
type: `REFRESH`,
action: `FINISHED`,
})
}
}
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)
}
refresh(req)
res.end()
})

Expand Down