Skip to content

Commit

Permalink
Delete all HTML at start of bootstrap fixes #1811 (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews authored Aug 19, 2017
1 parent 05204d3 commit afc6117
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
18 changes: 3 additions & 15 deletions docs/docs/deploy-gatsby.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,16 @@
title: "Deploying Gatsby"
---

## Best Practice


Though you can deploy from the same location multiple times it is recommended that you clear your public directory of any `.html` files before each build
e.g. using surge

```bash
rm -rf public/*.html && gatsby build && surge public/
```

We suggest creating an [NPM script](https://docs.npmjs.com/cli/run-script) for your deploy script.

## Tutorials for deploying on different static site hosts

* [S3/Cloudfront](/docs/deploy-gatsby/#amazon-s3-and-cloudfront)
* [Github Pages](/docs/deploy-gatsby/#github-pages)

## Amazon S3 and Cloudfront

If you decide to host your Gatsby site to S3 having Cloudfront as CDN you should edit on the Cloudfront panel the "Origin Domain Name" with the real URL of your S3 bucket: **examplewebsite.com.s3-website-eu-west-1.amazonaws.com** instead of the default one automatically suggested by Amazon **examplewebsite.com.s3.amazonaws.com**.
If you decide to host your Gatsby site on S3 with Cloudfront as CDN, you should change the "Origin Domain Name" on the Cloudfront panel with the real URL of your S3 bucket: **examplewebsite.com.s3-website-eu-west-1.amazonaws.com** replacing the default URL suggested by Amazon **examplewebsite.com.s3.amazonaws.com**.

This is recommended for rendering correctly the post pages in the subfolders without typing the index.html path as described [here](https://forums.aws.amazon.com/message.jspa?messageID=314454).
Without this change, [S3 doesn't look for index.html files when serving "clean urls"](https://forums.aws.amazon.com/message.jspa?messageID=314454).

## Github Pages

Expand All @@ -39,7 +27,7 @@ Then add a **deploy** command in your `package.json` file.

```
"scripts": {
"deploy": "rm -rf public && gatsby build --prefix-paths && gh-pages -d public",
"deploy": "gatsby build --prefix-paths && gh-pages -d public",
}
```

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"core-js": "^2.5.0",
"css-loader": "^0.26.1",
"debug": "^2.6.0",
"del": "^3.0.0",
"detect-port": "^1.2.1",
"domready": "^1.0.8",
"dotenv": "^4.0.0",
Expand Down
12 changes: 10 additions & 2 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const slash = require(`slash`)
const fs = require(`fs-extra`)
const md5File = require(`md5-file/promise`)
const crypto = require(`crypto`)
const del = require(`del`)

const apiRunnerNode = require(`../utils/api-runner-node`)
const testRequireError = require(`../utils/test-require-error`)
Expand All @@ -31,16 +32,23 @@ const { writePages } = require(`../internal-plugins/query-runner/pages-writer`)
const preferDefault = m => (m && m.default) || m

module.exports = async (program: any) => {
// Fix program directory path for windows env
// Fix program directory path for windows env.
program.directory = slash(program.directory)

store.dispatch({
type: `SET_PROGRAM`,
payload: program,
})

// Delete html files from the public directory as we don't want deleted
// pages from previous builds to stick around.
let activity = report.activityTimer(`delete html files from previous builds`)
activity.start()
await del([`public/*.html`, `public/**/*.html`])
activity.end()

// Try opening the site's gatsby-config.js file.
let activity = report.activityTimer(`open and validate gatsby-config.js`)
activity = report.activityTimer(`open and validate gatsby-config.js`)
activity.start()
let config
try {
Expand Down
35 changes: 25 additions & 10 deletions packages/gatsby/src/utils/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ async function startServer(program) {
req.pipe(request(proxiedUrl)).pipe(res)
})
}

// Check if the file exists in the public folder.
app.get(`*`, (req, res, next) => {
// Load file but ignore errors.
res.sendFile(directoryPath(`/public/${req.url}`), err => {
// No err so a file was sent successfully.
if (!err) {
next()
} else if (err) {
// There was an error. Let's check if the error was because it
// couldn't find an HTML file. We ignore these as we want to serve
// all HTML from our single empty SSR html file.
const parsedPath = parsePath(err.path)
if (
parsedPath.extname === `` ||
parsedPath.extname.startsWith(`.html`)
) {
next()
} else {
res.status(404).end()
}
}
})
})

// Render an HTML page and serve it.
app.use((req, res, next) => {
const parsedPath = parsePath(req.originalUrl)
Expand All @@ -119,16 +144,6 @@ async function startServer(program) {
}
})

// As last step, check if the file exists in the public folder.
app.get(`*`, (req, res) => {
// Load file but ignore errors.
res.sendFile(directoryPath(`/public/${req.url}`), err => {
if (err) {
res.status(404).end()
}
})
})

/**
* Set up the HTTP server and socket.io.
**/
Expand Down

0 comments on commit afc6117

Please sign in to comment.