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/reloading screen while refreshing #2

Open
wants to merge 1 commit into
base: fix/20576-call-refresh-while-refreshing
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions docs/docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ If set to true, this will expose a `/__refresh` webhook that is able to receive

You can trigger this endpoint locally, for example, on Unix-based operating systems (like Ubuntu and MacOS) using `curl -X POST http://localhost:8000/__refresh`.

- `ENABLE_GATSBY_RESTARTING_SCREEN_ON_REFRESH`

If set to true, this will show the restarting screen when the refresh endpoint is hit, until the refresh finishes.

## Build Variables

Gatsby uses additional environment variables in the build step to fine-tune the outcome of a build. You may find these helpful for more advanced configurations, such as using [CI/CD](https://en.wikipedia.org/wiki/CI/CD) to deploy a Gatsby site.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/running-a-gatsby-preview-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Roughly, you can follow these steps to enable your own preview server:

- Run `gatsby develop` on a Node.js host like [Heroku](/docs/deploying-to-heroku/) with the [ENABLE_GATSBY_REFRESH_ENDPOINT environment variable](/docs/environment-variables/#reserved-environment-variables) enabled. This will configure the develop server to re-source data when it receives a `POST` request to `/__refresh`.
- Then setup your CMS to ping this URL on the preview server whenever content is changed.
- if you want to show a restarting screen when the refresh endpoint is hit until it finishes refreshing add the [ENABLE_GATSBY_RESTARTING_SCREEN_ON_REFRESH environment variable](/docs/environment-variables/#reserved-environment-variables)

## Using Gatsby Cloud Preview

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,69 @@
describe(`When the refresh endpoint is hit multiple times`, () => {

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`)
describe(`When the refresh endpoint is hit`, () => {
afterEach(() => {
waitUntilRefreshFinishes()
})

describe(`and accessing any page while refreshing`, () => {
it(`the page shown should be restarting page for 404 page`, () => {
cy.request(`/non-existing`).then(response => {
expect(response.body).not.to.include(`<title>Restarting...</title>`)
})
cy.request(`POST`, `/__refresh`)
cy.request(`/non-existing`).then(response => {
expect(response.body).to.include(`<title>Restarting...</title>`)
})
})
cy.request(`POST`, `/__refresh`).then(response => {
expect(response.body).to.include(`already queued`)

it(`the page shown should be restarting page for existing pages`, () => {
cy.request(`/page-2/`).then(response => {
expect(response.body).not.to.include(`<title>Restarting...</title>`)
})
cy.request(`POST`, `/__refresh`)
cy.request(`/page-2/`).then(response => {
expect(response.body).to.include(`<title>Restarting...</title>`)
})
})
cy.request(`POST`, `/__refresh`, `otherBody`).then(response => {
expect(response.body).to.include(`queued`)
})

describe(`and it was not already refreshing`, () => {
it(`should return empty body`, () => {
cy.request(`POST`, `/__refresh`).then(response => {
expect(response.body).to.be.equal(``)
})
})
cy.request(`POST`, `/__refresh`, `otherBody`).then(response => {
expect(response.body).to.include(`already queued`)
})
describe(`and it was already refreshing`, () => {
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 waitUntilRefreshFinishes() {
cy.waitUntil(
() =>
cy
.request(`/`)
.then(
response => !response.body.includes(`<title>Restarting...</title>`)
),
{
timeout: 60000,
interval: 2000,
}
)
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import "@testing-library/cypress/add-commands"
import "cypress-wait-until"
3 changes: 2 additions & 1 deletion e2e-tests/development-refresh-endpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "cross-env CYPRESS_SUPPORT=y ENABLE_GATSBY_REFRESH_ENDPOINT=true gatsby develop",
"develop": "cross-env CYPRESS_SUPPORT=y ENABLE_GATSBY_RESTARTING_SCREEN_ON_REFRESH=true ENABLE_GATSBY_REFRESH_ENDPOINT=true gatsby develop",
"serve": "gatsby serve",
"start": "npm run develop",
"format": "prettier --write \"src/**/*.js\"",
Expand All @@ -33,6 +33,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.4.4",
"is-ci": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/cache-dir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ apiRunnerAsync(`onClientEntry`).then(() => {
`http://${window.location.hostname}:${services.developstatusserver.port}`
)

parentSocket.on(`develop:refresh-started`, msg => {
window.location.reload()
})

parentSocket.on(`develop:needs-restart`, msg => {
if (
window.confirm(
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ module.exports = async (program: IProgram): Promise<void> => {
if (msg.type === `REFRESH` && msg.action === `FINISHED`) {
proxy.refreshEnded()
}
if (msg.type === `REFRESH` && msg.action === `STARTED`) {
io.emit(`develop:refresh-started`)
}
}

io.on(`connection`, socket => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import PropTypes from "prop-types"
import { graphql, Link, navigate } from "gatsby"
import queryString from "query-string"
import io from "socket.io-client"

class Dev404Page extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -77,6 +78,22 @@ class Dev404Page extends React.Component {
}
}

componentDidMount() {
fetch(`/___services`)
.then(res => res.json())
.then(services => {
if (services.developstatusserver) {
const parentSocket = io(
`http://${window.location.hostname}:${services.developstatusserver.port}`
)

parentSocket.on(`develop:refresh-started`, msg => {
window.location.reload()
})
}
})
}

render() {
const { pathname } = this.props.location
let newFilePath
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/utils/develop-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export const startDevelopProxy = (input: {
return
}

const canShowRestartingScreenOnRefresh = !!process.env
.ENABLE_GATSBY_RESTARTING_SCREEN_ON_REFRESH

if (
(isRefreshing && canShowRestartingScreenOnRefresh) ||
shouldServeRestartingScreen ||
req.url === `/___debug-restarting-screen`
) {
Expand Down