-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (42 loc) · 1.58 KB
/
Copy pathserver.js
File metadata and controls
47 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// server.js
const { createServer } = require('http');
const next = require('next');
const { promises, existsSync } = require('fs');
const dev = (process.env.NODE_ENV || '').indexOf('production') === -1;
const app = next({ dev });
const handle = app.getRequestHandler();
const purgeData = async (pathname) => {
const fullPathname = `.next/server/pages${pathname}`;
const fullPathHTML = `${fullPathname}.html`;
const fullPathJSON = `${fullPathname}.json`;
try {
if (existsSync(fullPathHTML)) {
await promises.unlink(fullPathHTML);
console.info(`Deleted ${fullPathHTML}`);
}
if (existsSync(fullPathJSON)) {
await promises.unlink(fullPathJSON);
console.info(`deleted ${fullPathJSON}`);
}
// const cachedData = await app.server.incrementalCache.get(pathname);
// const staleTime = new Date().getTime() - 1000;
/* Delete the entry in cache */
app.server.incrementalCache.cache.del(pathname);
console.log(`Cache of ${fullPathname} was successfully purged`);
} catch (err) {
console.error(`Could not purge cache of ${fullPathname} - ${err}`);
}
}
app.prepare().then(() => {
createServer((req, res) => {
const url = new URL(req.url, "http://localhost:3000/");
if (url.searchParams.get('purge') == '1') {
purgeData(url.pathname).then(r => handle(req, res));
}else {
handle(req, res)
}
}).listen(3000, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:3000/`);
})
})