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

Commit e7c9fa4

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 3d2516d commit e7c9fa4

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,61 @@ function addonUrl(addonUrls, req) {
2323
return addonUrl ? `${addonUrl}${m[2]}` : null;
2424
}
2525

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

0 commit comments

Comments
 (0)