Skip to content

Commit 54a8453

Browse files
authored
fix(prerenderer): HEAD requests cause double requests (#594)
1 parent 4c2ebe1 commit 54a8453

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

client/src/run-server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function render(pathname, args='', body, locals={}, cb) {
2929
dispose()
3030

3131
cb(null, data || {
32-
html: lastHtml
32+
html: locals.isHead ? '' : lastHtml
3333
, title: lastState.title
3434
, status: lastState.view == 'notFound' ? 404
3535
: lastState.view == 'error' ? lastState.error.status || 400
@@ -38,7 +38,7 @@ export default function render(pathname, args='', body, locals={}, cb) {
3838
}
3939

4040
function htmlUpdate(html) {
41-
lastHtml = html
41+
if (!locals.isHead) lastHtml = html
4242
}
4343
function stateUpdate(S) {
4444
if (!lastState) { // the first state

prerender-server/client

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../client/src
1+
../client/dist

prerender-server/src/server.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import request from 'superagent'
77
import l10n from '../client/l10n'
88
import render from '../client/run-server'
99

10-
const themes = [ 'light', 'dark' ]
11-
, langs = Object.keys(l10n)
12-
, baseHref = process.env.BASE_HREF || '/'
13-
, canonBase = process.env.CANONICAL_URL ? process.env.CANONICAL_URL.replace(/\/$/, '') : null
14-
, apiUrl = process.env.API_URL.replace(/\/$/, '')
10+
const themes = ['light', 'dark']
11+
, langs = Object.keys(l10n)
12+
, baseHref = process.env.BASE_HREF || '/'
13+
, canonBase = process.env.CANONICAL_URL ? process.env.CANONICAL_URL.replace(/\/$/, '') : null
14+
, apiUrl = process.env.API_URL.replace(/\/$/, '')
1515

1616
const rpath = p => path.join(__dirname, p)
1717

@@ -37,7 +37,7 @@ app.use((req, res, next) => {
3737
if (!langs.includes(lang)) lang = 'en'
3838
if (req.query.lang && req.cookies.lang !== lang) res.cookie('lang', lang)
3939

40-
render(req._parsedUrl.pathname, req._parsedUrl.query || '', req.body, { theme, lang }, (err, resp) => {
40+
render(req._parsedUrl.pathname, req._parsedUrl.query || '', req.body, { theme, lang, isHead: req.method === 'HEAD' }, (err, resp) => {
4141
if (err) return next(err)
4242
if (resp.redirect) return res.redirect(301, baseHref + resp.redirect.substr(1))
4343
if (resp.errorCode) {
@@ -48,11 +48,11 @@ app.use((req, res, next) => {
4848
res.status(resp.status || 200)
4949
res.render(indexView, {
5050
prerender_title: resp.title
51-
, prerender_html: resp.html
52-
, canon_url: canonBase ? canonBase + req.url : null
53-
, noscript: true
54-
, theme
55-
, t: l10n[lang]
51+
, prerender_html: resp.html
52+
, canon_url: canonBase ? canonBase + req.url : null
53+
, noscript: true
54+
, theme
55+
, t: l10n[lang]
5656
})
5757
})
5858

@@ -64,10 +64,10 @@ if (process.env.SOCKET_PATH) {
6464
if (fs.statSync(process.env.SOCKET_PATH).isSocket()) {
6565
fs.unlinkSync(process.env.SOCKET_PATH)
6666
}
67-
} catch (_) {}
67+
} catch (_) { }
6868
}
6969

70-
app.listen(process.env.SOCKET_PATH || process.env.PORT || 5001, function(){
70+
app.listen(process.env.SOCKET_PATH || process.env.PORT || 5001, function () {
7171
let addr = this.address()
7272
if (addr.address) addr = `${addr.address}:${addr.port}`
7373
console.log(`HTTP server running on ${addr}`)

0 commit comments

Comments
 (0)