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

The dev server now checks if path matches a file #268

Merged
merged 3 commits into from
May 4, 2016
Merged
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
6 changes: 4 additions & 2 deletions lib/isomorphic/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ https://github.com/gatsbyjs/gatsby/blob/master/lib/isomorphic/html.js
`
console.info(defaultMessage)

export default function HTML (props) {
function HTML (props) {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0 maximum-scale=1.0"
content="width=device-width, initial-scale=1.0 maximum-scale=5.0"
/>
</head>
<body>
Expand All @@ -30,3 +30,5 @@ export default function HTML (props) {
}

HTML.propTypes = { body: PropTypes.any }

module.exports = HTML
4 changes: 3 additions & 1 deletion lib/isomorphic/pages/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ https://github.com/gatsbyjs/gatsby/blob/master/lib/isomorphic/pages/_template.js
`
console.info(defaultMessage)

export default function template (props) {
function template (props) {
return <div>{props.children}</div>
}

template.propTypes = { children: PropTypes.any }

module.exports = template
45 changes: 34 additions & 11 deletions lib/utils/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import find from 'lodash/find'
import webpackRequire from 'webpack-require'
import WebpackPlugin from 'hapi-webpack-plugin'
import opn from 'opn'
import fs from 'fs'

import globPages from './glob-pages'
import webpackConfig from './webpack.config'
Expand All @@ -24,7 +25,12 @@ module.exports = (program) => {

const compiler = webpack(compilerConfig.resolve())

const HTMLPath = `${directory}/html`
let HTMLPath = `${directory}/html`
try {
fs.lstatSync(HTMLPath)
} catch (e) {
HTMLPath = '../isomorphic/html'
}

const htmlCompilerConfig = webpackConfig(program, directory, 'develop', program.port)
// Remove react-transform option from Babel as redbox-react doesn't work
Expand Down Expand Up @@ -97,20 +103,37 @@ module.exports = (program) => {
server.ext('onRequest', (request, reply) => {
const negotiator = new Negotiator(request.raw.req)

if (negotiator.mediaType() === 'text/html') {
request.setUrl(`/html${request.path}`)
// Try to map the url path to match an actual path of a file on disk.
const parsed = parsePath(request.path)
const page = find(pages, (p) => p.path === (`${parsed.dirname}/`))

let absolutePath = `${directory}/pages`
let path
if (page) {
path = `/${parsePath(page.requirePath).dirname}/${parsed.basename}`
absolutePath += `/${parsePath(page.requirePath).dirname}/${parsed.basename}`
} else {
path = request.path
absolutePath += request.path
}
let isFile = false
try {
isFile = fs.lstatSync(absolutePath).isFile()
} catch (e) {
// Ignore.
}

// If the path matches a file, return that.
if (isFile) {
request.setUrl(path)
reply.continue()
// Let people load the bundle.js directly.
} else if (request.path === '/bundle.js') {
reply.continue()
} else if (negotiator.mediaType() === 'text/html') {
request.setUrl(`/html${request.path}`)
reply.continue()
} else {
// Rewrite path to match disk path.
const parsed = parsePath(request.path)
const page = find(pages, (p) => p.path === (`${parsed.dirname}/`))

if (page) {
request.setUrl(`/${parsePath(page.requirePath).dirname}/${parsed.basename}`)
}

reply.continue()
}
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
},
"devDependencies": {
"ava": "^0.14.0",
"ava-http": "^0.2.1",
"babel-cli": "^6.7.5",
"babel-eslint": "^6.0.2",
"babel-register": "^6.7.2",
Expand Down