Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 3a768bd

Browse files
committed
Handle alternative paths the same way as when deployed
This makes sure netlify dev handles .html/.htm/index.html/index.htm paths the same way our origin server handles these, independently of the underlying dev servers handling of pretty URLs etc
1 parent 9648d86 commit 3a768bd

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"bugs": "https://github.com/netlify/netlify-dev-plugin/issues",
77
"dependencies": {
88
"@netlify/cli-utils": "^1.0.1",
9-
"@netlify/rules-proxy": "^0.1.0",
9+
"@netlify/rules-proxy": "^0.1.1",
1010
"@netlify/zip-it-and-ship-it": "^0.1.3",
1111
"@oclif/command": "^1",
1212
"@oclif/config": "^1",

src/commands/dev/index.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,61 @@ function addonUrl(addonUrls, req) {
2222
return addonUrl ? `${addonUrl}${m[2]}` : null
2323
}
2424

25+
// Used as an optimization to avoid dual lookups for missing assets
26+
const assetExtensionRegExp = /\.(html?|png|jpg|js|css|svg|gif|ico|woff|woff2)$/
27+
28+
function alternativePathsFor(url) {
29+
const paths = []
30+
if (url[url.length - 1] === '/') {
31+
const end = url.length - 1
32+
if (url !== '/') {
33+
paths.push(url.slice(0, end) + '.html')
34+
paths.push(url.slice(0, end) + '.htm')
35+
}
36+
paths.push(url + 'index.html')
37+
paths.push(url + 'index.htm')
38+
} else if (!url.match(assetExtensionRegExp)) {
39+
paths.push(url + '.html')
40+
paths.push(url + '.htm')
41+
paths.push(url + '/index.html')
42+
paths.push(url + '/index.htm')
43+
}
44+
45+
return paths
46+
}
47+
48+
function initializeProxy(port) {
49+
const proxy = httpProxy.createProxyServer({
50+
selfHandleResponse: true,
51+
target: {
52+
host: 'localhost',
53+
port: port
54+
}
55+
})
56+
57+
proxy.on('proxyRes', (proxyRes, req, res) => {
58+
if (proxyRes.statusCode === 404 && req.alternativePaths && req.alternativePaths.length) {
59+
req.url = req.alternativePaths.shift()
60+
return proxy.web(req, res, req.proxyOptions)
61+
}
62+
res.writeHead(proxyRes.statusCode, proxyRes.headers)
63+
proxyRes.on('data', function(data) {
64+
res.write(data)
65+
})
66+
proxyRes.on('end', function() {
67+
res.end()
68+
})
69+
})
70+
71+
return {
72+
web: (req, res, options) => {
73+
req.proxyOptions = options
74+
req.alternativePaths = alternativePathsFor(req.url)
75+
return proxy.web(req, res, options)
76+
}
77+
}
78+
}
79+
2580
async function startProxy(settings, addonUrls) {
2681
const rulesProxy = require('@netlify/rules-proxy')
2782

@@ -32,12 +87,7 @@ async function startProxy(settings, addonUrls) {
3287
const port = await getPort({ port: settings.port })
3388
const functionsServer = settings.functionsPort ? `http://localhost:${settings.functionsPort}` : null
3489

35-
const proxy = httpProxy.createProxyServer({
36-
target: {
37-
host: 'localhost',
38-
port: settings.proxyPort
39-
}
40-
})
90+
const proxy = initializeProxy(settings.proxyPort)
4191

4292
const rewriter = rulesProxy({ publicFolder: settings.dist })
4393

0 commit comments

Comments
 (0)