Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.

Commit 8cd7ffb

Browse files
authored
Merge pull request #2 from eukarya-inc/fix-backpost-header
Fix backpost http header
2 parents d801473 + e60bd0b commit 8cd7ffb

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Diff for: src/lib/utility.js

+8
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ function getMineType(url, currentContentType) {
5353
return currentContentType;
5454
}
5555

56+
function isContent(originalUrl) {
57+
if (originalUrl.startsWith('/image') || originalUrl.startsWith('/icons') || originalUrl.endsWith('.wasm')) {
58+
return true
59+
}
60+
return false;
61+
}
62+
5663
module.exports = {
5764
generateSitemap: generateSitemap,
5865
generateNotionUrl: generateNotionUrl,
5966
getMineType: getMineType,
67+
isContent: isContent,
6068
};

Diff for: src/proxy/contentCache.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class ContentCache {
99
return;
1010
}
1111

12-
this.cache[originUrl] = new CacheData(this.toSec(Date.now()) + this.expiresSec, content);
12+
const now = this.toSec(Date.now());
13+
this.cache[originUrl] = new CacheData(now + this.expiresSec, content);
1314
}
1415

1516
getData(originUrl) {

Diff for: src/proxy/notionProxy.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ class NotionProxy {
6464
let url;
6565
try {
6666
url = utility.generateNotionUrl(req, res, this.PERMA_TO_PAGE, this.SLUG_TO_PAGE);
67-
//console.log('PROXY_TO ' + url)
67+
// console.log('[DEBUG] PROXY_TO ' + url)
6868
} catch (e) {
6969
if (e instanceof Redirect) {
70-
//console.log('REDIRECT_TO ' + e.message);
70+
// console.log('[DEBUG] REDIRECT_TO ' + e.message);
7171
return res.redirect(301, '/' + e.message);
7272
} else {
7373
console.error(e)
@@ -77,16 +77,15 @@ class NotionProxy {
7777
}
7878

7979
const requestHeader = req.headers
80-
requestHeader['If-Modified-Since'] = new Date().toString();
8180
delete requestHeader['host']
8281
delete requestHeader['referer']
8382
res.headers = requestHeader
83+
8484
let contentType = mime.lookup(req.originalUrl)
8585
if (!contentType) {
8686
contentType = 'text/html'
8787
}
8888
contentType = utility.getMineType(req.originalUrl, contentType);
89-
9089
res.set('Content-Type', contentType)
9190
res.removeHeader('Content-Security-Policy')
9291
res.removeHeader('X-Content-Security-Policy')
@@ -96,30 +95,31 @@ class NotionProxy {
9695
return res.send(cachedData);
9796
}
9897

98+
if (utility.isContent(req.originalUrl)) {
99+
// Set it to If-Modified-Since now to accommodate 304
100+
requestHeader['If-Modified-Since'] = new Date().toString();
101+
}
102+
99103
return fetch(url, {
100104
headers: requestHeader,
101105
method: 'GET',
102106
}, (error, header, body) => {
103-
let isObjectData = false;
104107

105108
// See https://github.com/stephenou/fruitionsite
106109
if (req.originalUrl.startsWith('/app') && req.originalUrl.endsWith('js')) {
107110
res.set('Content-Type', 'application/x-javascript')
108111
body = body.toString().replace(/www.notion.so/g, this.DOMAIN).replace(/notion.so/g, this.DOMAIN)
109112
} else if (req.originalUrl.endsWith('css') || req.originalUrl.endsWith('js')) {
110113
body = body.toString()
111-
} else if (req.originalUrl.startsWith('/image') || req.originalUrl.startsWith('/icons') || req.originalUrl.endsWith('.wasm')) {
114+
} else if (utility.isContent(req.originalUrl)) {
112115
// Nothing
113-
isObjectData = true;
114116
} else if (header !== undefined) {
115117
const dom = new JSDOM(body.toString(), { includeNodeLocations: true })
116118
this.PARSER.parse(dom.window.document);
117119
body = dom.serialize();
118120
}
119121

120-
if (!isObjectData) {
121-
this.CACHE_STORE.setData(req.originalUrl, body);
122-
}
122+
this.CACHE_STORE.setData(req.originalUrl, body);
123123
return res.send(body);
124124
})
125125
}

0 commit comments

Comments
 (0)