Skip to content

Commit 97247ea

Browse files
committed
Update keepChunks logic
1 parent b9b766e commit 97247ea

File tree

4 files changed

+542
-45
lines changed

4 files changed

+542
-45
lines changed

config/env.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ function getClientEnvironment(publicUrl) {
8585
}, {}),
8686
};
8787

88-
console.log(stringified);
89-
9088
const stringifiedForServerless = Object.keys(raw).reduce((env, key) => {
9189
env[`process.env.${key}`] = JSON.stringify(raw[key]);
9290
return env;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"date-fns": "^2.8.1",
6363
"dotenv": "8.2.0",
6464
"dotenv-expand": "5.1.0",
65+
"download": "^7.1.0",
6566
"eslint": "^6.6.0",
6667
"eslint-config-prettier": "^6.7.0",
6768
"eslint-config-react-app": "^5.1.0",

scripts/keepChunks.js

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const axios = require('axios');
44
const client = axios.default;
55
const fs = require('fs');
66
const path = require('path');
7+
const download = require('download');
78

89
const buildDir = path.join(__dirname, '../build/');
910

@@ -13,6 +14,9 @@ const assetHistoryDir = path.join(buildDir, 'asset-history.json');
1314
const readFileAsync = promisify(fs.readFile);
1415
const writeFileAsync = promisify(fs.writeFile);
1516

17+
/**
18+
* Loads current asset-history from CDN
19+
*/
1620
async function getAssetHistory() {
1721
try {
1822
const response = await client.get(
@@ -26,16 +30,27 @@ async function getAssetHistory() {
2630
}
2731
}
2832

33+
/**
34+
* reads asset-manifest.json of current build
35+
*/
2936
async function readAssetManifest() {
3037
const data = await readFileAsync(assetManifestDir, 'utf-8');
3138
return JSON.parse(data);
3239
}
3340

41+
/**
42+
* Saves asset-history.json with given data
43+
* @param data
44+
*/
3445
async function writeAssetHistory(data) {
3546
const stringified = JSON.stringify(data, null, 2);
3647
return writeFileAsync(assetHistoryDir, stringified, 'utf-8');
3748
}
3849

50+
/**
51+
* Filters out files that end with .map
52+
* @param {string[]} files
53+
*/
3954
function filterOutMapFiles(files) {
4055
return Object.fromEntries(
4156
Object.entries(files).filter(
@@ -44,12 +59,44 @@ function filterOutMapFiles(files) {
4459
);
4560
}
4661

62+
/**
63+
* Downloads file at the corresponding path
64+
* @param {string[]} urls
65+
*/
66+
function downloadUrls(urls) {
67+
return Promise.all(
68+
urls.map(url => {
69+
const downloadPath = url
70+
.slice(0, url.lastIndexOf('/'))
71+
.replace('https://static.velog.io', '');
72+
73+
return download(url, path.join(buildDir, downloadPath));
74+
}),
75+
);
76+
}
77+
4778
async function keepChunks() {
4879
const assetHistory = await getAssetHistory();
4980
const assetManifest = await readAssetManifest();
5081

51-
// TODO: filter out old data
52-
// TODO: download if exists...
82+
// filter out old data (always keep the latest one)
83+
const cacheDuration = 1000 * 60 * 60 * 24 * 3;
84+
const filteredHistory = assetHistory.history.filter(
85+
(item, index, array) =>
86+
index === array.length - 1 || Date.now() - item.date < cacheDuration,
87+
);
88+
89+
const urls = filteredHistory
90+
.reduce((acc, current) => {
91+
return acc.concat(Object.values(current.files));
92+
}, [])
93+
.filter(url =>
94+
['index.html', 'loadable-stats.json', 'service-worker.js'].every(
95+
ignored => !url.includes(ignored),
96+
),
97+
);
98+
99+
await downloadUrls(urls);
53100

54101
// update assetHistory
55102
assetHistory.history.push({
@@ -61,35 +108,3 @@ async function keepChunks() {
61108
}
62109

63110
keepChunks();
64-
65-
// const assetHistory = {
66-
// history: [
67-
// {
68-
// date: '2020-01-17T14:30:00.251Z',
69-
// files: {
70-
// 'static/js/0.98b2d894.chunk.js':
71-
// 'https://static.velog.io/static/js/0.98b2d894.chunk.js',
72-
// 'static/js/0.98b2d894.chunk.js.map':
73-
// 'https://static.velog.io/static/js/0.98b2d894.chunk.js.map',
74-
// },
75-
// },
76-
// {
77-
// date: '2020-01-17T14:30:00.251Z',
78-
// files: {
79-
// 'static/js/0.98b2d894.chunk.js':
80-
// 'https://static.velog.io/static/js/0.98b2d894.chunk.js',
81-
// 'static/js/0.98b2d894.chunk.js.map':
82-
// 'https://static.velog.io/static/js/0.98b2d894.chunk.js.map',
83-
// },
84-
// },
85-
// ],
86-
// };
87-
88-
// https://static.velog.io/manifest-history.json을 다운로드 (axios로 요청하는것 만으로도 충분)
89-
// 3일 이상된 객체들을 filter out
90-
// 남아있는 것들 모두 다운로드를 하고
91-
// 현재 asset-manifest 열어서
92-
// files의 .map으로 끝나는 것들 filter out 한다음에
93-
// 배열에 push
94-
95-
// 이걸 manifest-history.json으로 build 디렉터리에 저장

0 commit comments

Comments
 (0)