Last active
January 3, 2022 00:31
-
-
Save sand97/fa97179bdae673640467303c466c647a to your computer and use it in GitHub Desktop.
Custom Next.js server.js with purge cache handler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}`); | |
} | |
/* 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') { | |
/* Handle incoming request after purge the cache */ | |
purgeData(url.pathname).then(r => handle(req, res)); | |
}else { | |
/* Handle incoming request without purge the cache */ | |
handle(req, res) | |
} | |
}).listen(3000, (err) => { | |
if (err) throw err; | |
console.log(`> Ready on http://localhost:3000/`); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment