-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Resul Avan
authored and
Resul Avan
committed
Jun 10, 2020
1 parent
16a0811
commit 75fcf59
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import express,{ Request, Response, Router } from 'express'; | ||
import { SitemapStream, streamToPromise } from 'sitemap'; | ||
import { createGzip } from 'zlib' | ||
|
||
const service = '/sitemap.xml'; | ||
|
||
const staticRoutes = [ | ||
'/', | ||
'/terms', | ||
'/privacy-policy', | ||
'/register', | ||
'/login', | ||
'/crop', | ||
'/lightbox' | ||
] | ||
|
||
const app = express(); | ||
const router = Router(); | ||
|
||
let sitemap: Buffer | null = null; | ||
|
||
router.get(service, (req: Request, res: Response) => { | ||
console.log(req.originalUrl, ' called (get)'); | ||
res.header('Content-Type', 'application/xml'); | ||
res.header('Content-Encoding', 'gzip'); | ||
|
||
if (sitemap) { | ||
res.send(sitemap) | ||
return | ||
} | ||
|
||
try { | ||
const smStream = new SitemapStream({ hostname: 'https://nuxt-ts-firebase-auth-ssr.web.app/' }) | ||
const pipeline = smStream.pipe(createGzip()) | ||
|
||
staticRoutes.forEach((route) => smStream.write({ url: route, changefreq: 'weekly', priority: 0.8 })) | ||
smStream.end() | ||
|
||
// cache the response | ||
streamToPromise(pipeline).then((sm: Buffer) => sitemap = sm).catch((error:Error) => console.log(error)) | ||
// stream write the response | ||
pipeline.pipe(res).on('error', (e: Error) => { | ||
throw e | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
res.status(500).end() | ||
} | ||
}); | ||
|
||
app.use(router) | ||
|
||
export default { | ||
handler: app | ||
} |